Skip to content

Invoke get_dynamic_class_hook on method calls #7704

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 10 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2175,9 +2175,16 @@ def apply_dynamic_class_hook(self, s: AssignmentStmt) -> None:
if not isinstance(lval, NameExpr) or not isinstance(s.rvalue, CallExpr):
return
call = s.rvalue
if not isinstance(call.callee, RefExpr):
return
fname = call.callee.fullname
fname = None
# check if method call
if isinstance(call.callee, MemberExpr):
callee_expr = call.callee.expr
if isinstance(callee_expr, NameExpr) and callee_expr.fullname:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if isinstance(callee_expr, NameExpr) and callee_expr.fullname:
if isinstance(callee_expr, RefExpr) and callee_expr.fullname:

method_name = call.callee.name
fname = callee_expr.fullname + '.' + method_name
# else check if function call
elif isinstance(call.callee, RefExpr):
fname = call.callee.fullname
Copy link
Member

Choose a reason for hiding this comment

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

I believe the order is wrong here: a RefExpr (including MemberExpr) with a non-None full name should always take precedence.

if fname:
hook = self.plugin.get_dynamic_class_hook(fname)
if hook:
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,29 @@ class Instr(Generic[T]): ...
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/dyn_class.py

[case testDynamicClassHookFromClassMethod]
# flags: --config-file tmp/mypy.ini

from mod import QuerySet, Manager

MyManager = Manager.from_queryset(QuerySet)

reveal_type(MyManager()) # N: Revealed type is '__main__.MyManager'
reveal_type(MyManager().attr) # N: Revealed type is 'builtins.str'
Copy link
Member

Choose a reason for hiding this comment

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

Please add a check here that MyManager can be used as an annotation for a function argument.


[file mod.py]
from typing import Generic, TypeVar, Type
class QuerySet:
attr: str
class Manager:
@classmethod
def from_queryset(cls, queryset_cls: Type[QuerySet]): ...

[builtins fixtures/classmethod.pyi]
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/dyn_class_from_method.py

[case testBaseClassPluginHookWorksIncremental]
# flags: --config-file tmp/mypy.ini
import a
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/plugins/dyn_class_from_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from mypy.nodes import (Block, ClassDef, GDEF, SymbolTable, SymbolTableNode, TypeInfo)
from mypy.plugin import DynamicClassDefContext, Plugin
from mypy.types import Instance


class DynPlugin(Plugin):
def get_dynamic_class_hook(self, fullname):
if 'from_queryset' in fullname:
return add_info_hook
return None


def add_info_hook(ctx: DynamicClassDefContext):
class_def = ClassDef(ctx.name, Block([]))
class_def.fullname = ctx.api.qualified_name(ctx.name)

info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id)
class_def.info = info
queryset_type_fullname = ctx.call.args[0].fullname
queryset_info = ctx.api.lookup_fully_qualified_or_none(queryset_type_fullname).node # type: TypeInfo
obj = ctx.api.builtin_type('builtins.object')
info.mro = [info, queryset_info, obj.type]
info.bases = [Instance(queryset_info, [])]
ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))


def plugin(version):
return DynPlugin
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy