Skip to content

task: resolve circular dependency forcing mid-function import for node-type detection #1092

Description

@minitriga

Component

Python SDK

Task Description

RelatedNodeBase.__init__ (infrahub_sdk/node/related_node.py) needs to detect
whether the data it receives is a node instance. Because node.py imports
related_node.py at module load, related_node.py cannot import InfrahubNodeBase
at the top level — they form a circular dependency. The current check therefore
relies on a mid-function lazy import:

from .node import InfrahubNodeBase as _InfrahubNodeBase inside __init__.

This was introduced in #1090 to replace a too-broad hasattr(data, "_schema")
duck-typing check (which could accept non-node objects). The lazy import works and
is type-correct, but it's a workaround: the underlying problem is the circular
dependency between node.py and related_node.py and the absence of a clean
shared base / protocol that node-type detection can import without a cycle.

As @ajtmccarty noted on the PR, this is out of scope for #1090 but worth tracking.
The same duck-typing workaround likely appears elsewhere in the node layer, so a
structural fix (e.g. a dependency-free shared base or protocol module) would let
several call sites drop their lazy imports / hasattr checks.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/tech-debtItem we know we need to improve way it is implemented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions