PyCall.jl icon indicating copy to clipboard operation
PyCall.jl copied to clipboard

deprecation message for `haskey` should only be printed on Julia 1.2 or higher

Open bluesmoon opened this issue 5 years ago • 1 comments

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

bluesmoon avatar Oct 07 '20 14:10 bluesmoon

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

bluesmoon avatar Oct 07 '20 14:10 bluesmoon