Open jaap3 opened 1 week ago
In the context of lists, there's already a lint rule like this but it suggests the opposite: RUF005
I don't actually particularly care about the exact form of how dictionaries are merged, it's just that I'd like it to be consistent across the codebase. I picked the union operator |
because it's a recent language addition and resulted in the lowest line count in my case.
Just did a quick (unscientific) benchmark and it appears unpacking is currently more performant:
python3.13 -m timeit -s "a = {'a': 1};b = {'b': 2};c = {'a': 0}" "m = a | b | c"
2000000 loops, best of 5: 118 nsec per loop
python3.13 -m timeit -s "a = {'a': 1};b = {'b': 2};c = {'a': 0}" "m = {**a, **b, **c}"
5000000 loops, best of 5: 86.3 nsec per loop
Opinions may vary on the readability of the latter, but I'm fine with either.
Ran into an interesting variation that combined both unpacking and the union operator:
def get_form_kwargs(self):
return {**super().get_form_kwargs()} | {
"instance": self.request.user,
"claims": self.token_data["claims"],
}
Adding it here as a reminder to also detect this pattern if a rule is implemented.
I like this idea, but I'd prefer consistency with RUF005 and have dict unpacking.
I just made a PR in one of our private projects replacing patterns like:
and
with
Then I thought to myself, it would be nice if Ruff could just autofix this.
I looked into the rules, and open issues and didn't find an existing rule covering this.
I did come across https://github.com/astral-sh/ruff/issues/13533#issuecomment-2394953512 which I think is suggesting (nearly) the same thing.