PyCall.jl
PyCall.jl copied to clipboard
deprecation message for `haskey` should only be printed on Julia 1.2 or higher
I'm using PyCall v1.92.1
Near the top, it has this code:
if isdefined(Base, :hasproperty) # Julia 1.2
import Base: hasproperty
end
so it only overrides hasproperty on 1.2 where it was added to Base.
However further down, we see this code:
function haskey(o::PyObject, s::Union{Symbol,AbstractString})
Base.depwarn("`haskey(o::PyObject, s::Union{Symbol, AbstractString})` is deprecated, use `hasproperty(o, s)` instead.", :haskey)
return hasproperty(o, s)
end
# defining hasproperty on a Union triggers a method ambiguity
function pyhasproperty(o::PyObject, s::Union{Symbol,AbstractString})
if ispynull(o)
throw(ArgumentError("hasproperty of NULL PyObject"))
end
return 1 == ccall((@pysym :PyObject_HasAttrString), Cint,
(PyPtr, Cstring), o, s)
end
hasproperty(o::PyObject, s::Symbol) = pyhasproperty(o, s)
hasproperty(o::PyObject, s::AbstractString) = pyhasproperty(o, s)
So we're overriding hasproperty even when it hasn't been imported (eg: on Julia 1.0), and any calls to haskey display the deprecation warning.
This can be confusing because if someone were to try to use hasproperty on julia 1.0, they'd get an UndefVarError since hasproperty doesn't exist in Base and isn't exported by PyCall.
Perhaps we should add the if isdefined(Base, :hasproperty) # Julia 1.2 condition before displaying the depwarn
Maybe this way so it's defined at compile time:
if isdefined(Base, :hasproperty) # Julia 1.2
function haskey(o::PyObject, s::Union{Symbol,AbstractString})
Base.depwarn("`haskey(o::PyObject, s::Union{Symbol, AbstractString})` is deprecated, use `hasproperty(o, s)` instead.", :haskey)
return hasproperty(o, s)
end
else
haskey(o::PyObject, s::Union{Symbol,AbstractString}) = hasproperty(o, s)
end