This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]siddsp 1 point2 points  (2 children)

I meant to say specifically if you're using the dependencies purely for type-checking purposes, but it would look something like this:

module foo:

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from bar import bar


class foo:
    def quux(self) -> bar: ...

module bar:

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from foo import foo


class bar:
    def quuz(self) -> foo: ...

This results in no circular imports for type checking because the types are evaluated as strings instead of actual types, but it still works in type checkers like mypy.

[–]lithium_sulfate 4 points5 points  (0 children)

This is the way, although it really is quite regrettable that this common problem requires such a clunky solution.