Skip to content

Commit c203556

Browse files
committed
lookup manager type via mro
there's a lot going on in the test but this specifically breaks lookup through `TypeVar` in mypy 1.11 (mypy 1.10 also failed in this way as well -- but was fixed in python/mypy#17381) test originally failing with: ``` /tmp/django-stubs/tests/typecheck/models/test_inheritance.yml:114: E pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output: E Actual: E myapp/models:23: error: Incompatible return value type (got "Bound", expected "T") [return-value] (diff) E Expected: E (empty) ```
1 parent d747285 commit c203556

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

mypy_django_plugin/transformers/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
326326

327327
incomplete_manager_defs = set()
328328
for manager_name, manager in model_cls._meta.managers_map.items():
329-
manager_node = self.model_classdef.info.names.get(manager_name, None)
329+
manager_node = self.model_classdef.info.get(manager_name)
330330
manager_fullname = helpers.get_class_fullname(manager.__class__)
331331
manager_info = self.lookup_manager(manager_fullname, manager)
332332

tests/typecheck/models/test_inheritance.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,44 @@
109109
abstract = True
110110
class User(Mixin1, Mixin2):
111111
name = models.TextField()
112+
113+
- case: test_manager_typevar_through_bounds
114+
main: |
115+
from myapp.models import ResultProcessorConcrete
116+
reveal_type(ResultProcessorConcrete().f()) # N: Revealed type is "myapp.models.Concrete"
117+
installed_apps:
118+
- myapp
119+
files:
120+
- path: myapp/__init__.py
121+
- path: myapp/models.py
122+
content: |
123+
from __future__ import annotations
124+
125+
from django.db.models import Model
126+
from django.db.models.manager import Manager
127+
from typing import TypeVar, Generic, ClassVar
128+
from typing_extensions import Self
129+
130+
M = TypeVar("M", bound=Model, covariant=True)
131+
132+
class BaseManager(Manager[M]): ...
133+
134+
class Base(Model):
135+
custom_objects: ClassVar[BaseManager[Self]] = BaseManager()
136+
137+
class Bound(Base): pass
138+
139+
T = TypeVar("T", bound=Bound)
140+
141+
class ResultProcessorBase(Generic[T]):
142+
@property
143+
def model_cls(self) -> type[T]:
144+
raise NotImplementedError
145+
146+
def f(self) -> T:
147+
return self.model_cls.custom_objects.get()
148+
149+
class Concrete(Bound): pass
150+
151+
class ResultProcessorConcrete(ResultProcessorBase[Concrete]):
152+
pass

0 commit comments

Comments
 (0)