Skip to content

Commit

Permalink
Don't resolve Callable NamedTuple fields to their return type (#6576)
Browse files Browse the repository at this point in the history
Don't do self binding in `find_node_type` unless `is_initialized_in_class`.
This matches the behavior of checkmember.

Fixes #6575.
  • Loading branch information
FuegoFro authored and msullivan committed Mar 21, 2019
1 parent 22cd63a commit a642784
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,10 @@ def find_node_type(node: Union[Var, FuncBase], itype: Instance, subtype: Type) -
if typ is None:
return AnyType(TypeOfAny.from_error)
# We don't need to bind 'self' for static methods, since there is no 'self'.
if isinstance(node, FuncBase) or isinstance(typ, FunctionLike) and not node.is_staticmethod:
if (isinstance(node, FuncBase)
or (isinstance(typ, FunctionLike)
and node.is_initialized_in_class
and not node.is_staticmethod)):
assert isinstance(typ, FunctionLike)
signature = bind_self(typ, subtype)
if node.is_property:
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -2444,3 +2444,29 @@ class P(Protocol[T]):
@alias
def meth(self, arg: T) -> T: ...
[out]

[case testNamedTupleWithNoArgsCallableField]
from typing import Callable, NamedTuple, Protocol

class N(NamedTuple):
func: Callable[[], str]

class P(Protocol):
@property
def func(self) -> Callable[[], str]: ...

p: P = N(lambda: 'foo')
[builtins fixtures/property.pyi]

[case testNamedTupleWithManyArgsCallableField]
from typing import Callable, NamedTuple, Protocol

class N(NamedTuple):
func: Callable[[str, str, str], str]

class P(Protocol):
@property
def func(self) -> Callable[[str, str, str], str]: ...

p: P = N(lambda a, b, c: 'foo')
[builtins fixtures/property.pyi]

0 comments on commit a642784

Please sign in to comment.