Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 487 #488

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading