Type Hinting in Python Without Cyclic Imports
In an attempt to split a large class into two smaller ones, you encounter the issue of cyclic imports. The "main" class imports the mixin class, and the mixin class references the "main" class in its type hint. This results in an import cycle, preventing the type hinting from working properly.
To resolve this, you can consider the following approach:
# main.py
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from mixin import MyMixin
class Main(object):
def func1(self, xxx: Any):
...
# mixin.py
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from main import Main
class MyMixin(object):
def func2(self: Main, xxx: Any):
...
In this example, Python's TYPE_CHECKING constant is used to conditionally import the "main" class. When running the code, the import statement is skipped due to the constant being False. However, type-checking tools like mypy interpret the code within the if TYPE_CHECKING block and recognize the type annotation for func2.
Another approach involves using a forward reference:
# mixin.py
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from main import Main
class MyMixin(object):
def func2(self, xxx: 'Main'):
...
Here, the type annotation for func2 is a string representing the name of the class that will be imported later. Both approaches require the use of Python 3.7 or higher to work effectively with type hints.
Remember that using mixins and type hinting may require additional structuring in your code. For example, mypy recommends creating an Abstract Base Class (ABC) that both your "main" and mixin classes inherit from. This can improve the accuracy of type checking.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3