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

dict docs: document that ordering of keys/values/pairs match iterate #56842

Merged
merged 5 commits into from
Dec 18, 2024

Conversation

cossio
Copy link
Contributor

@cossio cossio commented Dec 15, 2024

Fix #56841.

Currently the documentation states that keys(dict) and values(dict) iterate in the same order. But it is not stated whether this is the same order as that used by pairs(dict), or when looping, for (k,v) in dict.

This PR makes this guarantee explicit.

Copy link
Member

@LilithHafner LilithHafner left a comment

Choose a reason for hiding this comment

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

In my interpretation, this guarantees that keys, values, and pairs iterate in the same order for all collections (not just Dicts).

I think this is a reasonable guarantee for this API.

@LilithHafner LilithHafner added docs This change adds or pertains to documentation collections Data structures holding multiple items, e.g. sets iteration Involves iteration or the iteration protocol labels Dec 15, 2024
@cossio
Copy link
Contributor Author

cossio commented Dec 16, 2024

I don't think the CI failure is related to this PR ?

test/dict.jl Outdated
@test [k for (k,v) = d] ==
[k for k = keys(d)] ==
[k for (k,v) = pairs(d)] ==
collect(keys(d))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do these need to be collected or is a check with any/all (consuming the iterators in the process) also good here? Would save on some allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

a check with any/all

How exactly?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should worry about optimizing allocations here; this code is run once with n=100 so it will be compile-time dominated anyway.

Copy link
Contributor

@Seelengrab Seelengrab Dec 16, 2024

Choose a reason for hiding this comment

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

How exactly?

Something like this:

@test all(zip(d, keys(d), pairs(d))) do (kv, k, kpair)
   first(kv) == k == first(kpair)
end

(might need some adjustment in the argument destructuring of the do block - not 100% sure this unpacks correctly)

I don't think we should worry about optimizing allocations here

I'm not worried about allocations, that would just be a sideeffect (of course, consuming fewer resources in CI is always nice to have). In my opinion, a all+zip version is just a bit cleaner and makes sure to only rely on iteration, and not incidental behavior of collect or other unrelated stuff :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Seelengrab That's a nice suggestion. I updated the tests.

@mbauman mbauman changed the title dict docs dict docs: document that ordering of keys/values/pairs match Dec 16, 2024
@mbauman mbauman changed the title dict docs: document that ordering of keys/values/pairs match dict docs: document that ordering of keys/values/pairs match iterate Dec 16, 2024
test/dict.jl Outdated
Comment on lines 792 to 796
foreach(zip(dict, keys(dict), values(dict), pairs(dict))) do (d, k, v, p)
@test d == p
@test first(d) == first(p) == k
@test last(d) == last(p) == v
end
Copy link
Contributor

@Seelengrab Seelengrab Dec 17, 2024

Choose a reason for hiding this comment

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

In case there's a bug that decouples keys/values/iterate/pairs, this will cause up to 300 individual failures, each showing up in the log. I think it'd be better for debugging to only have one failure through @test all(...), instead of three @test per key-value-pair in the dict.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better now?

@inkydragon inkydragon added the merge me PR is reviewed. Merge when all tests are passing label Dec 18, 2024
@giordano giordano merged commit 796d823 into JuliaLang:master Dec 18, 2024
8 checks passed
@giordano giordano added backport 1.10 Change should be backported to the 1.10 release backport 1.11 Change should be backported to release-1.11 and removed merge me PR is reviewed. Merge when all tests are passing labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.10 Change should be backported to the 1.10 release backport 1.11 Change should be backported to release-1.11 collections Data structures holding multiple items, e.g. sets docs This change adds or pertains to documentation iteration Involves iteration or the iteration protocol
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Iteration order of Dict
5 participants