python / python/mypy

MyPy reports errors when performing nested element access using a ``TypeVar``-parameterized generic type

Offen
#16,953 0 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

Bug Report

Type variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in a nested manner:

To Reproduce

import typing

T = typing.TypeVar("T")

class Container(typing.Generic[T]):
    def __init__(self, value : T) -> None:
        self.value = value

class A(Container[int]):
    pass

class B(Container[A]):
    pass

def get(x: Container[T]) -> T:
    return x.value


a = B(A(5))
reveal_type(a)

b = get(a)
reveal_type(b)

c1 = get(b)
reveal_type(c1)

# This is expected to match 'c1'
c2 = get(get(a))
reveal_type(c2)

MyPy-Play link: https://gist.github.com/mypy-play/40d62fe7a0fc28bf68bd9d156ba77c5b

a.py:20: note: Revealed type is "a.B"
a.py:23: note: Revealed type is "a.A"
a.py:26: note: Revealed type is "builtins.int"
a.py:30: note: Revealed type is "builtins.int"

Reference (PyRight):

/Users/wjakob/drjit-nanobind/a.py
  /Users/wjakob/drjit-nanobind/a.py:20:13 - information: Type of "a" is "B"
  /Users/wjakob/drjit-nanobind/a.py:23:13 - information: Type of "b" is "A"
  /Users/wjakob/drjit-nanobind/a.py:26:13 - information: Type of "c1" is "int"
  /Users/wjakob/drjit-nanobind/a.py:30:13 - information: Type of "c2" is "int"

Actual Behavior

a.py:20: note: Revealed type is "a.B"
a.py:23: note: Revealed type is "a.A"
a.py:26: note: Revealed type is "builtins.int"
a.py:29: error: Argument 1 to "get" has incompatible type "B"; expected "Container[Container[int]]"  [arg-type]
a.py:30: note: Revealed type is "builtins.int"
Found 1 error in 1 file (checked 1 source file)

Your Environment
Python 3.12.2, MyPy 1.8.0

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 der a.py-Reproduktion aus dem Issue unter Verwendung von Python 3.12.2 und MyPy 1.8.0 und vergleiche sie mit dem verknüpften MyPy-Play-Beispiel. Untersuche die verschachtelte TypeVar-Inferenz für get(get(a)). Als erledigt gilt die Aufgabe, wenn der verschachtelte Aufruf keinen incompatible-argument-Fehler erzeugt und c2 als builtins.int aufgelöst wird, sodass c1 entspricht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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