Skip to content

A question about import cycles #7393

Closed
Closed
@bluetech

Description

@bluetech

I investigated cyclic imports and thought about adding a warning to mypy about them. I looked a bit and found that mypy logs SCCs with more than one module:

mypy -v --no-incremental src |& grep 'Processing SCC of size'

I noticed that mypy considered the following (common?) pattern in some packages to be cyclic:

[file package/_utils.py]
def some_helper(): pass

[file package/_a.py]
# Alternative 1
from . import _utils
# Alternative 2
# from ._utils import some_helper

class A: pass

[file package/__init__.py]
# Expose public interface
from ._a import A

Alternative 1 causes a cycle:

LOG:  Processing SCC of size 2 (package.a package) as inherently stale with stale deps (builtins)

Alternative 2 doesn't.

I wonder if, in the absence of some redefinition of _utils in __init__.py (e.g. _utils = 5), Alternative 1 can avoid creating a dependency of package._a on package?

#6312 is similar but there the two files actually import each other.

mypy version 0.720.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions