Skip to content

TCH auto-quoting is invalid for strings containing quotation marks  #13934

@dscorbett

Description

@dscorbett

The automated fixes for TCH001, TCH002, and TCH003 do not correctly handle string literals containing quotation marks. In the following example, running Ruff introduces an invalid type annotation, which mypy reports.

$ ruff --version
ruff 0.7.1

$ cat tch.py
from collections.abc import Sequence
from typing import Literal
def f(x: Sequence[Literal["'"]]) -> None:
    pass

$ mypy tch.py
Success: no issues found in 1 source file

$ ruff check --isolated --config 'lint.flake8-type-checking.quote-annotations = true' --select TCH tch.py --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ cat tch.py
from typing import Literal, TYPE_CHECKING

if TYPE_CHECKING:
    from collections.abc import Sequence
def f(x: "Sequence[Literal[''']]") -> None:
    pass

$ mypy tch.py
tch.py:5: error: Invalid type comment or annotation  [valid-type]
Found 1 error in 1 file (checked 1 source file)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions