Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-91162: Support splitting of unpacked arbitrary-length tuple over TypeVar and TypeVarTuple parameters (alt)#93412

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
cfb43b2
gh-91162: Support substitution of TypeVar with an unpacked variable-s…
serhiy-storchakaMay 29, 2022
c638e0e
The C implementation.
serhiy-storchakaMay 29, 2022
731d5fb
Remove unused code.
serhiy-storchakaMay 29, 2022
03da794
Refactoring.
serhiy-storchakaMay 30, 2022
b497df5
Merge branch 'main' into typing-subst-unpacked-vat-tuple
serhiy-storchakaMay 31, 2022
55c4efb
Add a NEWS entry.
serhiy-storchakaJun 1, 2022
7dcf277
Merge branch 'main' into typing-subst-unpacked-vat-tuple
serhiy-storchakaJun 1, 2022
56f02d8
Merge branch 'main' into typing-subst-unpacked-vat-tuple
JelleZijlstraJun 4, 2022
b091d97
Merge branch 'main' into typing-subst-unpacked-vat-tuple
serhiy-storchakaJun 5, 2022
40ba6ca
Merge remote-tracking branch 'origin/typing-subst-unpacked-vat-tuple'…
serhiy-storchakaJun 5, 2022
a7efb91
Move the C code to Python.
serhiy-storchakaMay 30, 2022
c3869bd
Merge branch 'main' into typing-subst-unpacked-var-tuple2
serhiy-storchakaJun 12, 2022
ae0b4d6
Edit the NEWS entry.
serhiy-storchakaJun 12, 2022
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
PrevPrevious commit
NextNext commit
Refactoring.
  • Loading branch information
@serhiy-storchaka
serhiy-storchaka committedMay 30, 2022
commit03da7944672bf3bd3fece85df6bcd4d63056d5d5
3 changes: 2 additions & 1 deletionLib/test/test_typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -753,10 +753,11 @@ class C(Generic[*Ts]): pass
('generic[*Ts]', '[*tuple_type[int]]', 'generic[int]'),
('generic[*Ts]', '[*tuple_type[*Ts]]', 'generic[*Ts]'),
('generic[*Ts]', '[*tuple_type[int, str]]', 'generic[int, str]'),
('generic[*Ts]', '[str, *tuple_type[int, ...], bool]', 'generic[str, *tuple_type[int, ...], bool]'),
('generic[*Ts]', '[tuple_type[int, ...]]', 'generic[tuple_type[int, ...]]'),
('generic[*Ts]', '[tuple_type[int, ...], tuple_type[str, ...]]', 'generic[tuple_type[int, ...], tuple_type[str, ...]]'),
('generic[*Ts]', '[*tuple_type[int, ...]]', 'generic[*tuple_type[int, ...]]'),
('generic[*Ts]', '[str,*tuple_type[int, ...],bool]', 'generic[str,*tuple_type[int, ...], bool]'),
('generic[*Ts]', '[*tuple_type[int, ...], *tuple_type[str, ...]]', 'TypeError'),

('generic[*Ts]', '[*Ts]', 'generic[*Ts]'),
('generic[*Ts]', '[T, *Ts]', 'generic[T, *Ts]'),
Expand Down
58 changes: 7 additions & 51 deletionsLib/typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -906,14 +906,6 @@ def _is_unpacked_typevartuple(x: Any) -> bool:
return ((not isinstance(x, type)) and
getattr(x, '__typing_is_unpacked_typevartuple__', False))

def _is_unpacked_var_tuple(x: Any) -> bool:
if isinstance(x, type) and not isinstance(x, GenericAlias):
return False
args = getattr(x, '__typing_unpacked_tuple_args__', None)
if args and args[-1] is ...:
return True
return False


def _is_typevar_like(x: Any) -> bool:
return isinstance(x, (TypeVar, ParamSpec)) or _is_unpacked_typevartuple(x)
Expand DownExpand Up@@ -1263,44 +1255,6 @@ def __dir__(self):
+ [attr for attr in dir(self.__origin__) if not _is_dunder(attr)]))


def _is_unpacked_tuple(x: Any) -> bool:
# Is `x` something like `*tuple[int]` or `*tuple[int, ...]`?
if not isinstance(x, _UnpackGenericAlias):
return False
# Alright, `x` is `Unpack[something]`.

# `x` will always have `__args__`, because Unpack[] and Unpack[()]
# aren't legal.
unpacked_type = x.__args__[0]

return getattr(unpacked_type, '__origin__', None) is tuple


def _is_unpacked_arbitrary_length_tuple(x: Any) -> bool:
if not _is_unpacked_tuple(x):
return False
unpacked_tuple = x.__args__[0]

if not hasattr(unpacked_tuple, '__args__'):
# It's `Unpack[tuple]`. We can't make any assumptions about the length
# of the tuple, so it's effectively an arbitrary-length tuple.
return True

tuple_args = unpacked_tuple.__args__
if not tuple_args:
# It's `Unpack[tuple[()]]`.
return False

last_arg = tuple_args[-1]
if last_arg is Ellipsis:
# It's `Unpack[tuple[something, ...]]`, which is arbitrary-length.
return True

# If the arguments didn't end with an ellipsis, then it's not an
# arbitrary-length tuple.
return False


# Special typing constructs Union, Optional, Generic, Callable and Tuple
# use three special attributes for internal bookkeeping of generic types:
# * __parameters__ is a tuple of unique free type parameters of a generic
Expand DownExpand Up@@ -1433,11 +1387,13 @@ def _determine_new_args(self, args):
right = plen - typevartuple_index - 1
var_tuple_index = None
for k, arg in enumerate(args):
if _is_unpacked_var_tuple(arg):
if var_tuple_index is not None:
raise TypeError("More than one unpacked variable-size tuple argument")
var_tuple_index = k
fillarg = args[var_tuple_index].__typing_unpacked_tuple_args__[0]
if not (isinstance(arg, type) and not isinstance(arg, GenericAlias)):
subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
if subargs and len(subargs) == 2 and subargs[-1] is ...:
if var_tuple_index is not None:
raise TypeError("More than one unpacked arbitrary-length tuple argument")
var_tuple_index = k
fillarg = subargs[0]
if var_tuple_index is not None:
left = min(left, var_tuple_index)
right = min(right, alen - var_tuple_index - 1)
Expand Down
2 changes: 1 addition & 1 deletionObjects/genericaliasobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -469,7 +469,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
Py_DECREF(fillarg);
Py_DECREF(item);
return PyErr_Format(PyExc_TypeError,
"More than one unpackedvariable-size tuple argument",
"More than one unpackedarbitrary-length tuple argument",
self);
}
vartuplearg = i;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp