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

Possible false positive cyclical import #3278

Closed
Gjgarr opened this issue Mar 31, 2022 · 1 comment
Closed

Possible false positive cyclical import #3278

Gjgarr opened this issue Mar 31, 2022 · 1 comment
Labels
as designed Not a bug, working as intended

Comments

@Gjgarr
Copy link

Gjgarr commented Mar 31, 2022

Describe the bug
Pyright reports a cyclic import within __init__.py when a star-import includes an imported module at the same level.

To Reproduce
Reported as cyclical import:

# ./cyclical/__init__.py
from .f1 import * # Cycle detected - __init__.py & f1.py

# ./cyclical/f1.py
from . import f2

# ./cyclical/f2.py (empty)

No cyclical import reported:

# ./not_cyc/__init__.py (empty)

# ./not_cyc/f1.py
from .f2 import *

# ./not_cyc/f2.py
from . import f3

# ./not_cyc/f3.py (empty)

Expected behavior
Neither scenario reported as cyclical.

VS Code extension or command-line
VS Code extension - pylance v2022.3.4

@erictraut
Copy link
Collaborator

This is intended behavior, not a false positive.

The statement from .f1 import * within an __init__.py method imports f1 and assigns a reference to the f1 module to the local symbol f1. The f1 module imports f2 from the __init__.py module. So this is an actual cyclical import.

The cycle detection code in pyright is admittedly strict about what constitutes an import cycle. It will detect cycles that do not necessarily result in runtime exceptions. That's intended. If you want to use this diagnostic check, you'll need to factor your code to avoid all cycles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants