Skip to content

Commit

Permalink
deprecate getproperty(::Pairs, s) (#39448)
Browse files Browse the repository at this point in the history
Co-authored-by: Jameson Nash <[email protected]>
  • Loading branch information
simeonschaub and vtjnash authored Apr 27, 2021
1 parent 75c4f55 commit ff001a4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
12 changes: 6 additions & 6 deletions base/accumulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ function accumulate(op, A; dims::Union{Nothing,Integer}=nothing, kw...)
# This branch takes care of the cases not handled by `_accumulate!`.
return collect(Iterators.accumulate(op, A; kw...))
end
nt = kw.data
if nt isa NamedTuple{()}
nt = values(kw)
if isempty(kw)
out = similar(A, promote_op(op, eltype(A), eltype(A)))
elseif nt isa NamedTuple{(:init,)}
elseif keys(nt) === (:init,)
out = similar(A, promote_op(op, typeof(nt.init), eltype(A)))
else
throw(ArgumentError("acccumulate does not support the keyword arguments $(setdiff(keys(nt), (:init,)))"))
Expand Down Expand Up @@ -356,10 +356,10 @@ julia> B
```
"""
function accumulate!(op, B, A; dims::Union{Integer, Nothing} = nothing, kw...)
nt = kw.data
if nt isa NamedTuple{()}
nt = values(kw)
if isempty(kw)
_accumulate!(op, B, A, dims, nothing)
elseif nt isa NamedTuple{(:init,)}
elseif keys(kw) === (:init,)
_accumulate!(op, B, A, dims, Some(nt.init))
else
throw(ArgumentError("acccumulate! does not support the keyword arguments $(setdiff(keys(nt), (:init,)))"))
Expand Down
10 changes: 10 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,13 @@ end
cat_shape(dims, shape::Tuple{}) = () # make sure `cat_shape(dims, ())` do not recursively calls itself

# END 1.6 deprecations

# BEGIN 1.7 deprecations

# the plan is to eventually overload getproperty to access entries of the dict
@noinline function getproperty(x::Pairs, s::Symbol)
depwarn("use values(kwargs) and keys(kwargs) instead of kwargs.data and kwargs.itr", :getproperty, force=true)
return getfield(x, s)
end

# END 1.7 deprecations
28 changes: 14 additions & 14 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,30 @@ pairs(A::AbstractArray) = pairs(IndexCartesian(), A)
pairs(A::AbstractVector) = pairs(IndexLinear(), A)
# pairs(v::Pairs) = v # listed for reference, but already defined from being an AbstractDict

length(v::Pairs) = length(v.itr)
axes(v::Pairs) = axes(v.itr)
size(v::Pairs) = size(v.itr)
length(v::Pairs) = length(getfield(v, :itr))
axes(v::Pairs) = axes(getfield(v, :itr))
size(v::Pairs) = size(getfield(v, :itr))
@propagate_inbounds function iterate(v::Pairs{K, V}, state...) where {K, V}
x = iterate(v.itr, state...)
x = iterate(getfield(v, :itr), state...)
x === nothing && return x
indx, n = x
item = v.data[indx]
item = getfield(v, :data)[indx]
return (Pair{K, V}(indx, item), n)
end
@inline isdone(v::Pairs, state...) = isdone(v.itr, state...)
@inline isdone(v::Pairs, state...) = isdone(getfield(v, :itr), state...)

IteratorSize(::Type{<:Pairs{<:Any, <:Any, I}}) where {I} = IteratorSize(I)
IteratorSize(::Type{<:Pairs{<:Any, <:Any, <:Base.AbstractUnitRange, <:Tuple}}) = HasLength()

reverse(v::Pairs) = Pairs(v.data, reverse(v.itr))
reverse(v::Pairs) = Pairs(getfield(v, :data), reverse(getfield(v, :itr)))

haskey(v::Pairs, key) = (key in v.itr)
keys(v::Pairs) = v.itr
values(v::Pairs) = v.data # TODO: this should be a view of data subset by itr
getindex(v::Pairs, key) = v.data[key]
setindex!(v::Pairs, value, key) = (v.data[key] = value; v)
get(v::Pairs, key, default) = get(v.data, key, default)
get(f::Base.Callable, v::Pairs, key) = get(f, v.data, key)
haskey(v::Pairs, key) = (key in getfield(v, :itr))
keys(v::Pairs) = getfield(v, :itr)
values(v::Pairs) = getfield(v, :data) # TODO: this should be a view of data subset by itr
getindex(v::Pairs, key) = getfield(v, :data)[key]
setindex!(v::Pairs, value, key) = (getfield(v, :data)[key] = value; v)
get(v::Pairs, key, default) = get(getfield(v, :data), key, default)
get(f::Base.Callable, v::Pairs, key) = get(f, getfield(v, :data), key)

# zip

Expand Down
2 changes: 1 addition & 1 deletion base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ merge(a::NamedTuple, b::NamedTuple{()}) = a
merge(a::NamedTuple{()}, b::NamedTuple{()}) = a
merge(a::NamedTuple{()}, b::NamedTuple) = b

merge(a::NamedTuple, b::Iterators.Pairs{<:Any,<:Any,<:Any,<:NamedTuple}) = merge(a, b.data)
merge(a::NamedTuple, b::Iterators.Pairs{<:Any,<:Any,<:Any,<:NamedTuple}) = merge(a, getfield(b, :data))

merge(a::NamedTuple, b::Iterators.Zip{<:Tuple{Any,Any}}) = merge(a, NamedTuple{Tuple(b.is[1])}(b.is[2]))

Expand Down
2 changes: 1 addition & 1 deletion base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ julia> sum(1:20; init = 0.0)
"""
sum(a; kw...) = sum(identity, a; kw...)
sum(a::AbstractArray{Bool}; kw...) =
kw.data === NamedTuple() ? count(a) : reduce(add_sum, a; kw...)
isempty(kw) ? count(a) : reduce(add_sum, a; kw...)

## prod
"""
Expand Down
2 changes: 1 addition & 1 deletion stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Base: typesof, insert!

separate_kwargs(args...; kwargs...) = (args, kwargs.data)
separate_kwargs(args...; kwargs...) = (args, values(kwargs))

"""
Transform a dot expression into one where each argument has been replaced by a
Expand Down
2 changes: 1 addition & 1 deletion test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ end
@test_throws UndefVarError eval(:(1+$(Symbol(""))))

# issue #31404
f31404(a, b; kws...) = (a, b, kws.data)
f31404(a, b; kws...) = (a, b, values(kws))
@test f31404(+, (Type{T} where T,); optimize=false) === (+, (Type,), (optimize=false,))

# issue #28992
Expand Down

0 comments on commit ff001a4

Please sign in to comment.