Cleanup use of dicts, e.g. use of .keys()#5165
Conversation
efc69aa to
e3683a6
Compare
Using `x in dict.keys()` is discouraged and should be `x in dict` directly which is potentially more performant and easier to read/shorter. Accesses to keys and their values should use `dict.items` to avoid the lookup overhead.
e3683a6 to
f3d4804
Compare
| _log.info("Trying to derive target repository based on specified files...") | ||
|
|
||
| easyconfigs, files_to_delete, patch_files, py_files = [paths[key] for key in sorted(paths.keys())] | ||
| easyconfigs, files_to_delete, patch_files, py_files = [item[1] for item in sorted(paths.items())] |
There was a problem hiding this comment.
could this not be simplified to the following?
| easyconfigs, files_to_delete, patch_files, py_files = [item[1] for item in sorted(paths.items())] | |
| easyconfigs, files_to_delete, patch_files, py_files = sorted(paths.values()) |
There was a problem hiding this comment.
We need to sort by key so that we get the right order.
It looks like a dataclass or NamedTuple would be better than a dict where you know all existing keys.
There was a problem hiding this comment.
TBH: I had to read the code here and the caller again to understand this. Shall I add a commit changing this to a named Tuple? Even though it is a "public" method I'd doubt anyone would be using this outside of us.
There was a problem hiding this comment.
That bubbled up the callchain more than I expected: The origin of this dict is categorize_files_by_type
IMO still very much worth it: Makes it much easier to reason about what is actually passed instead of having a paths parameter in multiple places and sometimes it is a list of paths, and sometimes a dict with only some keys which might be misspelled.
If you'd like to review that change in isolation then use #5190 and I'll rebase this (will cause an easy conflict to resolve)
The current dict is pretty opaque and makes spelling mistakes easy. Avoid by using a typed, named tuple.
Using
x in dict.keys()is discouraged and should bex in dictdirectly which is potentially more performant and easier to read/shorter. Accesses to keys and their values should usedict.itemsto avoid the lookup overhead.