Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 12 additions & 12 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ def __subclasshook__(cls, C):


class _CallableGenericAlias(GenericAlias):
""" Represent `Callable[argtypes, resulttype]`.
""" Represent `Callable[paramtypes, returntype]`.

This sets ``__args__`` to a tuple containing the flattened ``argtypes``
followed by ``resulttype``.
This sets ``__args__`` to a tuple containing the flattened ``paramtypes``
followed by ``returntype``.

Example: ``Callable[[int, str], float]`` sets ``__args__`` to
``(int, str, float)``.
Expand All @@ -473,13 +473,13 @@ class _CallableGenericAlias(GenericAlias):
def __new__(cls, origin, args):
if not (isinstance(args, tuple) and len(args) == 2):
raise TypeError(
"Callable must be used as Callable[[arg, ...], result].")
t_args, t_result = args
if isinstance(t_args, (tuple, list)):
args = (*t_args, t_result)
elif not _is_param_expr(t_args):
"Callable must be used as Callable[[paramtype1, paramtype2, ...], returntype].")
t_params, t_return = args
if isinstance(t_params, (tuple, list)):
args = (*t_params, t_return)
elif not _is_param_expr(t_params):
raise TypeError(f"Expected a list of types, an ellipsis, "
f"ParamSpec, or Concatenate. Got {t_args}")
f"ParamSpec, or Concatenate. Got {t_params}")
return super().__new__(cls, origin, args)

def __repr__(self):
Expand Down Expand Up @@ -508,9 +508,9 @@ def __getitem__(self, item):

# args[0] occurs due to things like Z[[int, str, bool]] from PEP 612
if not isinstance(new_args[0], (tuple, list)):
t_result = new_args[-1]
t_args = new_args[:-1]
new_args = (t_args, t_result)
t_params = new_args[:-1]
t_return = new_args[-1]
new_args = (t_params, t_return)
return _CallableGenericAlias(Callable, tuple(new_args))

def _is_param_expr(obj):
Expand Down
12 changes: 6 additions & 6 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2831,15 +2831,15 @@ class Other(Leaf): # Error reported by type checker
Callable.__doc__ = \
"""Deprecated alias to collections.abc.Callable.

Callable[[int], str] signifies a function that takes a single
Callable[[int], str] signifies a function that has a single
parameter of type int and returns a str.

The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
The type specification "[]" must have two objects, a parameter
type list and return type. The parameter type list must be a list
of types, ParamSpec, Concatenate or ellipsis "...".
The return type must be a single type.

There is no syntax to indicate optional or keyword arguments;
There is no syntax to indicate optional or keyword parameters;
such function types are rarely used as callback types.
"""
AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct _CallableGenericAlias class and typing.Callable docstring
Loading