8000 More principled approach for callable vs callable inference by ilevkivskyi · Pull Request #15910 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

More principled approach for callable vs callable inference #15910

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 6 commits into from
Aug 21, 2023
Merged
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
Add more tests
  • Loading branch information
ilevkivskyi committed Aug 21, 2023
commit cb35b171da3f1a28faf52e094281aa17d6626b21
64 changes: 64 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3622,3 +3622,67 @@ def f(x: Call[T, S]) -> Tuple[T, S]: ...
def g(*, y: int, x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[builtins.str, builtins.int]"
[builtins fixtures/list.pyi]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test a few cases where we shouldn't be inferring constraints, e.g. positional-only vs keyword-only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, added such tests (note btw if there are no constraints, it now means they can never by subtypes, so there will be also an error in each test).


[case testCallableInferenceAgainstCallablePosOnlyVsNamed]
from typing import TypeVar, Callable, Tuple, Protocol

T = TypeVar('T', contravariant=True)
S = TypeVar('S', contravariant=True)

class Call(Protocol[T]):
def __call__(self, *, x: T) -> None: ...

def f(x: Call[T]) -> Tuple[T, T]: ...

def g(__x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[<nothing>, <nothing>]" \
# E: Argument 1 to "f" has incompatible type "Callable[[str], None]"; expected "Call[<nothing>]"
[builtins fixtures/list.pyi]

[case testCallableInferenceAgainstCallableNamedVsPosOnly]
from typing import TypeVar, Callable, Tuple, Protocol

T = TypeVar('T', contravariant=True)
S = TypeVar('S', contravariant=True)

class Call(Protocol[T]):
def __call__(self, __x: T) -> None: ...

def f(x: Call[T]) -> Tuple[T, T]: ...

def g(*, x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[<nothing>, <nothing>]" \
# E: Argument 1 to "f" has incompatible type "Callable[[NamedArg(str, 'x')], None]"; expected "Call[<nothing>]"
[builtins fixtures/list.pyi]

[case testCallableInferenceAgainstCallablePosOnlyVsKwargs]
from typing import TypeVar, Callable, Tuple, Protocol

T = TypeVar('T', contravariant=True)
S = TypeVar('S', contravariant=True)

class Call(Protocol[T]):
def __call__(self, __x: T) -> None: ...

def f(x: Call[T]) -> Tuple[T, T]: ...

def g(**x: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[<nothing>, <nothing>]" \
# E: Argument 1 to "f" has incompatible type "Callable[[KwArg(str)], None]"; expected "Call[<nothing>]"
[builtins fixtures/list.pyi]

[case testCallableInferenceAgainstCallableNamedVsArgs]
from typing import TypeVar, Callable, Tuple, Protocol

T = TypeVar('T', contravariant=True)
S = TypeVar('S', contravariant=True)

class Call(Protocol[T]):
def __call__(self, *, x: T) -> None: ...

def f(x: Call[T]) -> Tuple[T, T]: ...

def g(*args: str) -> None: pass
reveal_type(f(g)) # N: Revealed type is "Tuple[<nothing>, <nothing>]" \
# E: Argument 1 to "f" has incompatible type "Callable[[VarArg(str)], None]"; expected "Call[<nothing>]"
[builtins fixtures/list.pyi]
0