-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Make overloads support classmethod and staticmethod #5224
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
Changes from all commits
61f7f2a
4947e9f
f0a12e9
81dfa69
bfaba34
563748a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,13 +370,20 @@ def __str__(self) -> str: | |
return 'ImportedName(%s)' % self.target_fullname | ||
|
||
|
||
FUNCBASE_FLAGS = [ | ||
'is_property', 'is_class', 'is_static', | ||
] | ||
|
||
|
||
class FuncBase(Node): | ||
"""Abstract base class for function-like nodes""" | ||
|
||
__slots__ = ('type', | ||
'unanalyzed_type', | ||
'info', | ||
'is_property', | ||
'is_class', # Uses "@classmethod" | ||
'is_static', # USes "@staticmethod" | ||
'_fullname', | ||
) | ||
|
||
|
@@ -391,6 +398,8 @@ def __init__(self) -> None: | |
# TODO: Type should be Optional[TypeInfo] | ||
self.info = cast(TypeInfo, None) | ||
self.is_property = False | ||
self.is_class = False | ||
self.is_static = False | ||
# Name with module prefix | ||
# TODO: Type should be Optional[str] | ||
self._fullname = cast(str, None) | ||
|
@@ -436,8 +445,8 @@ def serialize(self) -> JsonDict: | |
'items': [i.serialize() for i in self.items], | ||
'type': None if self.type is None else self.type.serialize(), | ||
'fullname': self._fullname, | ||
'is_property': self.is_property, | ||
'impl': None if self.impl is None else self.impl.serialize() | ||
'impl': None if self.impl is None else self.impl.serialize(), | ||
'flags': get_flags(self, FUNCBASE_FLAGS), | ||
} | ||
|
||
@classmethod | ||
|
@@ -451,7 +460,7 @@ def deserialize(cls, data: JsonDict) -> 'OverloadedFuncDef': | |
if data.get('type') is not None: | ||
res.type = mypy.types.deserialize_type(data['type']) | ||
res._fullname = data['fullname'] | ||
res.is_property = data['is_property'] | ||
set_flags(res, data['flags']) | ||
# NOTE: res.info will be set in the fixup phase. | ||
return res | ||
|
||
|
@@ -481,9 +490,9 @@ def set_line(self, target: Union[Context, int], column: Optional[int] = None) -> | |
self.variable.set_line(self.line, self.column) | ||
|
||
|
||
FUNCITEM_FLAGS = [ | ||
FUNCITEM_FLAGS = FUNCBASE_FLAGS + [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change adds There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is. I made this change partly on principle since This change also doesn't actually change the serialized output in practice. FuncItem currently has only two subtypes: FuncDef and LambdaExpr. The former subclass previously explicitly set and serialized the |
||
'is_overload', 'is_generator', 'is_coroutine', 'is_async_generator', | ||
'is_awaitable_coroutine', 'is_static', 'is_class', | ||
'is_awaitable_coroutine', | ||
] | ||
|
||
|
||
|
@@ -503,8 +512,6 @@ class FuncItem(FuncBase): | |
'is_coroutine', # Defined using 'async def' syntax? | ||
'is_async_generator', # Is an async def generator? | ||
'is_awaitable_coroutine', # Decorated with '@{typing,asyncio}.coroutine'? | ||
'is_static', # Uses @staticmethod? | ||
'is_class', # Uses @classmethod? | ||
'expanded', # Variants of function with type variables with values expanded | ||
) | ||
|
||
|
@@ -525,8 +532,6 @@ def __init__(self, | |
self.is_coroutine = False | ||
self.is_async_generator = False | ||
self.is_awaitable_coroutine = False | ||
self.is_static = False | ||
self.is_class = False | ||
self.expanded = [] # type: List[FuncItem] | ||
|
||
self.min_args = 0 | ||
|
@@ -547,7 +552,7 @@ def is_dynamic(self) -> bool: | |
|
||
|
||
FUNCDEF_FLAGS = FUNCITEM_FLAGS + [ | ||
'is_decorated', 'is_conditional', 'is_abstract', 'is_property', | ||
'is_decorated', 'is_conditional', 'is_abstract', | ||
] | ||
|
||
|
||
|
@@ -561,7 +566,6 @@ class FuncDef(FuncItem, SymbolNode, Statement): | |
'is_decorated', | ||
'is_conditional', | ||
'is_abstract', | ||
'is_property', | ||
'original_def', | ||
) | ||
|
||
|
@@ -575,7 +579,6 @@ def __init__(self, | |
self.is_decorated = False | ||
self.is_conditional = False # Defined conditionally (within block)? | ||
self.is_abstract = False | ||
self.is_property = False | ||
# Original conditional definition | ||
self.original_def = None # type: Union[None, FuncDef, Var, Decorator] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -470,6 +470,16 @@ def _add_init(ctx: 'mypy.plugin.ClassDefContext', attributes: List[Attribute], | |
func_type = stmt.func.type | ||
if isinstance(func_type, CallableType): | ||
func_type.arg_types[0] = ctx.api.class_type(ctx.cls.info) | ||
if isinstance(stmt, OverloadedFuncDef) and stmt.is_class: | ||
func_type = stmt.type | ||
if isinstance(func_type, Overloaded): | ||
class_type = ctx.api.class_type(ctx.cls.info) | ||
for item in func_type.items(): | ||
item.arg_types[0] = class_type | ||
if stmt.impl is not None: | ||
assert isinstance(stmt.impl, Decorator) | ||
if isinstance(stmt.impl.func.type, CallableType): | ||
stmt.impl.func.type.arg_types[0] = class_type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good that you also take care about plugins. |
||
|
||
|
||
class MethodAdder: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we also update
astdiff.py
according to these flag reshuffling? This may break fine grained incremental mode (in some corner cases).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, good point -- I didn't even know that file existed.
I tried making the change + tried adding an test to one of the fine-grained incremental tests. (I'm pretty unfamiliar with fine-grained incremental stuff though, so let me know if I did it incorrectly.)