-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[flake8-type-checking
] Skip quoting annotation if it becomes invalid syntax (TCH001
)
#14285
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
[flake8-type-checking
] Skip quoting annotation if it becomes invalid syntax (TCH001
)
#14285
Conversation
|
Hi @dhruvmanila I spent some time on finding the issue and the fix and wrote it down below. I'm not sure if it's okay to skip fixing these tricky cases |
Hey, thanks for looking into this and opening this PR. I think the solution for now would be to avoid providing a fix if the annotation contains the preferred quote (the one used in to surround the annotation in the fix) or if it contains any escape characters like a newline character. Although Pyright supports them, the initial implementation for string annotations in red knot will reject them (#14151). There's also this discussion about the different string kinds here. I think that's what this PR is doing right now based on looking at the snapshot changes, right? |
Yes @dhruvmanila this will skip the fix for those scenarios. sorry the point was not clear in the description. |
9b49d98
to
c3428cf
Compare
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/quote3.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this.
flake8-type-checking
] Skip quoting annotation if it becomes invalid syntax (TCH001
)
(I updated the PR to get it into the next release.) |
Fix: #13934
Summary
Current implementation has a bug when the current annotation contains a string with single and double quotes.
TL;DR: I think these cases happen less than other use cases of Literal. So instead of fixing them we skip the fix in those cases.
One of the problematic cases:
The outcome is:
While it should be:
The solution in this case is that we check if there’s any quotes same as the quote style we want to use for this Literal parameter then escape that same quote used in the string.
Also this case is not uncommon to have: https://grep.app/search?current=2&q=Literal["'
But this can get more complicated for example in case of:
Here we escaped the inner quote but in the generated annotation it gets removed. Then we flip the quote style of the Literal paramter and the formatting is wrong.
In this case the solution is more complicated.
’
without\
before them. This can get more complicated since there can be multiple backslashes so checking for only\’
won’t be enough.Another problem is when the string contains
\n
. In case ofType[Literal["\n"]]
we generate'Type[Literal["\n"]]'
and both pyright and mypy reject this annotation.https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2AUAMaXkDOjUAKoiQNqsC6AXFAB0w6tQAmJYLBKMYAfQCOAVzCk5tMChjlUjOQCNytANaMGjABYAKRiUrAANLA4BGAQHJ2CLkVIVKnABEADoogTw87gCUfNRQ8VAITIyiElKksooqahpaOih6hiZmTNa29k7w3m5sHJy%2BZFRBoeE8MXEJScxAA
Test Plan
I added test cases for the original code in the reported issue and two more cases for backslash and new line.