You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Backslashes become forward slashes in literal types in tests #20884
uv run mypy -c "from typing import Literal; x: Literal[' \ '] = 1"
<string>:1: error: Incompatible types in assignment (expression has type "Literal[1]", variable has type "Literal[' \\ ']") [assignment]
Found 1 error in 1 file (checked 1 source file)
However, if you make this test case:
[case testLiteralWithBackslash]
-- This test case is actually more to test our mypy parsing than anything else.
from typing import Literal
x: Literal[" \ "] = "" # E: Incompatible types in assignment (expression has type "Literal['']", variable has type "Literal[' \\ ']")
you get this output:
Expected:
main:2: error: Incompatible types in assignment (expression has type "Literal['']", variable has type "Literal[' \ ']") (diff)
Actual:
main:2: error: Incompatible types in assignment (expression has type "Literal['']", variable has type "Literal[' // ']") (diff)
Alignment of first line difference:
E: ...le has type "Literal[' \ ']")
A: ...le has type "Literal[' // ']")
This also happens if you expect \\ instead of \. If you specify the variable is of Literal[" \\ "] type, then it becomes four forward slashes. If you do Literal[" \n "] then it becomes /n.
Can't reproduce. The second case suggests that the strings are being processed correctly inside mypy. This seems like an issue with how your terminal is rendering the output. Do you get the same behavior if you write the output to a file?
Yep, it's the same behavior if I write it to a file.
It's worth noting that I am on Windows, so maybe there's some over-eager "if windows, reverse all the slashes in the output" logic somewhere in the test suite.
Activity
brianschubert commentedon Feb 24, 2026
Can't reproduce. The second case suggests that the strings are being processed correctly inside mypy. This seems like an issue with how your terminal is rendering the output. Do you get the same behavior if you write the output to a file?
wyattscarpenter commentedon Feb 24, 2026
Yep, it's the same behavior if I write it to a file.
It's worth noting that I am on Windows, so maybe there's some over-eager "if windows, reverse all the slashes in the output" logic somewhere in the test suite.
brianschubert commentedon Feb 24, 2026
Oh, did you put the case in a
fine-grained*.testfile? Those do path normalization like what you describemypy/mypy/test/testfinegrained.py
Lines 130 to 131 in e7e5a09
wyattscarpenter commentedon Feb 24, 2026
I put it in check-literal.test. I've been able to reproduce it in this PR: https://github.com/python/mypy/actions/runs/22335061222/job/64625644063?pr=20885
Possibly this is something to do with some related logic in
mypy/test/data.py?