Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I am writing a Python package where I have two classes in different files that (indirectly) depend on each other. If I wouldn't care about type-hints, there would be no problem, but unfortunately,...
#1: Initial revision
How can I properly type-hint methods in different files that would lead to circular imports?
I am writing a Python package where I have two classes in different files that (indirectly) depend on each other. If I wouldn't care about type-hints, there would be no problem, but unfortunately, I do like static type-hinting. Therefore, I need one class for typing the methods of the other and vice versa. Of course, this leads to a circular import, but using [forward references](https://peps.python.org/pep-0484/#forward-references), I managed to arrive at the following code: ```python # process.py from helpers import Helper class Process: def do_something(self, helper: Helper): ... ``` ```python # helpers.py class Helper: def update(self, process: "Process"): ... ``` This code runs without problems, but when running `mypy`, it complains that `Name "Process" is not defined`. I know I could [silence these errors](https://mypy.readthedocs.io/en/stable/common_issues.html#spurious-errors-and-locally-silencing-the-checker), but I was wondering whether there is a "proper" way to fix the type-hinting in this case.