python / python/mypy

Typeshed import cycle causes mypy to start claiming a fixed-length tuple type alias declared with `TypeAlias` is "not valid as a type"

Aperta
#16,581 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

affects-typeshed bug topic-import-cycles topic-type-alias
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

We're having a bit of trouble over in https://github.com/python/typeshed/pull/11074. It seems like it's impossible to import importlib.readers inside stdlib/importlib/machinery.pyi and also use typing_extensions.TypeAlias to explicitly demarcate type aliases inside stdlib/zipfile.pyi. The cause appears to be a huge import cycle in our stubs for the standard library:

  • importlib.machinery imports NamespaceReader from importlib.resources.readers
  • importlib.reasources.readers imports zipfile.Path from zipfile
  • zipfile imports TypeAlias from typing_extensions
  • typing_extensions imports get_original_bases from types
  • types imports ModuleSpec from importlib.machinery... and we're back at the beginning.

As a workaround, things seem to work fine for us if we just don't use typing_extensions.TypeAlias for the problematic alias in zipfile.pyi. But this behaviour from mypy seems buggy, so I figured it would be good to file a bug.

To Reproduce

I haven't been able to reproduce this issue outside of the typeshed context. However, I have reduced typeshed down to a "minimum viable typeshed" required to reproduce the bug (https://github.com/AlexWaygood/typeshed/tree/mypy-import-cycle-repro/stdlib). Here are the repro steps:

  1. Clone https://github.com/AlexWaygood/typeshed
  2. Checkout the mypy-import-cycle-repro branch of the repo
  3. Create and activate a venv; run pip install -r requirements-tests.txt
  4. Run python tests/mypy_test.py stdlib -p3.12

Expected Behavior

Mypy handles the import cycle -- or at least gives an intelligible error reporting what the problem is.

Actual Behavior

stdlib\zipfile.pyi:4: error: Variable "zipfile._DateTuple" is not valid as a type  [valid-type]
stdlib\zipfile.pyi:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

Your Environment

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

Controlla il branch mypy-import-cycle-repro ed esegui python tests/mypy_test.py stdlib -p3.12 per riprodurre l'errore in stdlib/zipfile.pyi. Traccia il ciclo di importazione attraverso gli stub indicati nel report, in particolare stdlib/importlib/machinery.pyi, stdlib/importlib/resources/readers.pyi, stdlib/zipfile.pyi, stdlib/typing_extensions.pyi e stdlib/types.pyi; il lavoro è completato quando mypy gestisce il ciclo o lo segnala in modo comprensibile.

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.