8000 bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing by Fidget-Spinner · Pull Request #23702 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing #23702

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 27 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
219b4ee
Add ParamSpec and Concatenate
Fidget-Spinner Dec 8, 2020
a1c0d0a
support ParamSpec in generics
Fidget-Spinner Dec 8, 2020
7b3beab
Add typing tests, disallow Concatenate in other types
Fidget-Spinner Dec 9, 2020
5dd3b44
Add news
Fidget-Spinner Dec 9, 2020
59c0b20
Address some of Guido's review comments
Fidget-Spinner Dec 10, 2020
4c381b3
remove extraneous empty lines
Fidget-Spinner Dec 10, 2020
b36b62d
Support ParamSpec in __parameters__ of typing and builtin GenericAlias
Fidget-Spinner Dec 10, 2020
d09d088
add tests for user defined generics
Fidget-Spinner Dec 10, 2020
0a19f34
Merge remote-tracking branch 'upstream/master' into pep612
Fidget-Spinner Dec 14, 2020
9727e2a
cast list to tuple done, loosened type checks for Generic
Fidget-Spinner Dec 14, 2020
cc7fc1c
loosen generics, allow typevar-like subst, flatten out args if Callable
Fidget-Spinner Dec 15, 2020
3e67f23
fix whitespace issue, cast list to tuple for types.GenericAlias
Fidget-Spinner Dec 15, 2020
d9baa1a
convert list to tuples if substituting paramspecs in types.GenericAlias
Fidget-Spinner Dec 16, 2020
c4155b6
done! flattened __args__ in substitutions for collections.abc.Callable
Fidget-Spinner Dec 16, 2020
2dbf861
fix repr problems, add repr tests
Fidget-Spinner Dec 16, 2020
87c2d19
Add another test for multiple chaining
Fidget-Spinner Dec 16, 2020
2b09de6
fix typo
Fidget-Spinner Dec 16, 2020
d980702
Clean up some comments
Fidget-Spinner Dec 17, 2020
45f7894
Merge remote-tracking branch 'upstream/master' into pep612
Fidget-Spinner Dec 22, 2020
d6f777c
remove stray whitespace
Fidget-Spinner Dec 22, 2020
9a8176b
Address nearly all of Guido's reviews
Fidget-Spinner Dec 23, 2020
fa06838
more reviews; fix some docstrings, clean up code, cast list to tuple …
Fidget-Spinner Dec 23, 2020
b8672cd
remove uneeded tests copied over from typevar
Fidget-Spinner Dec 23, 2020
51a463c
remove unused variable
Fidget-Spinner Dec 23, 2020
6d5b754
Merge remote-tracking branch 'upstream/master' into pep612
Fidget-Spinner Dec 24, 2020
c05d5d7
merge length checking into _has_special_args too
Fidget-Spinner Dec 24, 2020
c49ba30
Update Lib/_collections_abc.py
gvanrossum Dec 24, 2020
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
more reviews; fix some docstrings, clean up code, cast list to tuple …
…by default
  • Loading branch information
Fidget-Spinner committed Dec 23, 2020
commit fa06838ce83cf72aecc1db5e3fb4fe59f3925b70
4 changes: 2 additions & 2 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ def __getitem__(self, item):


