Skip to content

RFC: Change MultiDict constructors to more closely match the abstraction: accept _multiple pairs_ - #656

Closed
NHDaly wants to merge 8 commits into
JuliaCollections:MultiDict-rebootfrom
NHDaly:nhd-MultiDict-constructors
Closed

NHDaly wants to merge 8 commits into
JuliaCollections:MultiDict-rebootfrom
NHDaly:nhd-MultiDict-constructors

Conversation

@NHDaly

@NHDaly NHDaly commented Aug 19, 2020

Copy link
Copy Markdown
Contributor

From my understanding, the MultiDict is like a Dict, except that it can hold multiple pairs that have the same key. Much like bag-semantics in SQL.

And the current constructors do imply this as well:

julia> MultiDict(1=>1, 1=>2)
MultiDict{Int64,Int64}(Dict(1 => [1, 2]))

But before this PR, the constructors actually sometimes accept pairs of {K,V}, in some contexts, but they also sometimes accept pairs of {K,Vector{V}}, in other contexts. But neither would work in all contexts.

Here are some examples of the failures before this PR:

julia> MultiDict(1=>1, 1=>2)
MultiDict{Int64,Int64}(Dict(1 => [1, 2]))

julia> MultiDict(1=>[2])   # without type args, [2] is treated as an array value when it's a pair
MultiDict{Int64,Array{Int64,1}}(Dict(1 => [[2]]))

julia> MultiDict([1=>[2]])   # Same for arrays of pairs.
MultiDict{Int64,Array{Int64,1}}(Dict(1 => [[2]]))

julia> MultiDict([(1,[2])])   # but when it's an array of *tuples*, an array is treated as _all the possible values for the given key!_
MultiDict{Int64,Int64}(Dict(1 => [2]))

julia> MultiDict([(1,2), (1,3)])   # But it's not required.
MultiDict{Int64,Int64}(Dict(1 => [2, 3]))

julia> MultiDict{Int,Int}(1=>[2]) # But WITH type args, the second element IS required to be a vector of all values
MultiDict{Int64,Int64}(Dict(1 => [2]))

julia> MultiDict{Int,Int}(1=>2, 2=>3)  # Can't construct with pairs if you've included the types on the dict.
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,1}
Closest candidates are:
  convert(::Type{T}, ::AbstractArray) where T<:Array at array.jl:533
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/factorization.jl:55
  ...
Stacktrace:
 [1] setindex!(::Dict{Int64,Array{Int64,1}}, ::Int64, ::Int64) at ./dict.jl:380
 [2] Dict{Int64,Array{Int64,1}}(::Pair{Int64,Int64}) at ./dict.jl:107
 [3] MultiDict{Int64,Int64}(::Pair{Int64,Int64}) at /Users/nathandaly/.julia/packages/DataStructures/iWRBf/src/multi_dict.jl:12
 [4] top-level scope at REPL[17]:1

julia> xs = [1,1,2];

julia> MultiDict(x=>x+1 for x in xs)  # Also, in Generators, pairs are also required to point to vectors, **with or without types**
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Any,1}
Closest candidates are:
  convert(::Type{T}, ::AbstractArray) where T<:Array at array.jl:533
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/factorization.jl:55
  ...
Stacktrace:
 [1] setindex!(::Dict{Any,Array{Any,1}}, ::Int64, ::Int64) at ./dict.jl:380
 [2] Dict at ./dict.jl:103 [inlined]
 [3] MultiDict at /Users/nathandaly/.julia/packages/DataStructures/iWRBf/src/multi_dict.jl:12 [inlined]
 [4] multi_dict_with_eltype at /Users/nathandaly/.julia/packages/DataStructures/iWRBf/src/multi_dict.jl:28 [inlined]
 [5] MultiDict(::Base.Generator{Array{Int64,1},var"#19#20"}) at /Users/nathandaly/.julia/packages/DataStructures/iWRBf/src/multi_dict.jl:18
 [6] top-level scope at REPL[13]:1

