Typeshed import cycle causes mypy to start claiming a fixed-length tuple type alias declared with `TypeAlias` is "not valid as a type"
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
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.machineryimportsNamespaceReaderfromimportlib.resources.readersimportlib.reasources.readersimportszipfile.PathfromzipfilezipfileimportsTypeAliasfromtyping_extensionstyping_extensionsimportsget_original_basesfromtypestypesimportsModuleSpecfromimportlib.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:
- Clone https://github.com/AlexWaygood/typeshed
- Checkout the
mypy-import-cycle-reprobranch of the repo - Create and activate a venv; run
pip install -r requirements-tests.txt - 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
- Mypy version used: 1.7.1
- Mypy command-line flags: https://github.com/AlexWaygood/typeshed/blob/4235e8c82a0378b7ce5df878659ed0e909692f13/tests/mypy_test.py#L252-L275
- Python version used: 3.12
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ブランチ mypy-import-cycle-repro を確認し、python tests/mypy_test.py stdlib -p3.12 を実行して stdlib/zipfile.pyi のエラーを再現します。レポートで示されているスタブ、特に stdlib/importlib/machinery.pyi、stdlib/importlib/resources/readers.pyi、stdlib/zipfile.pyi、stdlib/typing_extensions.pyi、stdlib/types.pyi をたどってインポートサイクルを追跡します。mypy がそのサイクルを処理するか、理解できる形で報告すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100