diff --git a/src/Utils/Utils.jl b/src/Utils/Utils.jl index 921ef6d6..0b4a29f0 100644 --- a/src/Utils/Utils.jl +++ b/src/Utils/Utils.jl @@ -90,8 +90,9 @@ module Utils mimes = copy(ALL_MIMES) # look for mimes on show methods for this type for meth in methods(show, Tuple{IO, MIME, typeof(x)}).ms - mimetype = _type_ub(meth.sig).parameters[3] + mimetype = _unwrap_unionall(meth.sig).parameters[3] mimetype isa DataType || continue + mimetype <: MIME || continue mime = string(mimetype.parameters[1]) push!(mimes, mime) end @@ -109,10 +110,7 @@ module Utils end @generated _type_lb(::Type{T}) where {T} = begin - R = T - while R isa UnionAll - R = R.body - end + R = _unwrap_unionall(T) if R isa DataType S = T while S isa UnionAll @@ -124,6 +122,14 @@ module Utils end end + @generated function _unwrap_unionall(::Type{T}) where {T} + R = T + while R isa UnionAll + R = R.body + end + R + end + @generated _promote_type_bounded(::Type{S}, ::Type{T}, ::Type{B}) where {S,T,B} = begin S <: B || error("require S <: B") T <: B || error("require T <: B") diff --git a/test/Utils.jl b/test/Utils.jl index a0d38492..0898cfea 100644 --- a/test/Utils.jl +++ b/test/Utils.jl @@ -1,6 +1,17 @@ @testitem "mimes_for" begin - for x in Any[1, "foo", [], 'z'] - @test PythonCall.Utils.mimes_for(x) isa Vector{String} + # this example from https://github.com/JuliaPy/PythonCall.jl/issues/487 + struct Test{T<:Number} + x::T + end + Base.show(io::IO, ::MIME"text/plain", x::Test{T}) where T = show(io, x.t) + Base.show(io::IO, ::MIME"text/x-test", x::Test) = show(io, x.t) + + @testset for x in Any[1, "foo", [], 'z', Test(5)] + mimes = PythonCall.Utils.mimes_for(x) + @test mimes isa Vector{String} + @test "text/plain" in mimes + @test "text/html" in mimes + @test ("text/x-test" in mimes) == (x isa Test) end end