julia> MultiDict{Int,Int}(x=>x+1 for x in xs)   # Also, in Generators, pairs are also required to point to vectors, **with or without types**
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,1}
Closest candidates are:
  convert(::Type{T}, ::AbstractArray) where T<:Array at array.jl:533
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/factorization.jl:55
  ...
Stacktrace:
 [1] setindex!(::Dict{Int64,Array{Int64,1}}, ::Int64, ::Int64) at ./dict.jl:380
 [2] Dict at ./dict.jl:103 [inlined]
 [3] MultiDict{Int64,Int64}(::Base.Generator{Array{Int64,1},var"#27#28"}) at /Users/nathandaly/.julia/packages/DataStructures/iWRBf/src/multi_dict.jl:12
 [4] top-level scope at REPL[31]:1

julia> MultiDict(x=>[x+1] for x in xs)  # These work, but they're also super weird because they're different than providing a single pair.
MultiDict{Any,Any}(Dict{Any,Array{Any,1}}(2 => [3],1 => [2]))

julia> MultiDict{Int,Int}(x=>[x+1] for x in xs)  # same here
MultiDict{Int64,Int64}(Dict(2 => [3],1 => [2]))

New Proposal

I tried fixing just some of those weird constructors, but the inconsistencies were too hard to grapple with. I think the new rules I'm proposing are quite logical, concise, easy to understand, and keep close to the core concept. But they will be a breaking change in the API. Is that okay?

Here's what I'm proposing and what this PR implements:

  1. MultiDict constructors expect pairs or tuples or generators of {K,V}:
    • MultiDict(1=>1,1=>2), MultiDict{Int,Int}(1=>2,2=>3), MultiDict{Int,Int}([(1,1), (1,2)]), etc
  2. To manually construct a MultiDict with {K,Vector{V}), you can construct with the underlying Dict directly.
    • MultiDict(Dict(1=>[1,2]), MultiDict{Int,Int}(Dict(1=>[2],2=>[3])), etc

The tests used the constructors that passed all values in a vector in a few places, so I've changed those to either enumerate the pairs, or to use the inner Dict constructor.


Does this seem reasonable? I think it makes the type much easier to use, more consistent, and more flexible. As a user, it took me a while before finding a constructor that actually did what I wanted, and so i think this will be helpful to others! :)

If we do go with this, should I bump the minor release number? (0.18 => 0.19)

Let me know what you think! Thanks! :)

NHDaly added 3 commits August 18, 2020 22:43
Before, constructors would accept _either_ (k=>v, k=>v...), or (k=>[v],
k=>[v]...). Each worked in some contexts, but neither in all contexts.
Also, before, generators did not work.

Now, there are two simple rules:
1. MultiDict constructors expect pairs or tuples or generators of {K,V}:
    - `MultiDict(1=>1,1=>2)`, `MultiDict{Int,Int}(1=>2,2=>3)`, etc
2. To manually construct a MultiDict with {K,Vector{V}), you can
   construct with the _underlying Dict_ directly.
    - `MultiDict(Dict(1=>[1,2])`, `MultiDict{Int,Int}(1=>[2],2=>[3])`, etc
- Fixes broken constructor by adding `grow_to!` overload. We need this
  because MultiDict isn't an AbstractDict.
- Adds `empty(::MultiDict, ::Type{K}, ::Type{V})`, which was needed in
  the process, and also makes us more standard! :raised-hands:
@NHDaly

NHDaly commented Aug 19, 2020

Copy link
Copy Markdown
Contributor Author

As an example, I left this comment here because i thought that the generator constructor was just broken:
#655 (comment)
I had no idea that they value was supposed to be a vector containing all values, considering that that's violating the core metaphor of this container.

@eulerkochy eulerkochy left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've reviewed the PR, and requested some stylistic changes. What you're proposing is a breaking change, so I would like to hear @oxinabox 's opinion about this.

