Skip to content

Commit

Permalink
fix as manager IndexError: tuple index out of range (#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
moranabadie committed Oct 21, 2023
1 parent 35bd241 commit 5985608
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy_django_plugin/transformers/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _process_dynamic_method(
ret_type = Instance(queryset_info, [manager_instance.args[0], manager_instance.args[0]])
variables = []
args_types = method_type.arg_types[1:]
if _has_compatible_type_vars(base_that_has_method):
if _has_compatible_type_vars(base_that_has_method) and manager_instance.args:
ret_type = _replace_type_var(
ret_type, base_that_has_method.defn.type_vars[0].fullname, manager_instance.args[0]
)
Expand Down
37 changes: 37 additions & 0 deletions tests/typecheck/managers/querysets/test_from_queryset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@
NewManager = BaseManager.from_queryset(ModelQuerySet)
class MyModel(models.Model):
objects = NewManager()
- case: bug_1785
main: |
from myapp.models import NewManager
reveal_type(NewManager().example()) # N: Revealed type is "_CTE`1"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/querysets.py
content: |
from typing import TypeVar, TYPE_CHECKING
from django.db import models
from django.db.models.manager import BaseManager
if TYPE_CHECKING:
from .models import MyModel
_CTE = TypeVar("_CTE", bound=models.Model)
class _MyModelQuerySet(models.QuerySet[_CTE]):
def example(self) -> _CTE: ...
class MyModelQuerySet(_MyModelQuerySet["MyModel"]): ...
- path: myapp/models.py
content: |
from django.db import models
from django.db.models.manager import BaseManager
from .querysets import MyModelQuerySet
class TypedManager(BaseManager[models.Model]): ...
NewManager = TypedManager.from_queryset(MyModelQuerySet)
class MyModel(models.Model):
objects = NewManager()
- case: handles_subclasses_of_queryset
main: |
from myapp.models import MyModel
Expand Down

0 comments on commit 5985608

Please sign in to comment.