def _has_special_args(obj):
"""Checks if obj matches either ``...``, 'ParamSpec' or
'_ConcatenateGenericAlias' from typing.py
"""Checks if obj matches either ``...``, ``ParamSpec`` or
``_ConcatenateGenericAlias`` from typing.py
"""
if obj is Ellipsis:
return True
Expand Down
13 changes: 6 additions & 7 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,10 @@ class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
Parameter specification variables exist primarily for the benefit of static
type checkers. They are used to forward the parameter types of one
Callable to another Callable, a pattern commonly found in higher order
functions and decorators. They are only valid as the first argument to
Callable, or as parameters for user-defined Generics. See class Generic
for more information on generic types. An example for annotating a
decorator::
functions and decorators. They are only valid when used in Concatenate, or
as the first argument to Callable, or as parameters for user-defined Generics.
See class Generic for more information on generic types. An example for
annotating a decorator::

T = TypeVar('T')
P = ParamSpec('P')
Expand Down Expand Up @@ -904,7 +904,7 @@ def __getitem__(self, params):
subst = dict(zip(self.__parameters__, params))
new_args = []
for arg in self.__args__:
if isinstance(arg, (TypeVar, ParamSpec)):
if isinstance(arg, _TypeVarLike):
arg = subst[arg]
elif isinstance(arg, (_GenericAlias, GenericAlias)):
subparams = arg.__parameters__
Expand Down Expand Up @@ -1150,7 +1150,7 @@ def __class_getitem__(cls, params):
params = tuple(_type_convert(p) for p in params)
if cls in (Generic, Protocol):
# Generic and Protocol can only be subscripted with unique type variables.
if not all(isinstance(p, (TypeVar, ParamSpec)) for p in params):
if not all(isinstance(p, _TypeVarLike) for p in params):
raise TypeError(
f"Parameters to {cls.__name__}[...] must all be type variables "
f"or parameter specification variables.")
Expand All @@ -1159,7 +1159,6 @@ def __class_getitem__(cls, params):
f"Parameters to {cls.__name__}[...] must all be unique")
else:
# Subscripting a regular Generic subclass.

if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
params = _prepare_paramspec_params(cls, params)
_check_generic(cls, params, len(cls.__parameters__))
Expand Down
35 changes: 18 additions & 17 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,20 @@ ga_repr(PyObject *self)
* If any one of the names are found, return 1, else 0.
**/
static inline int
is_typing_names(PyObject *obj, int num, ...)
is_typing_name(PyObject *obj, int num, ...)
{
va_list names;
va_start(names, num);

PyTypeObject *type = Py_TYPE(obj);
int cmp = 1;
for (int i = 0; i < num && cmp != 0; ++i) {
cmp &= strcmp(type->tp_name, va_arg(names, const char *));
int hit = 0;
for (int i = 0; i < num; ++i) {
if (!strcmp(type->tp_name, va_arg(names, const char *))) {
hit = 1;
break;
}
}
if (cmp != 0) {
if (!hit) {
return 0;
}
PyObject *module = PyObject_GetAttrString((PyObject *)type, "__module__");
Expand All @@ -190,13 +193,13 @@ is_typing_names(PyObject *obj, int num, ...)
static inline int
is_typevarlike(PyObject *obj)
{
return is_typing_names(obj, 2, "TypeVar", "ParamSpec");
return is_typing_name(obj, 2, "TypeVar", "ParamSpec");
}

static inline int
is_paramspec(PyObject *obj)
{
return is_typing_names(obj, 1, "ParamSpec");
return is_typing_name(obj, 1, "ParamSpec");
}

// Index of item in self[:len], or -1 if not found (self is a tuple)
Expand Down Expand Up @@ -302,17 +305,17 @@ subs_tvars(PyObject *obj, PyObject *params, PyObject **argitems)
PyObject *subst = arg;
Py_ssize_t iparam = tuple_index(params, nparams, arg);
if (iparam >= 0) {
subst = argitems[iparam];
arg = argitems[iparam];
}
// convert all the lists inside args to tuples to help
// with caching in other libaries if substituting a ParamSpec
if (PyList_CheckExact(subst) && is_paramspec(arg)) {
subst = PyList_AsTuple(subst);
// with caching in other libaries
if (PyList_CheckExact(arg)) {
arg = PyList_AsTuple(arg);
}
else {
Py_INCREF(subst);
Py_INCREF(arg);
}
PyTuple_SET_ITEM(subargs, i, subst);
PyTuple_SET_ITEM(subargs, i, arg);
}

obj = PyObject_GetItem(obj, subargs);
Expand Down Expand Up @@ -374,7 +377,6 @@ ga_getitem(PyObject *self, PyObject *item)
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
int typevar = is_typevarlike(arg);
int paramspec = is_paramspec(arg);
if (typevar < 0) {
Py_DECREF(newargs);
return NULL;
Expand All @@ -383,9 +385,8 @@ ga_getitem(PyObject *self, PyObject *item)
Py_ssize_t iparam = tuple_index(alias->parameters, nparams, arg);
assert(iparam >= 0);
arg = argitems[iparam];
// If substituting a ParamSpec, convert lists to tuples to help
// with caching in other libaries.
if (PyList_CheckExact(arg) && paramspec) {
// convert lists to tuples to help with caching in other libaries.
if (PyList_CheckExact(arg)) {
arg = PyList_AsTuple(arg);
}
else {
Expand Down
0