Skip to content

Commit eebecd2

Browse files
authored
Fix ambiguities on Julia 1.1 (#174)
JuliaLang/julia#29406 changed ambiguity rules, which makes the tests fail.
1 parent 8e5fc12 commit eebecd2

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/recode.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function recode! end
3030

3131
recode!(dest::AbstractArray, src::AbstractArray, pairs::Pair...) =
3232
recode!(dest, src, nothing, pairs...)
33+
# To fix ambiguity
34+
recode!(dest::CategoricalArray, src::AbstractArray, pairs::Pair...) =
35+
recode!(dest, src, nothing, pairs...)
36+
recode!(dest::CategoricalArray, src::CategoricalArray, pairs::Pair...) =
37+
recode!(dest, src, nothing, pairs...)
3338

3439
function recode!(dest::AbstractArray{T}, src::AbstractArray, default::Any, pairs::Pair...) where {T}
3540
if length(dest) != length(src)
@@ -251,7 +256,8 @@ julia> x
251256
-1
252257
```
253258
"""
254-
recode!(a::AbstractArray, default::Any, pairs::Pair...) = recode!(a, a, default, pairs...)
259+
recode!(a::AbstractArray, default::Any, pairs::Pair...) =
260+
recode!(a, a, default, pairs...)
255261
recode!(a::AbstractArray, pairs::Pair...) = recode!(a, a, nothing, pairs...)
256262

257263
promote_valuetype(x::Pair{K, V}) where {K, V} = V
@@ -326,6 +332,8 @@ julia> recode(1:10, 1=>100, 2:4=>0, [5; 9:10]=>-1, 6=>missing)
326332
function recode end
327333

328334
recode(a::AbstractArray, pairs::Pair...) = recode(a, nothing, pairs...)
335+
# To fix ambiguity
336+
recode(a::CategoricalArray, pairs::Pair...) = recode(a, nothing, pairs...)
329337

330338
function recode(a::AbstractArray, default::Any, pairs::Pair...)
331339
V = promote_valuetype(pairs...)

src/value.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,17 @@ Base.convert(::Type{Ref}, x::CatValue) = RefValue{leveltype(x)}(x)
8282
Base.convert(::Type{String}, x::CatValue) = convert(String, get(x))
8383
Base.convert(::Type{Any}, x::CatValue) = x
8484

85+
# Defined separately to avoid ambiguities
86+
Base.convert(::Type{AbstractString}, x::CategoricalString) = x
8587
Base.convert(::Type{T}, x::T) where {T <: CatValue} = x
86-
Base.convert(::Type{S}, x::T) where {S, T <: CatValue} = # fallback
88+
Base.convert(::Type{Union{T, Missing}}, x::T) where {T <: CatValue} = x
89+
Base.convert(::Type{Union{T, Nothing}}, x::T) where {T <: CatValue} = x
90+
# General fallbacks
91+
Base.convert(::Type{S}, x::T) where {S, T <: CatValue} =
92+
T <: S ? x : convert(S, get(x))
93+
Base.convert(::Type{Union{S, Missing}}, x::T) where {S, T <: CatValue} =
94+
T <: S ? x : convert(S, get(x))
95+
Base.convert(::Type{Union{S, Nothing}}, x::T) where {S, T <: CatValue} =
8796
T <: S ? x : convert(S, get(x))
8897

8998
(::Type{T})(x::T) where {T <: CatValue} = x

test/05_convert.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, is
2626
@test reftype(typeof(v1)) === DefaultRefType
2727
@test v1 isa CategoricalArrays.CategoricalValue{Int, DefaultRefType}
2828

29+
@test convert(Int, v1) === 1
30+
@test convert(Int, v2) === 2
31+
@test convert(Int, v3) === 3
32+
2933
@test convert(Int32, v1) === Int32(1)
3034
@test convert(Int32, v2) === Int32(2)
3135
@test convert(Int32, v3) === Int32(3)
@@ -55,7 +59,56 @@ using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, is
5559
@test promote(1, v1) === (1, 1)
5660
@test promote(1.0, v1) === (1.0, 1.0)
5761
@test promote(0x1, v1) === (1, 1)
62+
end
63+
64+
@testset "convert() for CategoricalPool{String, DefaultRefType} and values" begin
65+
pool = CategoricalPool(["a", "b", "c"])
66+
@test convert(CategoricalPool{String, DefaultRefType}, pool) === pool
67+
@test convert(CategoricalPool{String}, pool) === pool
68+
@test convert(CategoricalPool, pool) === pool
69+
convert(CategoricalPool{String, UInt8}, pool)
70+
71+
v1 = catvalue(1, pool)
72+
v2 = catvalue(2, pool)
73+
v3 = catvalue(3, pool)
74+
@test iscatvalue(v1)
75+
@test iscatvalue(typeof(v1))
76+
@test eltype(v1) === Char
77+
@test eltype(typeof(v1)) === Char
78+
@test leveltype(v1) === String
79+
@test leveltype(typeof(v1)) === String
80+
@test reftype(v1) === DefaultRefType
81+
@test reftype(typeof(v1)) === DefaultRefType
82+
@test v1 isa CategoricalArrays.CategoricalString{DefaultRefType}
83+
84+
@test convert(String, v1) == "a"
85+
@test convert(String, v2) == "b"
86+
@test convert(String, v3) == "c"
87+
88+
@test convert(AbstractString, v1) == "a"
89+
@test convert(AbstractString, v2) == "b"
90+
@test convert(AbstractString, v3) == "c"
91+
92+
@test convert(CategoricalString, v1) === v1
93+
@test convert(CategoricalString{DefaultRefType}, v1) === v1
94+
95+
@test convert(Any, v1) === v1
96+
@test convert(Any, v2) === v2
97+
@test convert(Any, v3) === v3
98+
99+
for T in (typeof(v1), CatValue, CategoricalString, CategoricalString{DefaultRefType}),
100+
U in (Missing, Nothing)
101+
@test convert(Union{T, U}, v1) === v1
102+
@test convert(Union{T, U}, v2) === v2
103+
@test convert(Union{T, U}, v3) === v3
104+
end
105+
106+
@test get(v1) === "a"
107+
@test get(v2) === "b"
108+
@test get(v3) === "c"
109+
end
58110

111+
@testset "promote_type" begin
59112
# Tests that return Any are due to JuliaLang/julia#29348
60113
# It is not clear what would be the most appropriate promotion type for them,
61114
# but at least they should not throw an error

0 commit comments

Comments
 (0)