# Prevent cyclical import when type hinting custom exception \_\_init\_\_ method

**URL:** <https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921>\
**Category:** Python Help\
**Tags:** help, typing\
**Created:** [September 7, 2026, 4:42pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921 "2026-09-07T16:42:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucie](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lucie](https://discuss.python.org/u/Lucie)\
**Post date:** [September 7, 2026, 4:42pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/1 "2026-09-07T16:42:53Z")

</div>

Hi, I’m trying to create a tree data structure in python to do this I have the following files:

```python
# module/node.py

from collections.abc import Iterable
from module.exceptions import MultipleParentsError

class Node:
    def __init__ (self, initial_children: Iterable[Node] = ()) -> None:
        self.parent: Node | None = None
        
        self.children: list[Node] = []
        for child in initial_children:
            self.add_child(child)
 
     def add_child(self, child):
          if child.parent is not None:
              raise MultipleParentsError(child, self, child.parent)
          else:
              self.children.append(child)
              child.parent = self

```

and

```python
# module/exceptions.py
 
from module.node import Node

class MultipleParentsError(RuntimeError):
    def __init__ (self, child: Node, new_parent: Node, preexisting_parent: Node, *args: object) -> None:
        super(). __init__ (f"Tried to add child {child} to {new_parent} but child already has a parent: {preexisting_parent}", *args)

```

I only import Node in the exceptions.py for type checking, The problem is that this creates an import cycle. From what I could see by searching online, the variable typing.TYPE\_CHECKING can be used to avoid this, however my type checker (basedpyright) still shows the import cycle error. This is apparently because this type of error is indicative of a larger issue with the program’s structure, I can see two solutions to this, but I don’t know which one is the best or if I am missing one:

1. Move MultipleParentsError to the node.py file as it is related to the same functionality (so far this is my preferred solution)
2. Give up and use type Any or no type hinting (but this seems like a bad idea)

---

<div class="post-metadata">

**Author:** ![zedzhen](https://avatars.discourse-cdn.com/v4/letter/z/97f17d/32.png) [@zedzhen](https://discuss.python.org/u/zedzhen)\
**Post date:** [September 7, 2026, 7:55pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/2 "2026-09-07T19:55:48Z")

</div>

1. use `from module import node` and `node.Node` in annotation

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [September 7, 2026, 8:16pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/3 "2026-09-07T20:16:20Z")

</div>

> [@Lucie](#):
>
> From what I could see by searching online, the variable typing.TYPE\_CHECKING can be used to avoid this, however my type checker (basedpyright) still shows the import cycle error.

Can you share what your code looks like when you do this? From the snippets you’ve provided, `typing.TYPE_CHECKING` should be perfectly suitable, so I think there must be some context you’re leaving out.

---

<div class="post-metadata">

**Author:** ![Lucie](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lucie](https://discuss.python.org/u/Lucie)\
**Post date:** [September 7, 2026, 8:18pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/4 "2026-09-07T20:18:01Z")

</div>

That doesn’t help as that still imports node.py which imports exceptions.py

---

<div class="post-metadata">

**Author:** ![Lucie](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lucie](https://discuss.python.org/u/Lucie)\
**Post date:** [September 7, 2026, 8:26pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/5 "2026-09-07T20:26:51Z")

</div>

It would look like this:

```python
# module/exceptions.py
 

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from module.node import Node

class MultipleParentsError(RuntimeError):
    def __init__ (self, child: Node, new_parent: Node, preexisting_parent: Node, *args: object) -> None:
        super(). __init__ (f"Tried to add child {child} to {new_parent} but child already has a parent: {preexisting_parent}", *args)

```

I still get the following error: Cycle detected in import chain basedpyright ([reportImportCycles](https://docs.basedpyright.com/v1.39.10/configuration/config-files/#reportImportCycles))

this is apparently intended by the developer: see [here](https://github.com/DetachHead/basedpyright/issues/960)

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [September 7, 2026, 8:35pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/6 "2026-09-07T20:35:30Z")

</div>

From a runtime and type safety standpoint, there’s nothing wrong with this code. This is a case where your type checker is being opinionated in a way that you may not agree with. I would personally just disable this error code, as it is an optional one.

---

<div class="post-metadata">

**Author:** ![Lucie](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lucie](https://discuss.python.org/u/Lucie)\
**Post date:** [September 7, 2026, 8:49pm UTC](https://discuss.python.org/t/prevent-cyclical-import-when-type-hinting-custom-exception-init-method/108921/7 "2026-09-07T20:49:41Z")

</div>

OK, I was worried it meant there was an issue with the structure of my code, but if you think it’s fine then I’ll just disable the error
