Skip to content

Commit a642784

Browse files
FuegoFromsullivan
authored andcommitted
Don't resolve Callable NamedTuple fields to their return type (python#6576)
Don't do self binding in `find_node_type` unless `is_initialized_in_class`. This matches the behavior of checkmember. Fixes python#6575.
1 parent 22cd63a commit a642784

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

mypy/subtypes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,10 @@ def find_node_type(node: Union[Var, FuncBase], itype: Instance, subtype: Type) -
619619
if typ is None:
620620
return AnyType(TypeOfAny.from_error)
621621
# We don't need to bind 'self' for static methods, since there is no 'self'.
622-
if isinstance(node, FuncBase) or isinstance(typ, FunctionLike) and not node.is_staticmethod:
622+
if (isinstance(node, FuncBase)
623+
or (isinstance(typ, FunctionLike)
624+
and node.is_initialized_in_class
625+
and not node.is_staticmethod)):
623626
assert isinstance(typ, FunctionLike)
624627
signature = bind_self(typ, subtype)
625628
if node.is_property:

test-data/unit/check-protocols.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,3 +2444,29 @@ class P(Protocol[T]):
24442444
@alias
24452445
def meth(self, arg: T) -> T: ...
24462446
[out]
2447+
2448+
[case testNamedTupleWithNoArgsCallableField]
2449+
from typing import Callable, NamedTuple, Protocol
2450+
2451+
class N(NamedTuple):
2452+
func: Callable[[], str]
2453+
2454+
class P(Protocol):
2455+
@property
2456+
def func(self) -> Callable[[], str]: ...
2457+
2458+
p: P = N(lambda: 'foo')
2459+
[builtins fixtures/property.pyi]
2460+
2461+
[case testNamedTupleWithManyArgsCallableField]
2462+
from typing import Callable, NamedTuple, Protocol
2463+
2464+
class N(NamedTuple):
2465+
func: Callable[[str, str, str], str]
2466+
2467+
class P(Protocol):
2468+
@property
2469+
def func(self) -> Callable[[str, str, str], str]: ...
2470+
2471+
p: P = N(lambda a, b, c: 'foo')
2472+
[builtins fixtures/property.pyi]

0 commit comments

Comments
 (0)
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