Skip to content

Commit

Permalink
fix issue 487 (#488)
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher Doris <github.com/cjdoris>
  • Loading branch information
cjdoris committed Apr 3, 2024
1 parent 478bce9 commit e99aa43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/Utils/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down
15 changes: 13 additions & 2 deletions test/Utils.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit e99aa43

Please sign in to comment.