From ae6112ffa0c70325b990e26a7fa2a3b92bf850d5 Mon Sep 17 00:00:00 2001 From: Craig de Stigter Date: Thu, 23 Jan 2025 14:32:58 +1300 Subject: [PATCH] Add type hinted get_type_classes() method --- testapp/models.py | 5 +++++ typedmodels/__init__.py | 2 +- typedmodels/models.py | 25 +++++++++++++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/testapp/models.py b/testapp/models.py index 4b07969..029b5af 100644 --- a/testapp/models.py +++ b/testapp/models.py @@ -147,3 +147,8 @@ def typed_queryset() -> None: # This isn't actually called, but it's here for the mypy check to ensure that type hinting works correctly. queryset = Animal.objects.filter(pk=1) queryset.filter(name="lynx") # works, because Animal has this field + + +def do_get_type_classes() -> None: + for x in Animal.get_type_classes(): + print(x) diff --git a/typedmodels/__init__.py b/typedmodels/__init__.py index ae2a0b4..40770c5 100644 --- a/typedmodels/__init__.py +++ b/typedmodels/__init__.py @@ -1 +1 @@ -__version__ = "0.15.0b3" +__version__ = "0.15.0b4" diff --git a/typedmodels/models.py b/typedmodels/models.py index d9f2803..6bab2b3 100644 --- a/typedmodels/models.py +++ b/typedmodels/models.py @@ -223,9 +223,7 @@ def do_related_class(self, other, cls): # add a get_type_classes classmethod to allow fetching of all the subclasses (useful for admin) - def get_type_classes( - subcls: type["TypedModel"], - ) -> list[type["TypedModel"]]: + def _get_type_classes(subcls) -> list[type["TypedModel"]]: """ Returns a list of the classes which are proxy subtypes of this concrete typed model. """ @@ -237,9 +235,9 @@ def get_type_classes( for k in subcls._typedmodels_subtypes ] - cls.get_type_classes = classmethod(get_type_classes) # type: ignore + cls._get_type_classes = classmethod(_get_type_classes) # type: ignore - def get_types(subcls: type["TypedModel"]) -> list[str]: + def _get_types(subcls) -> list[str]: """ Returns a list of the possible string values (for the `type` attribute) for classes which are proxy subtypes of this concrete typed model. @@ -249,7 +247,7 @@ def get_types(subcls: type["TypedModel"]) -> list[str]: else: return subcls._typedmodels_subtypes[:] - cls.get_types = classmethod(get_types) # type: ignore + cls.get_types = classmethod(_get_types) # type: ignore return cls @@ -450,6 +448,21 @@ def from_db(cls, db, field_names, values): new._state.db = db return new + @classmethod + def get_type_classes(cls) -> list["__builtins__.type[Self]"]: + """ + Returns a list of the classes which are proxy subtypes of this concrete typed model. + """ + return cls._get_type_classes() # type: ignore + + @classmethod + def get_types(cls) -> list[str]: + """ + Returns a list of the possible string values (for the `type` attribute) for classes + which are proxy subtypes of this concrete typed model. + """ + return cls._get_types() # type: ignore + def __init__(self, *args, _typedmodels_do_recast=None, **kwargs): # Calling __init__ on base class because some functions (e.g. save()) need access to field values from base # class.