Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dscorbett opened this issue Oct 25, 2024 · 5 comments · Fixed by #14285
Closed

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

dscorbett opened this issue Oct 25, 2024 · 5 comments · Fixed by #14285
Labels
bug Something isn't working help wanted Contributions especially welcome

Comments

@dscorbett
Copy link

dscorbett commented Oct 25, 2024

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)
@MichaReiser MichaReiser added bug Something isn't working help wanted Contributions especially welcome labels Oct 26, 2024
@MichaReiser
Copy link
Member

As with other string related fixes, we could consider disabling the fix if the string contains nested quotes. Ideally we would be somewhat clever about it to only disable it when running the fix creates invalid syntax because the use of Literal["Test"] is common

@dhruvmanila
Copy link
Member

dhruvmanila commented Oct 28, 2024

Ideally we would be somewhat clever about it to only disable it when running the fix creates invalid syntax because the use of Literal["Test"] is common

I'm not sure what do you mean here because we do fix this correctly by flipping the quotes but we don't consider nested quotes inside another existing string literal (https://play.ruff.rs/fc94efc6-2ccf-4b20-b053-f810a7d06c2f). I think we should avoid generating the fix for this case.

cc @Glyphack if you're interested

@MichaReiser
Copy link
Member

I'm not sure what do you mean here because we do fix this correctly by flipping the quotes but we don't consider nested quotes inside another existing string literal (play.ruff.rs/fc94efc6-2ccf-4b20-b053-f810a7d06c2f).

That's what I mean. We can't just naively test if the annotation contains any quotes. Instead, we have to search for nested quotes.

@Glyphack
Copy link
Contributor

Glyphack commented Oct 28, 2024

Thanks I like to spend some time on this. I think the simplest fix would be to check if the StringLiteral contains any quotes inside and don't apply the fix or check if the inner quotes will be a problem and then don't apply the fix.

@dscorbett
Copy link
Author

Other characters to beware of are backslashes and newlines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants