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

Add custom subtypes function to avoid deprecation warnings #236

Merged
merged 1 commit into from
Oct 15, 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
37 changes: 36 additions & 1 deletion src/libmagickwand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,42 @@
"Oklch"
]
const IMColordict = Dict([(IMColorspace[i], i) for i = 1:length(IMColorspace)])
for AC in vcat(subtypes(AlphaColor), subtypes(ColorAlpha))

# See the discussion of https://github.com/JuliaIO/ImageMagick.jl/issues/235
# This can be removed once the `isbindingresolved` function is checked in InteractiveUtils.jl: subtypes
# So if it starts to cause problems in future version of Julia, it can be
# removed without any issues
# This was copied from the Julia source code, and is used to avoid deprecation warnings
# caused by deprecations on RGB1 and RGB4 in ColorTypes.jl
function _subtypes_and_avoid_deprecation(x::Type)
mods = Base.loaded_modules_array()
xt = Base.unwrap_unionall(x)
if !isabstracttype(x) || !isa(xt, DataType)
# Fast path
return Type[]

Check warning on line 137 in src/libmagickwand.jl

View check run for this annotation

Codecov / codecov/patch

src/libmagickwand.jl#L137

Added line #L137 was not covered by tests
end
sts = Vector{Any}()
while !isempty(mods)
m = pop!(mods)
xt = xt::DataType
for s in names(m, all = true)
if Base.isbindingresolved(m, s) && !Base.isdeprecated(m, s) && isdefined(m, s)
t = getfield(m, s)
dt = isa(t, UnionAll) ? Base.unwrap_unionall(t) : t
if isa(dt, DataType)
if dt.name.name === s && dt.name.module == m && supertype(dt).name == xt.name
ti = typeintersect(t, x)
ti != Base.Bottom && push!(sts, ti)
end
elseif isa(t, Module) && nameof(t) === s && parentmodule(t) === m && t !== m
t === Base || push!(mods, t) # exclude Base, since it also parented by Main
end
end
end
end
return permute!(sts, sortperm(map(string, sts)))
end
for AC in vcat(_subtypes_and_avoid_deprecation(AlphaColor), _subtypes_and_avoid_deprecation(ColorAlpha))
Cstr = ColorTypes.colorant_string(color_type(AC))
if haskey(IMColordict, Cstr)
IMColordict[ColorTypes.colorant_string(AC)] = IMColordict[Cstr]
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Base.CoreLogging: SimpleLogger, with_logger
include("constructed_images.jl")
include("readremote.jl")
include("badimages.jl")
include("utilities.jl")

workdir = joinpath(tempdir(), "Images")
try
Expand Down
16 changes: 16 additions & 0 deletions test/utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using InteractiveUtils: subtypes
@testset "Utilities" begin
@testset "subtypes" begin
println()
println("---------------------------------------------------------------------------")
println(" This next test may cause deprecation warnings.")
println(" Remove this note if it stops doing that and remove the function.")
println(" _subtypes_and_avoid_deprecation ")
println(" function from libmagickwand.jl.")
println("---------------------------------------------------------------------------")
println()
@test subtypes(Integer) == ImageMagick._subtypes_and_avoid_deprecation(Integer)
@test subtypes(ImageMagick.ColorAlpha) == ImageMagick._subtypes_and_avoid_deprecation(ImageMagick.ColorAlpha)
@test subtypes(ImageMagick.AlphaColor) == ImageMagick._subtypes_and_avoid_deprecation(ImageMagick.AlphaColor)
end
end
Loading