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"

Open
#16,581 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

affects-typeshed bug topic-import-cycles topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Check out the mypy-import-cycle-repro branch and run python tests/mypy_test.py stdlib -p3.12 to reproduce the error in stdlib/zipfile.pyi. Trace the import cycle through the stubs named in the report, especially stdlib/importlib/machinery.pyi, stdlib/importlib/resources/readers.pyi, stdlib/zipfile.pyi, stdlib/typing_extensions.pyi, and stdlib/types.pyi; done means mypy handles the cycle or reports it intelligibly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.