-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
interpreter: Add values() method for dict #14432
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
base: master
Are you sure you want to change the base?
Conversation
c22cdeb
to
cbd0dc9
Compare
Updated to use list comprehension instead of |
595d9c1
to
5543b6d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all looks good to me, apart from the 1.7.2 -> 1.8.0 thing.
The question I have is, do we want to keep the sorting behavior. We have the following:
- the implementation of .keys() sorts
- we have no tests using .keys()
- we do not document anywhere that .keys() is sorted
I find the sorting behavior somewhat unexpected, and we don't document it or test it, but it may break something to change it. I also will need to do more git archeology to see where it came from
It came from #7900 (commit 4caa057), where changes to the iteration order caused changes in command lines. This in turn triggered unnecessary full rebuilds. Anyhow, returning in values in matching order is a good idea. |
@bonzini This should no longer be an issue given we guarantee insertion order and even document we do that. So ideally we should follow that for keys() and values() too, IMHO. Else it is quite confusing when iterating gives you a different order than these methods. Maybe we should add a sorted kwarg to keys and values that defaults to false but can be "asc" or "desc"? |
That's fine for me, I'd even do only a boolean (false = insertion/true = ascending). |
That original commit was made days before meson bumped the minimum python to 3.6, which had dictionaries that preserve order as an implementation detail -- and python 3.7 made that part of the language spec. So at the time, sorting dictionaries could make a difference, but was effectively immediately made obsolete. Sorting is no longer required in order to have the intended effect of stable build.ninja file contents. The other commits in that old PR are still relevant either way, since sets definitely do not have any order regardless of python version. |
Given all the explanations, I'm not a bit concerned about removing the sorting by default. I'd be fine if we wanted to add an |
@dcbaker Not sure I understand your message? You say you are not concerned about removing the sorting but then you go on to say:
which sounds like directly contradicting what you just said. Can you clarify what you mean? Also regarding internal fixups violating insertion order, we already guarantee and document dictionaries to maintain insertion order. |
https://mesonbuild.com/Reference-manual_elementary_dict.html#dict-dict
This is funny since https://mesonbuild.com/Syntax.html#dictionaries still says:
Anyway, I agree that we should have the same (documented in the RefMan) insertion order both using foreach and using |
I guess as long as dictionaries are immutable it doesn't really matter (which they are), although I'd like to have options akin to the array one likes |
@ePirat: At this point if we can fix the versions then I'm happy with this and would be fine to merge. I'd rather handle questions of different sort orders separately from values anyway |
@dcbaker Ok, I will fix the versions then and we can do the discussion about sorting or not sorting in a follow up PR |
Analogous to keys(), this returns the values in an array. It uses the same sorting as keys(), else it would quite confusing to return values in a different order than the corresponding keys.
5543b6d
to
2f6fdfb
Compare
This makes it possible to retrieve a list of values of a dictionary:
Note that just like
keys()
returns the keys sorted, so willvalues()
return them sorted by the corresponding keys to match between the two. I've updated the docs to clarify this. It is a bit unfortunate but I don't think it is ok to break behavior and makekeys()
not sort...