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
NextNext commit
gh-91162: Support substitution of TypeVar with an unpacked variable-s…
…ize tupleFor example:  A[T, *Ts][*tuple[int, ...]] -> A[int, *tuple[int, ...]]  A[*Ts, T][*tuple[int, ...]] -> A[*tuple[int, ...], int]
  • Loading branch information
@serhiy-storchaka
serhiy-storchaka committedMay 29, 2022
commitcfb43b27befa05d17e465cd2bb7419cfe7ab20f5
11 changes: 8 additions & 3 deletionsLib/test/test_typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -756,11 +756,12 @@ class C(Generic[*Ts]): pass
('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]'),

# Technically, multiple unpackings are forbidden by PEP 646, but we
# choose to be less restrictive at runtime, to allow folks room
# to experiment. So all three of these should be valid.
('generic[*Ts]', '[*tuple_type[int, ...], *tuple_type[str, ...]]', 'generic[*tuple_type[int, ...], *tuple_type[str, ...]]'),
#('generic[*Ts]', '[*tuple_type[int, ...], *tuple_type[str, ...]]', 'generic[*tuple_type[int, ...], *tuple_type[str, ...]]'),

('generic[*Ts]', '[*Ts]', 'generic[*Ts]'),
('generic[*Ts]', '[T, *Ts]', 'generic[T, *Ts]'),
Expand All@@ -769,11 +770,15 @@ class C(Generic[*Ts]): pass
('generic[T, *Ts]', '[int, str]', 'generic[int, str]'),
('generic[T, *Ts]', '[int, str, bool]', 'generic[int, str, bool]'),

('generic[T, *Ts]', '[*tuple[int, ...]]', 'TypeError'), # Should be generic[int, *tuple[int, ...]]
#('generic[T, *Ts]', '[*tuple[int, ...]]', 'TypeError'), # Should be generic[int, *tuple[int, ...]]
('C[T, *Ts]', '[*tuple_type[int, ...]]', 'C[int, *tuple_type[int, ...]]'),
('C[*Ts, T]', '[*tuple_type[int, ...]]', 'C[*tuple_type[int, ...], int]'),
('C[T1, *Ts, T2]', '[*tuple_type[int, ...]]', 'C[int, *tuple_type[int, ...], int]'),


('generic[*Ts, T]', '[int]', 'generic[int]'),
('generic[*Ts, T]', '[int, str]', 'generic[int, str]'),
('generic[*Ts, T]','[int, str, bool]', 'generic[int, str, bool]'),
('generic[*Ts, T]', '[int, str, bool]', 'generic[int, str, bool]'),

('generic[T, *tuple_type[int, ...]]', '[str]', 'generic[str, *tuple_type[int, ...]]'),
('generic[T1, T2, *tuple_type[int, ...]]', '[str, bool]', 'generic[str, bool, *tuple_type[int, ...]]'),
Expand Down
41 changes: 37 additions & 4 deletionsLib/typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -906,6 +906,14 @@ 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@@ -1422,13 +1430,28 @@ def _determine_new_args(self, args):
plen = len(params)
if typevartuple_index is not None:
i = typevartuple_index
j = alen - (plen - i - 1)
if j < i:
j = 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 var_tuple_index is not None:
i = min(i, var_tuple_index)
j = min(j, alen - var_tuple_index - 1)
elif i + j > alen:
raise TypeError(f"Too few arguments for {self};"
f" actual {alen}, expected at least {plen-1}")

new_arg_by_param.update(zip(params[:i], args[:i]))
new_arg_by_param[params[i]] = tuple(args[i: j])
new_arg_by_param.update(zip(params[i + 1:], args[j:]))
for k in range(i, typevartuple_index):
new_arg_by_param[params[k]] = fillarg
new_arg_by_param[params[typevartuple_index]] = tuple(args[i: alen - j])
for k in range(typevartuple_index + 1, plen - j):
new_arg_by_param[params[k]] = fillarg
new_arg_by_param.update(zip(params[plen - j:], args[alen - j:]))
else:
if alen != plen:
raise TypeError(f"Too {'many' if alen > plen else 'few'} arguments for {self};"
Expand DownExpand Up@@ -1760,6 +1783,16 @@ def __typing_is_unpacked_typevartuple__(self):
assert len(self.__args__) == 1
return isinstance(self.__args__[0], TypeVarTuple)

@property
def __typing_is_unpacked_var_tuple__(self):
assert self.__origin__ is Unpack
assert len(self.__args__) == 1
arg, = self.__args__
if isinstance(arg, _GenericAlias):
assert arg.__origin__ is tuple
return len(arg.__args__) >= 2 and arg.__args__[-1] is ...
return False


class Generic:
"""Abstract base class for generic types.
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp