Skip to content

Commit 756f62b

Browse files
don't copy magic or private methods in from_queryset managers (typeddjango#2240) (#16)
Co-authored-by: Anthony Sottile <[email protected]>
1 parent a4d581a commit 756f62b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

mypy_django_plugin/transformers/managers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ def populate_manager_from_queryset(manager_info: TypeInfo, queryset_info: TypeIn
454454
for name, sym in class_mro_info.names.items():
455455
if not isinstance(sym.node, (FuncDef, OverloadedFuncDef, Decorator)):
456456
continue
457+
# private, magic methods are not copied
458+
# https://github.com/django/django/blob/5.0.4/django/db/models/manager.py#L101
459+
elif name.startswith("_"):
460+
continue
457461
# Insert the queryset method name as a class member. Note that the type of
458462
# the method is set as Any. Figuring out the type is the job of the
459463
# 'resolve_manager_method' attribute hook, which comes later.

tests/typecheck/managers/querysets/test_from_queryset.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,37 @@
791791
792792
StrCallable = BaseManager.from_queryset(ModelQuerySet, class_name=str(1))
793793
794+
- case: queryset_inheritable_does_not_clobber_super_init
795+
main: |
796+
from myapp.models import BaseManager
797+
798+
reveal_type(BaseManager().ttl) # N: Revealed type is "builtins.int"
799+
installed_apps:
800+
- myapp
801+
files:
802+
- path: myapp/__init__.py
803+
- path: myapp/models.py
804+
content: |
805+
from typing import Any, TYPE_CHECKING
806+
from django.db import models
807+
from django.db.models.manager import Manager
808+
from django.db.models.query import QuerySet
809+
810+
if TYPE_CHECKING:
811+
from django.db.models.manager import _T
812+
from django.db.models.query import _Model, _Row
813+
814+
class MyQuerySet(QuerySet["_Model", "_Row"]):
815+
def __init__(self, *a: Any, **k: Any) -> None:
816+
super().__init__(*a, **k)
817+
818+
_base = Manager.from_queryset(MyQuerySet)
819+
820+
class BaseManager(_base["_T"]):
821+
def __init__(self, *a: Any, ttl: int = 0, **k: Any) -> None:
822+
self.ttl = ttl
823+
super().__init__(*a, **k)
824+
794825
- case: test_queryset_arg_as_unsupported_expressions
795826
main: |
796827
from typing import Union, Generic, TypeVar

0 commit comments

Comments
 (0)