python / python/mypy

Backslashes become forward slashes in literal types in tests

Aperta
#20,884 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-tests
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il problema con il comando indicato uv run mypy -c e i casi testLiteralWithBackslash e testLiteralWithBackslash2. La segnalazione non indica alcun file sorgente, quindi traccia il modo in cui i casi di test dei tipi literal analizzano e visualizzano gli errori attesi. Il lavoro è completato quando i backslash e le sequenze di escape rimangono invariati nell'output dei test e le riproduzioni hanno esito positivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools, testing-qa
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.