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

Use promote_typejoin for Tuple and NamedTuple promotion #25924

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ indexed_next(t::NamedTuple, i::Int, state) = (getfield(t, i), i+1)
isempty(::NamedTuple{()}) = true
isempty(::NamedTuple) = false

promote_rule(::Type{NamedTuple{n, S}}, ::Type{NamedTuple{n, T}}) where {n, S, T} =
NamedTuple{n, promote_type(S, T)}

promote_typejoin(::Type{NamedTuple{n, S}}, ::Type{NamedTuple{n, T}}) where {n, S, T} =
NamedTuple{n, promote_typejoin(S, T)}

Expand Down
6 changes: 6 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ function _compute_eltype(t::Type{<:Tuple})
return r
end

# promotion

# Not an actual promotion, just an improved typejoin
promote_rule(::Type{S}, ::Type{T}) where {S<:Tuple, T<:Tuple} = promote_typejoin(S, T)
promote_rule(::Type{NTuple{N,Any}}, ::Type{<:NTuple{N}}) where {N} = NTuple{N,Any}

# version of tail that doesn't throw on empty tuples (used in array indexing)
safe_tail(t::Tuple) = tail(t)
safe_tail(t::Tuple{}) = ()
Expand Down
27 changes: 27 additions & 0 deletions test/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,36 @@ abstr_nt_22194_3()
@test findfirst(equalto(1), (a=2, b=3)) === nothing
@test findlast(equalto(1), (a=2, b=3)) === nothing

# Test promotion

@test promote_type(NamedTuple{(:a,),Tuple{Int}},
NamedTuple{(:a,),Tuple{Float64}}) ===
NamedTuple{(:a,),Tuple{Real}}
@test promote_type(NamedTuple{(:a,:b),Tuple{Int,Float64}},
NamedTuple{(:a,:b),Tuple{Float64,Int}}) ===
NamedTuple{(:a, :b),Tuple{Real, Real}}
@test promote_type(NamedTuple{(:a,:b),Tuple{Int,String}},
NamedTuple{(:a,:b),Tuple{Float64,Int}}) ===
NamedTuple{(:a,:b),Tuple{Real,Any}}

for T in (Nothing, Missing)
@test promote_type(NamedTuple{(:a,),Tuple{Int}},
NamedTuple{(:a,),Tuple{T}}) ===
NamedTuple{(:a,),Tuple{Union{T,Int}}}
@test promote_type(NamedTuple{(:a,:b),Tuple{Int,T}},
NamedTuple{(:a,:b),Tuple{T,Float64}}) ===
NamedTuple{(:a, :b),Tuple{Union{T,Int}, Union{T,Float64}}}
@test promote_type(NamedTuple{(:a,),Tuple{Int}},
NamedTuple{(:a,),Tuple{Float64}},
NamedTuple{(:a,),Tuple{T}}) ===
NamedTuple{(:a,),Tuple{Union{Any,T}}}
end

# Test map with Nothing and Missing
for T in (Nothing, Missing)
x = [(a=1, b=T()), (a=1, b=2)]
@test x isa Vector{NamedTuple{(:a,:b),Tuple{Int,Union{T,Int}}}}

y = map(v -> (a=v.a, b=v.b), [(a=1, b=T()), (a=1, b=2)])
@test y isa Vector{NamedTuple{(:a,:b),Tuple{Int,Union{T,Int}}}}
@test isequal(x, y)
Expand Down
20 changes: 20 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,31 @@ end
typejoin(Int, Float64, Bool)
@test eltype(Tuple{Int, Missing}) === Union{Missing, Int}
@test eltype(Tuple{Int, Nothing}) === Union{Nothing, Int}
@test eltype(Tuple{Int, Float64, Missing}) === Any
@test eltype(Tuple{Int, Float64, Nothing}) === Any
end

@testset "promotion" begin
@test promote_type(Tuple{Int}, Tuple{Float64}) === Tuple{Real}
@test promote_type(Tuple{Int,Float64}, Tuple{Float64,Int}) === Tuple{Real,Real}
@test promote_type(Tuple{Int,String}, Tuple{Float64,Int}) === Tuple{Real,Any}

@test promote_type(Tuple{Any}, Tuple{Int}) === Tuple{Any}
@test promote_type(Tuple{Any,Any}, Tuple{Float64,Int}) === Tuple{Any,Any}

for T in (Nothing, Missing)
@test promote_type(Tuple{Int}, Tuple{T}) === Tuple{Union{T,Int}}
@test promote_type(Tuple{Int,T}, Tuple{T,Float64}) ===
Tuple{Union{T,Int},Union{T,Float64}}
@test promote_type(Tuple{Int}, Tuple{Float64}, Tuple{T}) === Tuple{Any}
end
end

@testset "map with Nothing and Missing" begin
for T in (Nothing, Missing)
x = [(1, T()), (1, 2)]
@test x isa Vector{Tuple{Int,Union{Int,T}}}

y = map(v -> (v[1], v[2]), [(1, T()), (1, 2)])
@test y isa Vector{Tuple{Int,Union{T,Int}}}
@test isequal(x, y)
Expand Down