python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.3k stars 2.8k forks source link

Another circular import in `mypy/test/testreports.py` #17726

Open webknjaz opened 1 month ago

webknjaz commented 1 month ago

Bug Report

Running the testreports module in isolation reveals a circular import similar to #15570.

To Reproduce

$ tox r -e py311 -- mypy/test/testreports.py

Expected Behavior

It should collect the tests from the module.

Actual Behavior

______________________________________________________________________________ ERROR collecting mypy/test/testreports.py _______________________________________________________________________________
ImportError while importing test module '~/src/github/python/mypy/mypy/test/testreports.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
mypy/test/testreports.py:7: in <module>
    from mypy.report import CoberturaPackage, get_line_rate
mypy/report.py:19: in <module>
    from mypy import stats
mypy/stats.py:11: in <module>
    from mypy.argmap import map_formals_to_actuals
mypy/argmap.py:8: in <module>
    from mypy.maptype import map_instance_to_supertype
mypy/maptype.py:3: in <module>
    from mypy.expandtype import expand_type_by_instance
mypy/expandtype.py:7: in <module>
    from mypy.types import (
mypy/types.py:3751: in <module>
    from mypy.expandtype import ExpandTypeVisitor
E   ImportError: cannot import name 'ExpandTypeVisitor' from partially initialized module 'mypy.expandtype' (most likely due to a circular import) (~/src/github/python/mypy/mypy/expandtype.py)

Your Environment

webknjaz commented 1 month ago

I tried out all of the traceback frames and here's paths that trigger the circular import:

$ python -m mypy.report
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "~/src/github/python/mypy/mypy/report.py", line 19, in <module>
    from mypy import stats
  File "~/src/github/python/mypy/mypy/stats.py", line 11, in <module>
    from mypy.argmap import map_formals_to_actuals
  File "~/src/github/python/mypy/mypy/argmap.py", line 8, in <module>
    from mypy.maptype import map_instance_to_supertype
  File "~/src/github/python/mypy/mypy/maptype.py", line 3, in <module>
    from mypy.expandtype import expand_type_by_instance
  File "~/src/github/python/mypy/mypy/expandtype.py", line 7, in <module>
    from mypy.types import (
  File "~/src/github/python/mypy/mypy/types.py", line 3751, in <module>
    from mypy.expandtype import ExpandTypeVisitor
ImportError: cannot import name 'ExpandTypeVisitor' from partially initialized module 'mypy.expandtype' (most likely due to a circular import) (~/src/github/python/mypy/mypy/expandtype.py)

$ python -m mypy
usage: mypy [-h] [-v] [-V] [more options; see below]
            [-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Missing target module, package, files, or command.

$ python -m mypy.argmap
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "~/src/github/python/mypy/mypy/argmap.py", line 8, in <module>
    from mypy.maptype import map_instance_to_supertype
  File "~/src/github/python/mypy/mypy/maptype.py", line 3, in <module>
    from mypy.expandtype import expand_type_by_instance
  File "~/src/github/python/mypy/mypy/expandtype.py", line 7, in <module>
    from mypy.types import (
  File "~/src/github/python/mypy/mypy/types.py", line 3751, in <module>
    from mypy.expandtype import ExpandTypeVisitor
ImportError: cannot import name 'ExpandTypeVisitor' from partially initialized module 'mypy.expandtype' (most likely due to a circular import) (~/src/github/python/mypy/mypy/expandtype.py)

$ python -m mypy.maptype
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "~/src/github/python/mypy/mypy/maptype.py", line 3, in <module>
    from mypy.expandtype import expand_type_by_instance
  File "~/src/github/python/mypy/mypy/expandtype.py", line 7, in <module>
    from mypy.types import (
  File "~/src/github/python/mypy/mypy/types.py", line 3751, in <module>
    from mypy.expandtype import ExpandTypeVisitor
ImportError: cannot import name 'ExpandTypeVisitor' from partially initialized module 'mypy.expandtype' (most likely due to a circular import) (~/src/github/python/mypy/mypy/expandtype.py)

$ python -m mypy.expandtype

$ python -m mypy.types
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "~/src/github/python/mypy/mypy/types.py", line 3178, in <module>
    from mypy.type_visitor import (
  File "~/src/github/python/mypy/mypy/type_visitor.py", line 21, in <module>
    from mypy.types import (
  File "~/src/github/python/mypy/mypy/types.py", line 3178, in <module>
    from mypy.type_visitor import (
ImportError: cannot import name 'ALL_STRATEGY' from partially initialized module 'mypy.type_visitor' (most likely due to a circular import) (~/src/github/python/mypy/mypy/type_visitor.py)
webknjaz commented 1 month ago

The traceback was discovered by @x612skm while looking into #17689.

webknjaz commented 1 month ago

It might be good to adapt https://github.com/aio-libs/aiohttp/blob/a847b5f/tests/test_circular_imports.py to run in this repo and catch any future import loops.