python / python/mypy

Nondeterministic type-var error with LiteralString-bounded TypeVars

Aperta
#20,528 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

After upgrading from mypy 1.19.0 to 1.19.1, we're experiencing nondeterministic type checking behavior:

  1. Without type: ignore[type-var]: Sometimes reports "Value of type variable 'S' of 'Response' cannot be 'object'" [type-var]
  2. With type: ignore[type-var]: Sometimes reports "Unused 'type: ignore[type-var]' comment" [unused-ignore]

The error state is inconsistent and appears related to PR #20345 (which fixed issue #20344 about nondeterministic behavior with tuple type vars).

Environment

  • Mypy version: 1.19.1
  • Python version: 3.13
  • Upgraded from: mypy 1.19.0 (no errors)

Example

Granted, this is a simplified, sanitized example I have extracted from our code base but I believe it highlights the case where the issue is happening

from typing import LiteralString


class Container[S: LiteralString, T]:
    """A container with a LiteralString key."""

    def __init__(self, key: S, value: T) -> None:
        self.key = key
        self.value = value


class Response[S: LiteralString, T]:
    """A response parameterized by LiteralString."""

    def __init__(self, container: Container[S, T]) -> None:
        self.container = container


def process[S: LiteralString, S1: LiteralString](
    containers: list[Container[S, int] | Container[S1, str]],
) -> dict[int, Response]:
    """Create responses from a union of containers with different LiteralString keys."""
    return {
        i: Response(container)  # May error: type-var or unused-ignore
        for i, container in enumerate(containers)
    }
Scenario 1: With type: ignore[type-var] comment
example.py:28: error: Unused "type: ignore[type-var]" comment  [unused-ignore]
Scenario 2: Without the comment (in different contexts)

Sometimes passes with no errors, sometimes shows:

example.py:28: error: Value of type variable "S" of "Response" cannot be "object"  [type-var]

I'm not able to nail down exactly when each scenario surfaces but hopefully this is enough to go on as it otherwise presents as a regression since 1.19.0

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 eseguendo l'esempio fornito con mypy 1.19.0 e 1.19.1, sia con sia senza il commento type: ignore[type-var], e confronta le diagnosi. Leggi PR #20345 e issue #20344 per il comportamento non deterministico correlato dei type-var delle tuple; il lavoro è concluso quando il caso di TypeVar vincolato da LiteralString produce un risultato deterministico e corretto.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.