Skip to content

Commit aa3b783

Browse files
committed
fix collections.abc.Callable and typing.Callable
1 parent b0401bc commit aa3b783

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Callable
4+
from typing_extensions import assert_type
5+
6+
assert_type(Callable[[], None], type[Callable[[], None]])
7+
8+
9+
def f(c: Callable[[], None]) -> None:
10+
assert_type(c.__call__, Callable[[], None])
11+
12+
13+
class C(Callable[[], None]):
14+
def __call__(self) -> None: ...
15+
16+
17+
isinstance(C(), Callable)

stdlib/_collections_abc.pyi

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from abc import abstractmethod
2+
from abc import ABCMeta, abstractmethod
33
from types import MappingProxyType
44
from typing import ( # noqa: Y022,Y038,UP035,Y057,RUF100
55
AbstractSet as Set,
@@ -8,7 +8,6 @@ from typing import ( # noqa: Y022,Y038,UP035,Y057,RUF100
88
AsyncIterator as AsyncIterator,
99
Awaitable as Awaitable,
1010
ByteString as ByteString,
11-
Callable as Callable,
1211
ClassVar,
1312
Collection as Collection,
1413
Container as Container,
@@ -25,6 +24,7 @@ from typing import ( # noqa: Y022,Y038,UP035,Y057,RUF100
2524
MutableMapping as MutableMapping,
2625
MutableSequence as MutableSequence,
2726
MutableSet as MutableSet,
27+
ParamSpec,
2828
Protocol,
2929
Reversible as Reversible,
3030
Sequence as Sequence,
@@ -68,6 +68,16 @@ if sys.version_info >= (3, 12):
6868
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
6969
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
7070

71+
_R_co = TypeVar("_R_co", covariant=True) # return type for Callable
72+
_P = ParamSpec("_P")
73+
74+
@runtime_checkable
75+
class Callable(Protocol[_P, _R_co], metaclass=ABCMeta):
76+
__slots__ = ()
77+
78+
@abstractmethod
79+
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R_co: ...
80+
7181
@final
7282
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
7383
def __eq__(self, value: object, /) -> bool: ...

stdlib/typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ class _SpecialForm(_Final):
233233

234234
Union: _SpecialForm
235235
Protocol: _SpecialForm
236-
Callable: _SpecialForm
237236
Type: _SpecialForm
238237
NoReturn: _SpecialForm
239238
ClassVar: _SpecialForm
@@ -427,6 +426,7 @@ class _Alias:
427426
# Class for defining generic aliases for library types.
428427
def __getitem__(self, typeargs: Any) -> Any: ...
429428

429+
Callable = _Alias()
430430
List = _Alias()
431431
Dict = _Alias()
432432
DefaultDict = _Alias()

0 commit comments

Comments
 (0)