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:
from typing import List, TYPE_CHECKING
if TYPE_CHECKING:
import bar
def listify(arg: 'bar.BarClass') -> 'List[bar.BarClass]':
return [arg]
File bar.py:
from typing import List
from foo import listify
class BarClass:
def listifyme(self) -> 'List[BarClass]':
return listify(self)
Expected behavior
No errors.
VS Code extension or command-line
VS Code extension 1.1.44
Python 3.7.4
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.
Most helpful comment
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.