Backslashes become forward slashes in literal types in tests
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
This works:
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.
This can create the further odd situation:
[case testLiteralWithBackslash2]
from typing import Literal
x: Literal[" \ "] = " // "
Expected:
Actual:
main:2: error: Incompatible types in assignment (expression has type "Literal[' // ']", variable has type "Literal[' // ']") (diff)
(if you expect this error verbatim, then at least the test passes!)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the reported uv run mypy -c command and the testLiteralWithBackslash and testLiteralWithBackslash2 cases. The report names no source file, so trace how literal-type test cases parse and render expected errors. Done means backslashes and escape sequences remain unchanged in test output and the reproductions pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100