Skip to content

Commit

Permalink
Add custom subtypes function to avoid deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dgleich committed Oct 13, 2024
1 parent 0bd5d85 commit 43a7f98
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
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 @@ const IMColorspace = [
"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

0 comments on commit 43a7f98

Please sign in to comment.