python / python/mypy

mypy loses information about types. 'str | tuple[str, ...]' becomes 'Sequence[str]'

Aperta
#19,211 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

mypy loses information about types. 'str | tuple[str, ...]' becomes 'Sequence[str]'

To Reproduce

version 1: this fails

from typing import TypeVar

T = TypeVar('T')

def unwrap(x: T | None) -> T:

  assert x is not None

  return x

class Qux:

    def foo(self) -> str | tuple[str, ...]:

        baz = unwrap(self.bar())

        return baz

    def bar(self) -> str | tuple[str, ...] | None:

        return 'quux'

version 2: this works

class Qux:

    def foo(self) -> str | tuple[str, ...]:

        baz = self.bar()

        assert baz is not None

        return baz

    def bar(self) -> str | tuple[str, ...] | None:

        return 'quux'

Expected Behavior

both versions of code should run successfully

Actual Behavior

version 2 fails with the following error: 'Incompatible return value type (got "Sequence[str]", expected "str | tuple[str, ...]") [return-value]'

Your Environment

mypy version: 1.15.0
python version: 3.12

reproducible even the mypy playground

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 con le due riproduzioni autonome nell’issue ed eseguilе nel mypy playground collegato usando mypy 1.15.0 e Python 3.12. Confronta il tipo inferito restituito tramite unwrap con il caso di narrowing diretto; il lavoro è completato quando entrambe le versioni superano il controllo dei tipi senza l’errore relativo a un valore di ritorno incompatibile, mantenendo l’unione dichiarata.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
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.