astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.42k stars 1.08k forks source link

pyupgrade (`UP`) fixes do not include `dict`/`set` literal fixes #12592

Open stephenfin opened 3 months ago

stephenfin commented 3 months ago

I noticed that the pyupgrade fixes result in different output to pyupgrade itself. Namely, it doesn't apply replace the use of dict() and set() with the equivalent literals.

x = dict(a=1)
y = dict((a, b) for a, b in ['a', '1'])
a = set(['a', 'b'])
b = set([x for x in ['a', 'b']])

pyupgrade identifies and replaces these:

❯ pyupgrade --py38-plus test.py 
Rewriting test.py
❯ cat test.py 
x = dict(a=1)
y = {a: b for a, b in ['a', '1']}
a = {'a', 'b'}
b = {x for x in ['a', 'b']}

ruff does not (at least not using just the U fix class):

❯ ruff check --select U --fix --unsafe-fixes test.py 
All checks passed!

However, while I couldn't find anything confirming this (it's not mentioned in https://github.com/astral-sh/ruff/issues/827 and a search through other issues didn't highlight anything), it seems enabling another class of rules, the flake8-comprehensions (C4), resolves this.

❯ ruff check --select U,C4 --fix --unsafe-fixes test.py 
Found 7 errors (7 fixed, 0 remaining).
❯ cat test.py 
x = {'a': 1}
y = dict(['a', '1'])
a = {'a', 'b'}
b = {'a', 'b'}

I don't know if the documentation tooling allows this (https://docs.astral.sh/ruff/rules/ appears to be auto-generated) but it could be helpful to provide a pointer or note to this effect for someone looking to replace pyupgrade wholesale? Alternatively, maybe this issue is enough of a pointer and it can simply be closed straight off :smile:

charliermarsh commented 3 months ago

👍 Yeah I think we just rolled these out under the C4 category because the already existed there. I'm not sure where we could relay this information. We've talked about supporting rule aliases in the past, but it raises all kinds of tricky UX questions 🤔