Skip to content

[match-case] fix matching against typing.Callable and Protocol types. #19471

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Improved callback test by checking both against concrete implementati…
…on and anonymous `Callable`
  • Loading branch information
randolf-scholz committed Jul 17, 2025
commit c5d34c9a9a0f23e2e09b812846f0d0c5bd5e666a
24 changes: 20 additions & 4 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1091,15 +1091,23 @@ from typing_extensions import Protocol, runtime_checkable
class FnProto(Protocol):
def __call__(self, x: int, /) -> object: ...

class FnImpl:
def __call__(self, x: object, /) -> int: ...

def test_object(x: object) -> None:
match x:
case FnProto() as fn:
reveal_type(fn) # N: Revealed type is "__main__.FnProto"

def test_impl(x: Callable[[int], int]) -> None:
def test_impl(x: FnImpl) -> None:
match x:
case FnProto() as fn:
reveal_type(fn) # N: Revealed type is "def (builtins.int) -> builtins.int"
reveal_type(fn) # N: Revealed type is "__main__.FnImpl"

def test_callable(x: Callable[[object], int]) -> None:
match x:
case FnProto() as fn:
reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"

[builtins fixtures/dict.pyi]

Expand All @@ -1111,15 +1119,23 @@ from typing_extensions import Protocol, runtime_checkable
class AnyCallable(Protocol):
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

class FnImpl:
def __call__(self, x: object, /) -> int: ...

def test_object(x: object) -> None:
match x:
case AnyCallable() as fn:
reveal_type(fn) # N: Revealed type is "__main__.AnyCallable"

def test_impl(x: Callable[[int], int]) -> None:
def test_impl(x: FnImpl) -> None:
match x:
case AnyCallable() as fn:
reveal_type(fn) # N: Revealed type is "def (builtins.int) -> builtins.int"
reveal_type(fn) # N: Revealed type is "__main__.FnImpl"

def test_callable(x: Callable[[object], int]) -> None:
match x:
case AnyCallable() as fn:
reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"

[builtins fixtures/dict.pyi]

Expand Down
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