-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invoke get_dynamic_class_hook on method calls (#7990)
Fixes #7266 Note: a RefExpr (including MemberExpr) with a non-None full name should always take precedence.
- Loading branch information
1 parent
fffb4b4
commit 20b60b5
Showing
3 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |