diff --git a/mypy/subtypes.py b/mypy/subtypes.py index f6a0b52b0028..8c08ea873b6c 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -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: diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 0456ab31bada..c4c37aaa5eec 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -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]
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: