Skip to content

Backslashes become forward slashes in literal types in tests #20884

Open

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

Activity

  1. brianschubert commented on Feb 24, 2026

    @brianschubert
    Collaborator

    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?

  2. added
    pendingIssues that may be closed
    and removed
    pendingIssues that may be closed
    on Feb 24, 2026
  3. wyattscarpenter commented on Feb 24, 2026

    @wyattscarpenter
    ContributorAuthor

    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.

  4. brianschubert commented on Feb 24, 2026

    @brianschubert
    Collaborator

    Oh, did you put the case in a fine-grained*.test file? Those do path normalization like what you describe

    # Normalize paths in test output (for Windows).
    a = [line.replace("\\", "/") for line in a]

  5. wyattscarpenter commented on Feb 24, 2026

    @wyattscarpenter
    ContributorAuthor

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions