python / python/mypy

Unable to make generic subclass using type variable with value restriction

Offen
#10,130 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

Mypy reports some strange false positive errors when I try to define a generic subclass of a generic class where the type variable has a value restriction.

To Reproduce

I made this minimal example to reproduce the bug:

from typing import Generic, TypeVar

T = TypeVar("T", str, int)


class GenericSuperClass(Generic[T]):
    def __init__(self, value: T) -> None:
        # mypy complains if self.value is not annotated, for some reason
        self.value: T = value


class GenericSubClass(GenericSuperClass[T]):
    def __init__(self, value: T, flag: bool) -> None:
        super().__init__(value)
        self.flag = flag


instance = GenericSubClass("string", True)
reveal_type(instance.value)

This example is a bit simple, of course, and does not make use of the value restriction. But you could imagine that there is some logic here that assumes T is either a str or an int.

Expected Behavior

I would expect the above example to not report any errors with Mypy, and I would expect Mypy to infer that self.value is of type T.

I expect the revealed type to be builtins.str.

Actual Behavior

Mypy reports the following errors:

testing_inheriting_generics.py:14: error: Argument 1 to "__init__" of "GenericSuperClass" has incompatible type "str"; expected "T"
testing_inheriting_generics.py:14: error: Argument 1 to "__init__" of "GenericSuperClass" has incompatible type "int"; expected "T"
testing_inheriting_generics.py:19: note: Revealed type is 'builtins.str*'
Found 2 errors in 1 file (checked 1 source file)

Additionally, if self.value is not annotated, Mypy reports the error testing_inheriting_generics.py:9: error: Need type annotation for 'value'

The revealed type is correct, but I believe the errors are false positives?

If I replace T = TypeVar("T", str, int) with T = TypeVar("T"), everything works as expected, but then I would not be able to assume that T is either a str or an int.

Your Environment

  • Mypy version used: mypy 0.812
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
    [mypy]
    disallow_untyped_calls = True
    warn_redundant_casts = True
    warn_return_any = True
    warn_unused_configs = True
    warn_unused_ignores = True
    
    [mypy-responses.*]
    ignore_missing_imports = True
    
  • Python version used: Python 3.9.1
  • Operating system and version: macOS Catalina 10.15.7

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

Beginne mit dem minimalen Reproducer in testing_inheriting_generics.py und führe mypy mit dem angegebenen Python-3.9-Setup aus. Verfolge die Prüfung des wertbeschränkten TypeVar durch GenericSubClass und GenericSuperClass. Die Aufgabe ist abgeschlossen, wenn die falsch-positiven Konstruktorfehler und der Fehler wegen der unnötigen Attributannotation verschwunden sind und reveal_type builtins.str meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

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