Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PlaceholderNode when deferring manager class hook #2228

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy_django_plugin/transformers/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MemberExpr,
Node,
OverloadedFuncDef,
PlaceholderNode,
RefExpr,
StrExpr,
SymbolTableNode,
Expand Down Expand Up @@ -317,6 +318,9 @@ def create_new_manager_class_from_from_queryset_method(ctx: DynamicClassDefConte
new_manager_info = create_manager_info_from_from_queryset_call(semanal_api, ctx.call, ctx.name)
if new_manager_info is None:
if not ctx.api.final_iteration:
# XXX: hack for python/mypy#17402
ph = PlaceholderNode(ctx.api.qualified_name(ctx.name), ctx.call, ctx.call.line, becomes_typeinfo=True)
ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, ph))
ctx.api.defer()
return

Expand Down
35 changes: 35 additions & 0 deletions tests/typecheck/managers/querysets/test_from_queryset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,41 @@
self.ttl = ttl
super().__init__(*a, **k)

- case: test_from_queryset_with_deferral
main: |
from myapp.models import Concrete
reveal_type(Concrete.objects.qs_meth()) # N: Revealed type is "builtins.int"
reveal_type(Concrete.objects.manager_meth()) # N: Revealed type is "builtins.str"
mypy_config: |
[mypy.plugins.django-stubs]
django_settings_module = myapp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed to skip the importing of models -- since the forced-deferral trick models.py triggers a NameError when actually imported

files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from typing import TypeVar, ClassVar
from typing_extensions import Self
from django.db import models
from django.db.models.manager import Manager

M = TypeVar("M", bound=models.Model, covariant=True)

# bogus forward-`metaclass=` to trigger second semanal pass similar to #2224
class CustomQuerySet(models.QuerySet[M], metaclass=MCS):
def qs_meth(self) -> int:
return 1

_base = Manager.from_queryset(CustomQuerySet)

class CustomBase(_base[M]):
def manager_meth(self) -> str:
return 'hi'

class Concrete(models.Model):
objects: ClassVar[CustomBase[Self]] = CustomBase()

class MCS(type): pass

- case: test_queryset_arg_as_unsupported_expressions
main: |
from typing import Union, Generic, TypeVar
Expand Down
Loading