From 173fc477f4704f3b74a162e39c8e44afc3ff88f8 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Thu, 9 Nov 2023 12:49:52 +0200 Subject: [PATCH 1/3] Export RelatedManager, ManyRelatedManager stub-only classes from django-stubs-ext --- ext/django_stubs_ext/db/models.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ext/django_stubs_ext/db/models.py b/ext/django_stubs_ext/db/models.py index 172cd3055..bdbc704b2 100644 --- a/ext/django_stubs_ext/db/models.py +++ b/ext/django_stubs_ext/db/models.py @@ -1,5 +1,6 @@ from typing import TYPE_CHECKING +# Define TypedModelMeta if TYPE_CHECKING: from typing import ClassVar, List, Literal, Sequence, Tuple, Union @@ -42,3 +43,26 @@ class TypedModelMeta: else: TypedModelMeta = object + + +# Re-export stubs-only classes RelatedManger and ManyRelatedManager. +# These are fake, Django defines these inside function body. +if TYPE_CHECKING: + # noinspection PyUnresolvedReferences + from django.db.models.fields.related_descriptors import ManyRelatedManager as ManyRelatedManager + + # noinspection PyUnresolvedReferences + from django.db.models.fields.related_descriptors import RelatedManager as RelatedManager + +else: + from typing import Protocol, TypeVar + + _T = TypeVar("_T") + + # Define as `Protocol` to prevent them being used with `isinstance()`. + # These actually inherit from `BaseManager`. + class RelatedManager(Protocol[_T]): + pass + + class ManyRelatedManager(Protocol[_T]): + pass From bbb90adb4a527fd279aa8aeb713f1bc3677c99ab Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Thu, 9 Nov 2023 13:16:21 +0200 Subject: [PATCH 2/3] Move to django_stubs_ext.db.models.managers --- .../db/{models.py => models/__init__.py} | 24 ------------------- ext/django_stubs_ext/db/models/managers.py | 23 ++++++++++++++++++ 2 files changed, 23 insertions(+), 24 deletions(-) rename ext/django_stubs_ext/db/{models.py => models/__init__.py} (71%) create mode 100644 ext/django_stubs_ext/db/models/managers.py diff --git a/ext/django_stubs_ext/db/models.py b/ext/django_stubs_ext/db/models/__init__.py similarity index 71% rename from ext/django_stubs_ext/db/models.py rename to ext/django_stubs_ext/db/models/__init__.py index bdbc704b2..172cd3055 100644 --- a/ext/django_stubs_ext/db/models.py +++ b/ext/django_stubs_ext/db/models/__init__.py @@ -1,6 +1,5 @@ from typing import TYPE_CHECKING -# Define TypedModelMeta if TYPE_CHECKING: from typing import ClassVar, List, Literal, Sequence, Tuple, Union @@ -43,26 +42,3 @@ class TypedModelMeta: else: TypedModelMeta = object - - -# Re-export stubs-only classes RelatedManger and ManyRelatedManager. -# These are fake, Django defines these inside function body. -if TYPE_CHECKING: - # noinspection PyUnresolvedReferences - from django.db.models.fields.related_descriptors import ManyRelatedManager as ManyRelatedManager - - # noinspection PyUnresolvedReferences - from django.db.models.fields.related_descriptors import RelatedManager as RelatedManager - -else: - from typing import Protocol, TypeVar - - _T = TypeVar("_T") - - # Define as `Protocol` to prevent them being used with `isinstance()`. - # These actually inherit from `BaseManager`. - class RelatedManager(Protocol[_T]): - pass - - class ManyRelatedManager(Protocol[_T]): - pass diff --git a/ext/django_stubs_ext/db/models/managers.py b/ext/django_stubs_ext/db/models/managers.py new file mode 100644 index 000000000..97a4651b0 --- /dev/null +++ b/ext/django_stubs_ext/db/models/managers.py @@ -0,0 +1,23 @@ +from typing import TYPE_CHECKING + +# Re-export stubs-only classes RelatedManger and ManyRelatedManager. +# These are fake, Django defines these inside function body. +if TYPE_CHECKING: + # noinspection PyUnresolvedReferences + from django.db.models.fields.related_descriptors import ManyRelatedManager as ManyRelatedManager + + # noinspection PyUnresolvedReferences + from django.db.models.fields.related_descriptors import RelatedManager as RelatedManager + +else: + from typing import Protocol, TypeVar + + _T = TypeVar("_T") + + # Define as `Protocol` to prevent them being used with `isinstance()`. + # These actually inherit from `BaseManager`. + class RelatedManager(Protocol[_T]): + pass + + class ManyRelatedManager(Protocol[_T]): + pass From 51e6a4e988d83bf557b6ccbfe0e23140e43b727c Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Thu, 9 Nov 2023 13:21:39 +0200 Subject: [PATCH 3/3] Move django_stubs_ext.db.models.managers -> manager to mirror Django better --- ext/django_stubs_ext/db/models/{managers.py => manager.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ext/django_stubs_ext/db/models/{managers.py => manager.py} (100%) diff --git a/ext/django_stubs_ext/db/models/managers.py b/ext/django_stubs_ext/db/models/manager.py similarity index 100% rename from ext/django_stubs_ext/db/models/managers.py rename to ext/django_stubs_ext/db/models/manager.py