python / python/mypy

Lazy circular import with bottom import cannot determine type if the module name is alphabetically after the base class module

Offen
#18,734 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

To Reproduce

mkdir mypyissue
cd mypyissue
touch __init__.py a.py b.py c.py circular.py py.typed

a.py:

import typing_extensions as t
from .b import Base
class A(Base):
    def run(self) -> None:
        t.reveal_type(self._v)

b.py:

import typing as t
class Base:
    def __init__(self, v: t.Optional[str] = None) -> None:
        from .circular import circular
        self._v: t.Final = v or circular.v
from .a import A
from .c import C

c.py:

import typing_extensions as t
from .b import Base
class C(Base):
    def run(self) -> None:
        t.reveal_type(self._v)

circular.py

from .b import Base
class Circular:
    @property
    def v(self) -> str:
        return "value"
circular = Circular()

Expected Behavior

self._v to have the correct type for all children. Or at least consistently fail rather than now where somehow the module name matters.

Actual Behavior

c.py:5: error: Cannot determine type of "_v"  [has-type]
c.py:5: note: Revealed type is "Any"
a.py:5: note: Revealed type is "builtins.str"
Found 1 error in 1 file (checked 5 source files)

Notice that a.py has the correct type but not c.py.

Your Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Mypy command-line flags: Nothing
  • Mypy configuration options from mypy.ini (and other config files): Nothing
  • Python version used: 3.9.19

Additional notes

  • Pyright works in this case.
  • The first character of the file name is important. In this case, since Base is in b.py, any module named after b alphabetically (i.e., c, d, e, etc...) will fail to determine the type but not a.py. E.g., if b.py was instead d.py, then both a.py and c.py will have the correct type.
  • The bottom imports and the Base import in circular.py are needed to reproduce this. If removed, this works.

Workaround

Explicitly add the type:

self._v: t.Final[str] = v or circular.v

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

Reproduziere das Problem mit den aufgeführten Dateien a.py, b.py, c.py und circular.py unter Verwendung von mypy 1.15.0 und vergleiche anschließend die abgeleiteten Typen für A und C. Verfolge die Behandlung von Circular Imports und Bottom Imports, die für den alphabetischen Unterschied verantwortlich ist; abgeschlossen bedeutet, dass beide Kindklassen _v konsistent ableiten oder konsistent fehlschlagen, mit einer Regressionstestabdeckung für das Beispiel.

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
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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