-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Cycle detected in import chain #746
Comments
This behavior is "as designed" in Pyright. If you enable the cycle detection feature, Pyright will detect and report all import cycles regardless of whether they actually result in an exception at runtime. Import cycles, even those that don't generate runtime exceptions because they are guarded by a runtime conditional, point to architectural layering issues that should be avoided in code. This feature is intended to detect and report these layering violations. If you don't care about architectural layering violations, you can disable the cycle detection in Pyright by disabling this diagnostic rule. If you do care about architectural layering, then you can refactor your code to eliminate the cycles. This might mean splitting up declarations into different source files — i.e. placing low-level constants, utility functions, and class definitions in a "leaf" module and placing higher-level classes and functions in a separate file that consumes the lower-level definitions. |
Thank you for your kind instruction. |
@erictraut , What is the suggested solution for import cycles generated when creating self-referencing SQLalchemy models? Circular references are fine in DBMSs, however to statically type ORM models which capture these circular relationships, one would need to create self-referencing parent and child classes. The only way of avoiding import cycles is currently to either not add types, or put all offending classes into a single file, which in my opinion feels like more of an "architectural layering violation" than cyclical type imports. Import cycle detection is a wonderful feature, but would benefit greatly from understanding |
The solution I use in this case is to place model classes in the same file if they have mutual recursive dependencies. Use of conditional imports within Making the type checker's behavior dependent on whether code is found within a |
Describe the bug
Pyright report
cycle detected in import chain
when using the method described in the link.https://mypy.readthedocs.io/en/stable/common_issues.html#import-cycles
To Reproduce
File foo.py:
File bar.py:
Expected behavior
No errors.
VS Code extension or command-line
VS Code extension 1.1.44
Python 3.7.4
The text was updated successfully, but these errors were encountered: