Skip to content

Backslashes become forward slashes in literal types in tests #20884

@wyattscarpenter

Description

@wyattscarpenter

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!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions