Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
webknjaz opened this issue Aug 30, 2024 · 3 comments
Open

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

webknjaz opened this issue Aug 30, 2024 · 3 comments
Labels
bug mypy got something wrong

Comments

@webknjaz
Copy link

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

  • Mypy version used: master
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.11
@webknjaz webknjaz added the bug mypy got something wrong label Aug 30, 2024
@webknjaz
Copy link
Author

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
Copy link
Author

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

@webknjaz
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant