Originally, I intended to analyze django webpack; however, midway through, for the sake of rigorous documentation, I found myself having to delve into django components. Within django components, I discovered some mechanisms previously unknown to me. Consequently, let’s first discuss the typing issues encountered!
While recently reading the code of django components, I came across the following snippet in component_registry.py:
from typing import TYPE_CHECKING, Callable, Dict, Type, TypeVar
if TYPE_CHECKING:
from django_components import component
_TC = TypeVar("_TC", bound=Type["component.Component"])
From this, several points of knowledge can be gleaned:
- The
TYPE_CHECKING
variable is used as a tag for handling type-related code by programmers. During actual execution, the internal code is not executed. More details can be found in the official typing documentation. TypeVar
can be used like this:
S = TypeVar('S', bound=str) # Can be any subtype of str
In this snippet, _TC
signifies that it can be any subtype of component.Component
.
Conclusion
Python is gradually enriching its type system, and many new usages are also emerging. However, compared to TypeScript, its type system is somewhat more advanced. Keep learning!
Additional aside: It’s quite strange that a
Component
is actually a django view.