Skip to content

Commit

Permalink
don't copy magic or private methods in from_queryset managers (typedd…
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile authored and asottile-sentry committed Jun 28, 2024
1 parent ac84848 commit 91f7fe5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy_django_plugin/transformers/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ def populate_manager_from_queryset(manager_info: TypeInfo, queryset_info: TypeIn
for name, sym in class_mro_info.names.items():
if not isinstance(sym.node, (FuncDef, OverloadedFuncDef, Decorator)):
continue
# private, magic methods are not copied
# https://github.com/django/django/blob/5.0.4/django/db/models/manager.py#L101
elif name.startswith("_"):
continue
# Insert the queryset method name as a class member. Note that the type of
# the method is set as Any. Figuring out the type is the job of the
# 'resolve_manager_method' attribute hook, which comes later.
Expand Down
31 changes: 31 additions & 0 deletions tests/typecheck/managers/querysets/test_from_queryset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,37 @@
StrCallable = BaseManager.from_queryset(ModelQuerySet, class_name=str(1))
- case: queryset_inheritable_does_not_clobber_super_init
main: |
from myapp.models import BaseManager
reveal_type(BaseManager().ttl) # N: Revealed type is "builtins.int"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from typing import Any, TYPE_CHECKING
from django.db import models
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
if TYPE_CHECKING:
from django.db.models.manager import _T
from django.db.models.query import _Model, _Row
class MyQuerySet(QuerySet["_Model", "_Row"]):
def __init__(self, *a: Any, **k: Any) -> None:
super().__init__(*a, **k)
_base = Manager.from_queryset(MyQuerySet)
class BaseManager(_base["_T"]):
def __init__(self, *a: Any, ttl: int = 0, **k: Any) -> None:
self.ttl = ttl
super().__init__(*a, **k)
- case: test_queryset_arg_as_unsupported_expressions
main: |
from typing import Union, Generic, TypeVar
Expand Down

0 comments on commit 91f7fe5

Please sign in to comment.