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

Ouverte
#16,581 7 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
python
Domaine
compilers

Piste de recherche

Examinez la branche mypy-import-cycle-repro et exécutez python tests/mypy_test.py stdlib -p3.12 pour reproduire l’erreur dans stdlib/zipfile.pyi. Suivez le cycle d’importation à travers les stubs nommés dans le rapport, en particulier stdlib/importlib/machinery.pyi, stdlib/importlib/resources/readers.pyi, stdlib/zipfile.pyi, stdlib/typing_extensions.pyi et stdlib/types.pyi ; le travail est terminé lorsque mypy gère le cycle ou le signale de manière intelligible.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

affects-typeshed bug topic-import-cycles topic-type-alias

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

Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Merge moyen
1 j 18 h
PR mergées (30 j)
54

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de python/mypy

Toutes les issues de python/mypy

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.