-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deprecate unintended methods of zeros, ones #21183
Conversation
base/deprecated.jl
Outdated
@@ -1298,6 +1298,10 @@ end | |||
end | |||
end | |||
|
|||
# #19635 | |||
@deprecate zeros{T <: Number}(::Type{T}, arr::Range) zeros(T, length(arr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not just Range
. 0.5:
julia> zeros(Int, [1,2,3,4])
4-element Array{Int64,1}:
0
0
0
0
master:
julia> zeros(Int, [1,2,3,4])
4-element Array{Int64,1}:
0
0
0
0
On 0.5, zeros(T, arg)
ends up calling fill!(convert(Array{T}, arg), zero(T))
. So anything that can be converted to an Array
is a plausible input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could restore zeros(Int, [1,2,3])
with a depwarn, but zeros(Int, [1,2,3])
was buggy, see #19265 and I have a test that this case throws an error now. So removing the error in favor of a depwarn is a bit awkward.
test/arrayops.jl
Outdated
@test x == [1.] | ||
end | ||
# #19265" | ||
@test_throws Exception zeros(Float64, [1.]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@test_throws Exception
isn't very useful, there could be a typo in the implementation and it would pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes originally it was a MethodError
. And indeed a MethodError
will be thrown, when the depwarn is deleted. Should I change it back to MethodError
again and make the depwarn throw a MethodError
?
base/deprecated.jl
Outdated
@eval function ($f){T}(::Type{T}, arr::Array{T}) | ||
msg = string("`", $f , "{T}(::Type{T}, arr::Array{T})` is deprecated, use ", | ||
"`", $f , "(T, size(arr))` instead.", | ||
"A `MethodError` will be thrown." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error(::String)
throws an ErrorException, not a MethodError
I encounter a strange behaviour.
fails depending on where it is called. In the REPL it passes. If I do
If I just type
|
likely depends on the value of the --depwarn flag, if you're trying to test a method that gives a warning unfortunately MethodError can't really be customized with a deprecation message at the moment. though might be better to have the message only in the depwarn, then the error can be whatever. Tim, opinion on what to throw? |
Okay, do I get this correctly: CI will never pass if there is a |
I think this PR is ready. |
base/array.jl
Outdated
@@ -251,6 +251,14 @@ for (fname, felt) in ((:zeros,:zero), (:ones,:one)) | |||
$fname(dims::Tuple) = ($fname)(Float64, dims) | |||
$fname(T::Type, dims...) = $fname(T, dims) | |||
$fname(dims...) = $fname(dims) | |||
|
|||
# #19635 | |||
function ($fname){T}(::Type{T}, arr::Array{T}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be moved to base/deprecated.jl
. That way it will be deleted when we delete the 0.6 deprecations (which happens late in the next release cycle); we might not notice/remember to delete it if it's in array.jl
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One also needs to change a @test_throws
when this is deleted. Thats why I put it here and not in deprecated.jl
. You are still in favor of deprecated.jl
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that is a bit tricky. Still, I think I'd favor moving this to deprecated.jl
and then writing the test like this:
@test_throws ErrorException error("some error") # TODO: change this to MethodError when 0.6 deprecations are deleted
@jw3126, I was feeling guilty about asking for more changes, so I just pushed #21265. That's mostly for discussion purposes (you can make the modifications yourself if you prefer). The main differences:
If you're fine with #21265, we can merge that, but if it has problems please let me know. |
|
Sounds good. |
Oh, and thanks for doing this! |
This makes the tests in Tensors.jl fail (bisected to here) Ferrite-FEM/Tensors.jl#53 (comment). Pre this PR: julia> eltype(zeros(Tensor{2, 3}, 3))
Tensors.Tensor{2,3,Float64,9}
julia> @which zeros(Tensor{2, 3}, 3)
zeros(::Type{T}, dims...) where T<:Tensors.AbstractTensor in Tensors at /home/kristoffer/Dropbox/julia/Tensors/src/constructors.jl:106 After: julia> eltype(zeros(Tensor{2, 3}, 3))
Tensors.Tensor{2,3,T,M} where M where T<:Real
julia> @which zeros(Tensor{2, 3}, 3)
ERROR: no method found for the specified argument types
Stacktrace:
[1] which(::Any, ::Any) at ./reflection.jl:824 The error message from |
That is weird. |
See @timholy's comment in #19635.