Skip to content

[flake8-type-checking] Mark TC006 fix unsafe when it adds an escape#25792

Open
eyupcanakman wants to merge 1 commit into
astral-sh:mainfrom
eyupcanakman:fix/tc006-escape-22131
Open

[flake8-type-checking] Mark TC006 fix unsafe when it adds an escape#25792
eyupcanakman wants to merge 1 commit into
astral-sh:mainfrom
eyupcanakman:fix/tc006-escape-22131

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

Summary

Fixes #22131.

The TC006 fix quotes the type expression in typing.cast(). When a string inside the expression uses the same quote character that wraps the result, the quoted output gains a backslash escape, for example cast(Literal["'"], "'") becomes cast("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.

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.
@astral-sh-bot astral-sh-bot Bot requested a review from ntBre June 9, 2026 22:22
@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TC006 fix creates quoted type expressions with escape sequences

2 participants