Skip to content

Commit 6d88431

Browse files
carljmgvanrossum
authored andcommitted
Correct the type of MethodType.__func__. (#1383)
See python/mypy#3484 for background.
1 parent d25c5b9 commit 6d88431

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

stdlib/3/types.pyi

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,23 @@ class CoroutineType:
121121
@overload
122122
def throw(self, typ: type, val: BaseException = ..., tb: 'TracebackType' = ...) -> Any: ...
123123

124+
class _StaticFunctionType:
125+
"""Fictional type to correct the type of MethodType.__func__.
126+
127+
FunctionType is a descriptor, so mypy follows the descriptor protocol and
128+
converts MethodType.__func__ back to MethodType (the return type of
129+
FunctionType.__get__). But this is actually a special case; MethodType is
130+
implemented in C and its attribute access doesn't go through
131+
__getattribute__.
132+
133+
By wrapping FunctionType in _StaticFunctionType, we get the right result;
134+
similar to wrapping a function in staticmethod() at runtime to prevent it
135+
being bound as a method.
136+
"""
137+
def __get__(self, obj: Optional[object], type: Optional[type]) -> 'FunctionType': ...
138+
124139
class MethodType:
125-
__func__ = ... # type: FunctionType
140+
__func__ = ... # type: _StaticFunctionType
126141
__self__ = ... # type: object
127142
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
128143
class BuiltinFunctionType:

0 commit comments

Comments
 (0)