Comment thread src/multi_dict.jl Resolved
Comment thread src/multi_dict.jl Resolved
Comment thread src/multi_dict.jl Resolved
Comment thread src/multi_dict.jl Resolved
@eulerkochy
eulerkochy requested a review from oxinabox August 19, 2020 11:23
NHDaly and others added 2 commits August 19, 2020 11:56
Apply @eulerkochy's formatting suggestions

Co-authored-by: Koustav Chowdhury <kc99.kol@gmail.com>
@NHDaly

NHDaly commented Aug 19, 2020

Copy link
Copy Markdown
Contributor Author

Also, some of the other MultiDict changes I've proposed: #654, #655 would also be breaking changes.. Maybe it makes sense to do them all at once, in one release?

@oxinabox

Copy link
Copy Markdown
Member

yes, we do want to batch up the breaking changes into a single release.
It is unfortunate that these PRs were not make 2 weeks ago, as we were making a breaking release at the time.
it would be annoying to make another breaking release in the next 2 months.

@NHDaly

NHDaly commented Aug 20, 2020 •

Copy link
Copy Markdown
Contributor Author

😭 oh no! Bummer. I'm fine leaving all of these on like one big "multi-dict" branch or something that we can wait to merge until you're comfortable with another breaking change? Does that work?

I guess, the kind of nice thing about this package is that probably most people are using it for only a few container types, so hopefully each breaking API change only affects some (hopefully different) people?

@oxinabox

Copy link
Copy Markdown
Member

It we can't workout a way to deprecate them safely then yes, putting it onto a branch would be best.
Then we can look at that branch and see if at the end its all worth making a breaking release over.
or if we want to hold-off

@NHDaly

NHDaly commented Aug 24, 2020

Copy link
Copy Markdown
Contributor Author

Okay, makes sense! If you can create a new branch (maybe MultiDict? Or MultiDict-changes or something?), I'll switch the PR base over to it!

@NHDaly

NHDaly commented Aug 24, 2020

Copy link
Copy Markdown
Contributor Author

Or I guess you're saying maybe we can still support the older constructors but deprecated?

Unfortunately I don't think so, without the behavior remaining ambiguous and confusing. For example, the fact that whether or not you add the type parameters changes the meaning of the pair arguments seems completely broken to me:

julia> MultiDict(1=>[2,3])  # Treated as a single pair
MultiDict{Int64,Array{Int64,1}}(Dict(1 => [[2, 3]]))

julia> MultiDict{Int,Int}(1=>[2,3])  # Treated as a normalized representation of all pairs
MultiDict{Int64,Int64}(Dict(1 => [2, 3]))

I think that in order to make the constructors behave consistently, we'll need to break the old behavior.

I'm very open to suggestions for how to do this change in a safe, backwards compatible, deprecation way though if you see such a potential path!! :) Thanks again for your time :)

Fixes constructors that must do type-promotion, e.g.:
```julia
julia> MultiDict(1 => 2, 1 => "hello")
MultiDict{Int64,Any}(Dict{Int64,Array{Any,1}}(1 => [2, "hello"]))
```
@nickrobinson251 nickrobinson251 added this to the 1.0 milestone Sep 4, 2020
@NHDaly
NHDaly changed the base branch from master to MultiDict-reboot October 24, 2020 04:09
@NHDaly
NHDaly force-pushed the nhd-MultiDict-constructors branch from 7799147 to 74fcbc5 Compare October 24, 2020 04:13
@NHDaly

NHDaly commented Oct 24, 2020

Copy link
Copy Markdown
Contributor Author

Thanks friends! Sorry it took me so long to get to this.

Now that i have the committer bit on this repo, i've created a new MultiDict-reboot branch, and adjusted this PR to point to it. i'm going to do that with all the MultiDict PRs that have breaking changes, so we can do them all together.

I still think it's nice to review and merge this as individual PRs though, to make reviewing easier. :) Anyone wanna do a final review and/or approval? ❤️

@fingolfin
fingolfin deleted the branch JuliaCollections:MultiDict-reboot March 17, 2025 09:44
@fingolfin fingolfin closed this Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants