Skip to content

TCH fixes remove quotation marks from Literal strings #14368

@dscorbett

Description

@dscorbett

The fixes for TCH001, TCH002, and TCH003 sometimes delete quotation marks from Literals when lint.flake8-type-checking.quote-annotations = true. This can make a program fail to type-check. I am not sure exactly what makes it delete quotation marks but it seems to only happen to literals within nested brackets.

$ ruff --version
ruff 0.7.4

$ cat tch003.py
from collections.abc import Sequence
from typing import Callable, Literal
def f(x: Sequence[Callable[[Literal["1"]], bool]]) -> bool:
    return False
def g(x: Literal["1"]) -> bool:
    return False
print(f([g]))

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

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

$ cat tch003.py
from typing import Callable, Literal, TYPE_CHECKING

if TYPE_CHECKING:
    from collections.abc import Sequence
def f(x: "Sequence[Callable[[Literal[1]], bool]]") -> bool:
    return False
def g(x: Literal["1"]) -> bool:
    return False
print(f([g]))

$ mypy tch003.py
tch003.py:9: error: List item 0 has incompatible type "Callable[[Literal['1']], bool]"; expected "Callable[[Literal[1]], bool]"  [list-item]
Found 1 error in 1 file (checked 1 source file)

With "x" instead of "1" in that example, "x" is rewritten to x and the final mypy error is:

$ mypy tch003.py
tch003.py:5: error: Name "x" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions