-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-type-checking
] Skip quoting annotation if it becomes invali…
…d syntax (`TCH001`) (#14285) 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: ``` from typing import Literal from third_party import Type def error(self, type1: Type[Literal["'"]]): pass ``` The outcome is: ``` - def error(self, type1: Type[Literal["'"]]): + def error(self, type1: "Type[Literal[''']]"): ``` While it should be: ``` "Type[Literal['\'']" ``` 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: ``` - def error(self, type1: Type[Literal["\'"]]): + def error(self, type1: "Type[Literal[''']]"): ``` 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. 1. When generating the string of the source code preserve the backslash. 2. After we have the annotation check if there isn’t any escaped quote of the same type we want to use for the Literal parameter. In this case check if we have any `’` 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 of `Type[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. --------- Co-authored-by: Dhruv Manilawala <[email protected]>
- Loading branch information
1 parent
1f82731
commit 6591775
Showing
5 changed files
with
72 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...r__rules__flake8_type_checking__tests__quote_typing-only-third-party-import_quote.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...__rules__flake8_type_checking__tests__quote_typing-only-third-party-import_quote2.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters