diff --git a/mypy_django_plugin/transformers/querysets.py b/mypy_django_plugin/transformers/querysets.py index f7e7933d4..05b1dad3c 100644 --- a/mypy_django_plugin/transformers/querysets.py +++ b/mypy_django_plugin/transformers/querysets.py @@ -33,10 +33,14 @@ def determine_proper_manager_type(ctx: FunctionContext) -> MypyType: assert isinstance(default_return_type, Instance) outer_model_info = helpers.get_typechecker_api(ctx).scope.active_class() - if outer_model_info is None or not outer_model_info.has_base(fullnames.MODEL_CLASS_FULLNAME): + if ( + outer_model_info is None + or not outer_model_info.has_base(fullnames.MODEL_CLASS_FULLNAME) + or outer_model_info.self_type is None + ): return default_return_type - return helpers.reparametrize_instance(default_return_type, [Instance(outer_model_info, [])]) + return helpers.reparametrize_instance(default_return_type, [outer_model_info.self_type]) def get_field_type_from_lookup( diff --git a/tests/typecheck/managers/test_managers.yml b/tests/typecheck/managers/test_managers.yml index adf38db7a..4eedd90bd 100644 --- a/tests/typecheck/managers/test_managers.yml +++ b/tests/typecheck/managers/test_managers.yml @@ -581,6 +581,7 @@ - path: myapp/models.py content: | from typing import ClassVar, TypeVar + from typing_extensions import Self from django.db import models T = TypeVar("T", bound="MyModel") @@ -595,7 +596,7 @@ pass class MySubModel(MyModel): - objects: ClassVar[MySubManager["MySubModel"]] = MySubManager() + objects: ClassVar[MySubManager[Self]] = MySubManager() - case: subclass_manager_without_type_parameters_disallow_any_generics main: |