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 pairwise for type-unstable corner case function #772

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions src/pairwise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ function _pairwise!(f, dest::AbstractMatrix, x, y;
return _pairwise!(Val(skipmissing), f, dest, x′, y′, symmetric)
end

if VERSION >= v"1.6.0-DEV"
# Function has moved in Julia 1.7
if isdefined(Base, :typejoin_union_tuple)
using Base: typejoin_union_tuple
else
using Base.Broadcast: typejoin_union_tuple
end
# Identical to `Base.promote_typejoin` except that it uses `promote_type`
# instead of `typejoin`
function promote_type_union(::Type{T}) where T
if T === Union{}
return Union{}
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
elseif T isa UnionAll
return Any # TODO: compute more precise bounds
elseif T isa Union
return promote_type(promote_type_union(T.a), promote_type_union(T.b))
elseif T <: Tuple
return typejoin_union_tuple(T)
else
return T
end
end
else
promote_type_union(::Type) = Any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to be that defensive? If passed type is not a union then I guess we can return it - right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Actually the only case that would be hard to support is Tuple types. I've refactored the code to improve this.

end

function _pairwise(::Val{skipmissing}, f, x, y, symmetric::Bool) where {skipmissing}
x′ = x isa Union{AbstractArray, Tuple, NamedTuple} ? x : collect(x)
y′ = y isa Union{AbstractArray, Tuple, NamedTuple} ? y : collect(y)
Expand All @@ -148,10 +174,11 @@ function _pairwise(::Val{skipmissing}, f, x, y, symmetric::Bool) where {skipmiss
if isconcretetype(eltype(dest))
return dest
else
# Final eltype depends on actual contents (consistent with map and broadcast)
# Final eltype depends on actual contents (consistent with `map` and `broadcast`
# but using `promote_type` rather than `promote_typejoin`)
U = mapreduce(typeof, promote_type, dest)
# V is inferred (contrary to U), but it only gives an upper bound for U
V = promote_type(T, Tsm)
V = promote_type_union(Union{T, Tsm})
return convert(Matrix{U}, dest)::Matrix{<:V}
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/pairwise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,17 @@ arbitrary_fun(x, y) = cor(x, y)
end
end
end

@testset "type-unstable corner case (#771)" begin
v = [rand(5) for _=1:10]
function f(v)
pairwise(v) do x, y
(x[1] < 0 ? nothing :
x[1] > y[1] ? 1 : 1.5,
0)
end
end
res = f(v)
@test res isa Matrix{Tuple{Real, Int}}
end
end