Skip to content

Don't resolve Callable NamedTuple fields to their return type #6576

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

Merged
merged 2 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Instead use is_initialized_in_class to skip the entire 'self' bindi…
…ng. Add tests.
  • Loading branch information
FuegoFro committed Mar 21, 2019
commit a25c96875e2c8bdf85eeed3868ac885e17787ee5
7 changes: 5 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,13 @@ 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 and not node.info.is_named_tuple:
if node.is_property:
assert isinstance(signature, CallableType)
typ = signature.ret_type
else:
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]
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