Skip to content

Commit e99aa43

Browse files
authored
fix issue 487 (#488)
Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent 478bce9 commit e99aa43

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/Utils/Utils.jl

+11-5
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ module Utils
9090
mimes = copy(ALL_MIMES)
9191
# look for mimes on show methods for this type
9292
for meth in methods(show, Tuple{IO, MIME, typeof(x)}).ms
93-
mimetype = _type_ub(meth.sig).parameters[3]
93+
mimetype = _unwrap_unionall(meth.sig).parameters[3]
9494
mimetype isa DataType || continue
95+
mimetype <: MIME || continue
9596
mime = string(mimetype.parameters[1])
9697
push!(mimes, mime)
9798
end
@@ -109,10 +110,7 @@ module Utils
109110
end
110111

111112
@generated _type_lb(::Type{T}) where {T} = begin
112-
R = T
113-
while R isa UnionAll
114-
R = R.body
115-
end
113+
R = _unwrap_unionall(T)
116114
if R isa DataType
117115
S = T
118116
while S isa UnionAll
@@ -124,6 +122,14 @@ module Utils
124122
end
125123
end
126124

125+
@generated function _unwrap_unionall(::Type{T}) where {T}
126+
R = T
127+
while R isa UnionAll
128+
R = R.body
129+
end
130+
R
131+
end
132+
127133
@generated _promote_type_bounded(::Type{S}, ::Type{T}, ::Type{B}) where {S,T,B} = begin
128134
S <: B || error("require S <: B")
129135
T <: B || error("require T <: B")

test/Utils.jl

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
@testitem "mimes_for" begin
2-
for x in Any[1, "foo", [], 'z']
3-
@test PythonCall.Utils.mimes_for(x) isa Vector{String}
2+
# this example from https://github.com/JuliaPy/PythonCall.jl/issues/487
3+
struct Test{T<:Number}
4+
x::T
5+
end
6+
Base.show(io::IO, ::MIME"text/plain", x::Test{T}) where T = show(io, x.t)
7+
Base.show(io::IO, ::MIME"text/x-test", x::Test) = show(io, x.t)
8+
9+
@testset for x in Any[1, "foo", [], 'z', Test(5)]
10+
mimes = PythonCall.Utils.mimes_for(x)
11+
@test mimes isa Vector{String}
12+
@test "text/plain" in mimes
13+
@test "text/html" in mimes
14+
@test ("text/x-test" in mimes) == (x isa Test)
415
end
516
end
617

0 commit comments

Comments
 (0)