Skip to content

Commit 4e8ef02

Browse files
committed
Merge remote-tracking branch 'upstream/main' into concatenate/ellipsis-support-110
2 parents c092d7e + 80958f3 commit 4e8ef02

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
subscripted objects) had wrong parameters if they were directly
1717
subscripted with an `Unpack` object.
1818
Patch by [Daraan](https://github.com/Daraan).
19+
- Backport to Python 3.10 the ability to substitute `...` in generic `Callable`
20+
aliases that have a `Concatenate` special form as their argument.
21+
Patch by [Daraan](https://github.com/Daraan).
1922
- Fix error in subscription of `Unpack` aliases causing nested Unpacks
2023
to not be resolved correctly. Patch by [Daraan](https://github.com/Daraan).
2124
- Extended the `Concatenate` backport for Python 3.8-3.10 to now accept

src/test_typing_extensions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5422,9 +5422,7 @@ def test_invalid_use(self):
54225422
):
54235423
Concatenate[(str,), P]
54245424

5425-
@skipUnless(TYPING_3_11_0,
5426-
"Cannot be backported to <=3.9. See issue #48"
5427-
"Cannot use ... with typing._ConcatenateGenericAlias after 3.10.2")
5425+
@skipUnless(TYPING_3_10_0, "Missing backport to <=3.9. See issue #48")
54285426
def test_alias_subscription_with_ellipsis(self):
54295427
P = ParamSpec('P')
54305428
X = Callable[Concatenate[int, P], Any]

src/typing_extensions.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,26 @@ def __parameters__(self):
17991799
else:
18001800
_ConcatenateGenericAlias = typing._ConcatenateGenericAlias
18011801

1802+
# 3.10
1803+
if sys.version_info < (3, 11):
1804+
_typing_ConcatenateGenericAlias = _ConcatenateGenericAlias
1805+
1806+
1807+
class _ConcatenateGenericAlias(_typing_ConcatenateGenericAlias, _root=True):
1808+
# needed for checks in collections.abc.Callable to accept this class
1809+
__module__ = "typing"
1810+
1811+
def copy_with(self, params):
1812+
if isinstance(params[-1], (list, tuple)):
1813+
return (*params[:-1], *params[-1])
1814+
if isinstance(params[-1], _ConcatenateGenericAlias):
1815+
params = (*params[:-1], *params[-1].__args__)
1816+
elif not (params[-1] is ... or isinstance(params[-1], ParamSpec)):
1817+
raise TypeError("The last parameter to Concatenate should be a "
1818+
"ParamSpec variable or ellipsis.")
1819+
return super(_typing_ConcatenateGenericAlias, self).copy_with(params)
1820+
1821+
18021822
# 3.8-3.9.2
18031823
class _EllipsisDummy: ...
18041824

@@ -1842,7 +1862,7 @@ def _concatenate_getitem(self, parameters):
18421862
return _create_concatenate_alias(self, parameters)
18431863

18441864
# 3.11+; Concatenate does not accept ellipsis in 3.10
1845-
if hasattr(typing, 'Concatenate') and sys.version_info >= (3, 11):
1865+
if sys.version_info >= (3, 11):
18461866
Concatenate = typing.Concatenate
18471867
# 3.9-3.10
18481868
elif sys.version_info[:2] >= (3, 9):

0 commit comments

Comments
 (0)