django-simple-history
django-simple-history copied to clipboard
Q: Is there a recommended way for type hinting Historical[Model]?
Was looking for a way to add type hinting so I could reference the historical model. This is what I came up with. Just curious if anyone else has a solution they use.
I have this defined in a shared part of the code:
from datetime import datetime
from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar
from simple_history.models import ModelDelta
from typing_extensions import Self
if TYPE_CHECKING:
from users.models import User
T = TypeVar("T")
class HistoricalModel(Generic[T]):
history_change_reason: str
history_date: datetime
history_id: Any
history_object: Any
history_relation: Any
history_type: str
history_user: Optional[User]
history_user_id: Any
instance_type: type[T]
instance: T
def diff_against(
self,
old_history: "Self",
excluded_fields: Optional[list[str]] = None,
included_fields: Optional[list[str]] = None,
) -> ModelDelta:
...
def get_default_history_user(self) -> Optional[User]:
...
def next_record(self) -> Optional[T]:
...
def prev_record(self) -> Optional[T]:
...
def revert_url(self) -> str:
...
And below each model I define the historical model. I had to add a type ignore because mypy was complaining about incompatible metaclasses.
class MyModel(models.Model):
...
if TYPE_CHECKING:
from common.utils import HistoricalModel
class HistoricalMyModel( # type: ignore
HistoricalModel[MyModel],
MyModel,
):
...
Btw, if you (or anyone reading this) would like to add type hints to this project's code, feel free to open a PR! 😊
(Keep in mind that we currently support Python 3.8 and up, though, so type hinting features from e.g. 3.9 will have to wait until October this year when its EOL is - based on Python's version status page.)