-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
UP035 autofix breaks code when a typing
-module alias is used in a PEP-604 union in combination with a string
#15937
Comments
I'm marking this issue as a bug since it's true that the autofix changes behaviour here, and ideally we should fix that. FWIW, though, I'd recommend always quoting the entire union if you're using forward references in a PEP-604 union, e.g. from collections.abc import Iterable
a = 'Iterable[ciao] | ciao' You can get unpredictable results (like this) otherwise. This is the official advice given at https://docs.python.org/3/library/stdtypes.html#types-union |
The original code is wrong as well, see However, since you're using an implicit type alias, ruff can't know that using |
I'd say that the original code is "ill-advised" @Daverball, but it's true that it does work at runtime :-) |
You're right. I didn't know the aliases in the typing module had some special magic to convert strings to |
Yeah... and it's arguably somewhat regrettable that they do, since it leads to highly confusing situationsn like this :/ but it can't realistically be reversed now |
Ah, I was not aware that even my code was 'ill-advised' wrong! However my point stands... It might be a good idea to suggest turning on TC010 with UP035 and some other UP rules as well? |
(Yeah, I acknowledge that the 'right' and 'wrong' adjectives I used in the original post was a bit strong. Sorry, I was just trying to open the issue quickly because I had some other things to attend to) |
Yes, we agree that this is a bug in Ruff that should be fixed :-) The autofix for this rule isn't meant to change the behaviour of your code!
Hmm, not sure -- this is the first time I've seen this come up, so I don't think many people run into this issue. |
The easiest way to address this would probably be to mark the fix as unsafe if the parent node to any of the references is a union Eventually if #15222 or #15635 gets merged, we could reuse that fix to extend the quotes and make the fix safe again if it doesn't get rid of any comments. |
typing
-module alias is used in a PEP-604 union in combination with a string
Description
The following code is correct
However it gets tagged as wrong by UP35 and automatically fixed to
And the fixed code is wrong.
The text was updated successfully, but these errors were encountered: