From 6658aed323bf48b03c1fd5d0239ecf004ee42526 Mon Sep 17 00:00:00 2001 From: Petter Friberg Date: Fri, 22 Sep 2023 08:31:43 +0200 Subject: [PATCH 1/5] Verify that `Manager.from_queryset()` handles invalid argument types --- .../managers/querysets/test_from_queryset.yml | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/typecheck/managers/querysets/test_from_queryset.yml b/tests/typecheck/managers/querysets/test_from_queryset.yml index 59f0cd63c..1d67d6220 100644 --- a/tests/typecheck/managers/querysets/test_from_queryset.yml +++ b/tests/typecheck/managers/querysets/test_from_queryset.yml @@ -675,3 +675,36 @@ ... StrCallable = BaseManager.from_queryset(ModelQuerySet, class_name=str(1)) + +- case: test_queryset_arg_as_unsupported_expressions + main: | + from myapp.models import ManagerFromAlias, ManagerFromNonQSGeneric + reveal_type(ManagerFromAlias) + reveal_type(ManagerFromNonQSGeneric) + out: | + main:2: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" + main:3: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" + myapp/models:11: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" + myapp/models:16: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" + installed_apps: + - myapp + files: + - path: myapp/__init__.py + - path: myapp/models.py + content: | + from typing import TypeAlias, Union, Generic, TypeVar + from django.db import models + from django.db.models.manager import Manager + + class QS1(models.QuerySet[models.Model]): + ... + class QS2(models.QuerySet[models.Model]): + ... + + Alias: TypeAlias = Union[QS1, QS2] + ManagerFromAlias = Manager[models.Model].from_queryset(Alias) + + T = TypeVar("T") + class NonQSGeneric(Generic[T]): + ... + ManagerFromNonQSGeneric = Manager[models.Model].from_queryset(NonQSGeneric[int]) From dffe3eac277d93d327884c0ed8b4ffb093154681 Mon Sep 17 00:00:00 2001 From: Petter Friberg Date: Fri, 22 Sep 2023 08:41:48 +0200 Subject: [PATCH 2/5] fixup! Verify that `Manager.from_queryset()` handles invalid argument types --- tests/typecheck/managers/querysets/test_from_queryset.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/typecheck/managers/querysets/test_from_queryset.yml b/tests/typecheck/managers/querysets/test_from_queryset.yml index 1d67d6220..d930e8949 100644 --- a/tests/typecheck/managers/querysets/test_from_queryset.yml +++ b/tests/typecheck/managers/querysets/test_from_queryset.yml @@ -692,7 +692,8 @@ - path: myapp/__init__.py - path: myapp/models.py content: | - from typing import TypeAlias, Union, Generic, TypeVar + from typing import Union, Generic, TypeVar + from typing_extensions import TypeAlias from django.db import models from django.db.models.manager import Manager From a974f172570186868bbd19ea8cd37d26fc3dd75b Mon Sep 17 00:00:00 2001 From: Petter Friberg Date: Sat, 23 Sep 2023 15:39:57 +0200 Subject: [PATCH 3/5] fixup! fixup! Verify that `Manager.from_queryset()` handles invalid argument types --- tests/typecheck/managers/querysets/test_from_queryset.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/typecheck/managers/querysets/test_from_queryset.yml b/tests/typecheck/managers/querysets/test_from_queryset.yml index d930e8949..45124aac4 100644 --- a/tests/typecheck/managers/querysets/test_from_queryset.yml +++ b/tests/typecheck/managers/querysets/test_from_queryset.yml @@ -684,8 +684,8 @@ out: | main:2: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" main:3: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" - myapp/models:11: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" - myapp/models:16: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" + myapp/models:12: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" + myapp/models:17: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" installed_apps: - myapp files: From c4c603f6a3a6b6bd3350cc4aee985de92a412e67 Mon Sep 17 00:00:00 2001 From: Petter Friberg Date: Sat, 23 Sep 2023 15:56:29 +0200 Subject: [PATCH 4/5] fixup! fixup! fixup! Verify that `Manager.from_queryset()` handles invalid argument types --- .../managers/querysets/test_from_queryset.yml | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/tests/typecheck/managers/querysets/test_from_queryset.yml b/tests/typecheck/managers/querysets/test_from_queryset.yml index 45124aac4..9fa6c4bbc 100644 --- a/tests/typecheck/managers/querysets/test_from_queryset.yml +++ b/tests/typecheck/managers/querysets/test_from_queryset.yml @@ -678,34 +678,25 @@ - case: test_queryset_arg_as_unsupported_expressions main: | - from myapp.models import ManagerFromAlias, ManagerFromNonQSGeneric - reveal_type(ManagerFromAlias) - reveal_type(ManagerFromNonQSGeneric) + from typing import Union, Generic, TypeVar + from typing_extensions import TypeAlias + from django.db import models + from django.db.models.manager import Manager + + class QS1(models.QuerySet[models.Model]): + ... + class QS2(models.QuerySet[models.Model]): + ... + + Alias: TypeAlias = Union[QS1, QS2] + reveal_type(Manager[models.Model].from_queryset(Alias)) + + T = TypeVar("T") + class NonQSGeneric(Generic[T]): + ... + reveal_type(Manager[models.Model].from_queryset(NonQSGeneric[int])) out: | - main:2: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" - main:3: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" - myapp/models:12: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" - myapp/models:17: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" - installed_apps: - - myapp - files: - - path: myapp/__init__.py - - path: myapp/models.py - content: | - from typing import Union, Generic, TypeVar - from typing_extensions import TypeAlias - from django.db import models - from django.db.models.manager import Manager - - class QS1(models.QuerySet[models.Model]): - ... - class QS2(models.QuerySet[models.Model]): - ... - - Alias: TypeAlias = Union[QS1, QS2] - ManagerFromAlias = Manager[models.Model].from_queryset(Alias) - - T = TypeVar("T") - class NonQSGeneric(Generic[T]): - ... - ManagerFromNonQSGeneric = Manager[models.Model].from_queryset(NonQSGeneric[int]) + main:12: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" + main:12: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" + main:17: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" + main:17: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" From 3b021608853cf77fd005ef923bc3048222ab0a70 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 18 Oct 2023 14:02:20 +0300 Subject: [PATCH 5/5] Fix tests (I think) --- tests/typecheck/managers/querysets/test_from_queryset.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/typecheck/managers/querysets/test_from_queryset.yml b/tests/typecheck/managers/querysets/test_from_queryset.yml index 9fa6c4bbc..ec08da3da 100644 --- a/tests/typecheck/managers/querysets/test_from_queryset.yml +++ b/tests/typecheck/managers/querysets/test_from_queryset.yml @@ -697,6 +697,6 @@ reveal_type(Manager[models.Model].from_queryset(NonQSGeneric[int])) out: | main:12: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" - main:12: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" + main:12: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type ""; expected "Type[_QuerySet[Model, Model]]" [arg-type] main:17: note: Revealed type is "Type[django.db.models.manager.Manager[django.db.models.base.Model]]" - main:17: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" + main:17: error: Argument 1 to "from_queryset" of "BaseManager" has incompatible type "Type[NonQSGeneric[Any]]"; expected "Type[_QuerySet[Model, Model]]" [arg-type]