[flake8-type-checking] Mark TC006 fix unsafe when it adds an escape#25792
Open
eyupcanakman wants to merge 1 commit into
Open
[flake8-type-checking] Mark TC006 fix unsafe when it adds an escape#25792eyupcanakman wants to merge 1 commit into
eyupcanakman wants to merge 1 commit into
Conversation
The TC006 autofix quotes the cast type expression. When a string inside it uses the same quote that wraps the result, the quoted output gains a backslash escape that type checkers reject inside a forward reference. Mark the fix unsafe when the quoted output would contain an escape so it is no longer auto-applied. Fixes astral-sh#22131.
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #22131.
The
TC006fix quotes the type expression intyping.cast(). When a string inside the expression uses the same quote character that wraps the result, the quoted output gains a backslash escape, for examplecast(Literal["'"], "'")becomescast("Literal[\"'\"]", "'"). ty rejects an escape sequence inside a forward reference, so the auto-applied fix turns valid code into code that fails type checking.A single-character quote can't always avoid the escape, so the fix is now marked unsafe whenever the quoted output would contain one. Expressions that quote cleanly keep the safe fix. Producing an escape-free quoted form would need triple-quoted output, which is a larger change to the string generator and can follow separately.
Test Plan
Added two fixture cases to
TC006.py, one with an apostrophe and one with a double-quote value, and confirmed the snapshot marks both as unsafe fixes while the existing safe cases stay unchanged.