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

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

Open
stephenfin opened this issue Jul 31, 2024 · 1 comment
Open

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

stephenfin opened this issue Jul 31, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@stephenfin
Copy link

stephenfin commented Jul 31, 2024

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 #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 😄

@stephenfin stephenfin changed the title pyupgrade (U) fixes do not include dict/set literal fixes pyupgrade (UP) fixes do not include dict/set literal fixes Jul 31, 2024
@charliermarsh
Copy link
Member

👍 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 🤔

@charliermarsh charliermarsh added the documentation Improvements or additions to documentation label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants