python / python/mypy

Cannot annotate child of a `Generic[AnyStr]` with an `Optional[AnyStr]` attribute

Offen
#12,984 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Consider the following code:

from typing import AnyStr, Generic, Optional

class Base(Generic[AnyStr]):
    def __init__(self, data: AnyStr) -> None:
        self.data: AnyStr = data

class Child(Base):
    def __init__(self, data: AnyStr) -> None:
        super().__init__(data)
        self.buffer: Optional[AnyStr] = None

    def act(self, s: AnyStr) -> None:
        self.buffer = s

Running mypy on this code produces the following errors:

anystr01.py:13: error: Incompatible types in assignment (expression has type
"str", variable has type "Optional[AnyStr]")
            self.buffer = s
                          ^
anystr01.py:13: error: Incompatible types in assignment (expression has type
"bytes", variable has type "Optional[AnyStr]")
            self.buffer = s
                          ^
Found 2 errors in 1 file (checked 1 source file)

If I try to address the problem by changing the child class declaration to class Child(Base[AnyStr]):, mypy instead emits the following:

anystr01.py:9: error: Argument 1 to "__init__" of "Base" has incompatible type
"str"; expected "AnyStr"
            super().__init__(data)
                             ^
anystr01.py:9: error: Argument 1 to "__init__" of "Base" has incompatible type
"bytes"; expected "AnyStr"
            super().__init__(data)
                             ^
Found 2 errors in 1 file (checked 1 source file)

Is this a bug, or am I simply doing something wrong?

Your Environment

  • Mypy version used: both 0.961 and commit 132b8e527730b654a3f3d04684bebc9daff6cabd
  • Mypy command-line flags: --pretty
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.13
  • Operating system and version: macOS 11.6.6

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Verwende das bereitgestellte Python-Snippet als Reproduktionsfall und führe mypy mit den gemeldeten Flags aus. Verfolge die Inferenz von Generics und die Zuweisungsprüfung für Child, Base[AnyStr] und Optional[AnyStr]; abgeschlossen ist die Aufgabe, wenn das Verhalten korrigiert oder eindeutig dokumentiert ist und eine Regressionstestabdeckung für den Fall vorhanden ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.