Repository navigation
bpo-43224: Draft implementation of PEP 646 #30398
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
64abc0a
PEP 646: Grammar and compiler changes
mrahtz b725671
PEP 646: Add tests for syntax changes
mrahtz ea329b9
PEP 646: Add AST tests for *args annotations
mrahtz 681dc4e
PEP 646: Add AST tests for return types with unpacking
mrahtz 69f575f
PEP 646: Add AST tests for variable annotations with unpacking
mrahtz 095a620
PEP 646: Add support for starring tuple types
mrahtz 6713748
PEP 646: Add tests for starring tuple types
mrahtz 038b8f0
PEP 646: Implement support in typing.py
mrahtz f028339
PEP 646: Add tests for everything in typing.py
mrahtz 38c17d3
PEP 646: Add tests for typing.get_{origin,args}
mrahtz 9991937
PEP 646: Various fixes to genericaliasobject.c
mrahtz 9017d22
PEP 646: Fix test_pep646_syntax import
mrahtz 07ed599
PEP 646: Add docstring for TypeVarTuple
mrahtz 74e5252
PEP 646: Swap a Py_INCREF with a Py_NewRef
mrahtz 6cb3aa5
PEP 646: Add test for assigning to a slice containing a star
mrahtz daef59b
📜🤖 Added by blurb_it.
blurb-it[bot] 3c2a7e7
PEP 646: Add line to Doc/whatsnew
mrahtz 456156a
PEP 646: Use stdbool.h; separate test cases
mrahtz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PEP 646: Add tests for syntax changes
- Loading branch information
commit b725671a14dd66d48a537a9b1e201561d7b5f7ea
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| doctests = """ | ||
|
|
||
| Setup | ||
|
|
||
| >>> class A: | ||
| ... def __class_getitem__(cls, x): | ||
| ... return ParameterisedA(x) | ||
| >>> class ParameterisedA: | ||
| ... def __init__(self, params): | ||
| ... self._params = params | ||
| ... def __repr__(self): | ||
| ... return f"A[{self._params}]" | ||
| ... def __iter__(self): | ||
| ... for p in self._params: | ||
| ... yield p | ||
| >>> class B: | ||
| ... def __iter__(self): | ||
| ... yield StarredB() | ||
| ... def __repr__(self): | ||
| ... return "B" | ||
| >>> class StarredB: | ||
| ... def __repr__(self): | ||
| ... return "StarredB" | ||
| >>> b = B() | ||
|
|
||
| Slices that are supposed to work, starring our custom B class | ||
|
|
||
| >>> A[*b] | ||
| A[(StarredB,)] | ||
| >>> A[*b, *b] | ||
| A[(StarredB, StarredB)] | ||
| >>> A[b, *b] | ||
| A[(B, StarredB)] | ||
| >>> A[*b, b] | ||
| A[(StarredB, B)] | ||
| >>> A[b, b, *b] | ||
| A[(B, B, StarredB)] | ||
| >>> A[*b, b, b] | ||
| A[(StarredB, B, B)] | ||
| >>> A[b, *b, b] | ||
| A[(B, StarredB, B)] | ||
| >>> A[b, b, *b, b] | ||
| A[(B, B, StarredB, B)] | ||
| >>> A[b, *b, b, b] | ||
| A[(B, StarredB, B, B)] | ||
| >>> A[A[b, *b, b]] | ||
| A[A[(B, StarredB, B)]] | ||
| >>> A[*A[b, *b, b]] | ||
| A[(B, StarredB, B)] | ||
| >>> A[b, ...] | ||
| A[(B, Ellipsis)] | ||
| >>> A[*A[b, ...]] | ||
| A[(B, Ellipsis)] | ||
|
|
||
| Slices that are supposed to work, starring a list | ||
|
|
||
| >>> l = [1, 2, 3] | ||
| >>> A[*l] | ||
| A[(1, 2, 3)] | ||
| >>> A[*l, 4] | ||
| A[(1, 2, 3, 4)] | ||
| >>> A[0, *l] | ||
| A[(0, 1, 2, 3)] | ||
| >>> A[1:2, *l] | ||
| A[(slice(1, 2, None), 1, 2, 3)] | ||
| >>> repr(A[1:2, *l]) == repr(A[1:2, 1, 2, 3]) | ||
| True | ||
|
|
||
| Slices that are supposed to work, starring a tuple | ||
|
|
||
| >>> t = (1, 2, 3) | ||
| >>> A[*t] | ||
| A[(1, 2, 3)] | ||
| >>> A[*t, 4] | ||
| A[(1, 2, 3, 4)] | ||
| >>> A[0, *t] | ||
| A[(0, 1, 2, 3)] | ||
| >>> A[1:2, *t] | ||
| A[(slice(1, 2, None), 1, 2, 3)] | ||
| >>> repr(A[1:2, *t]) == repr(A[1:2, 1, 2, 3]) | ||
| True | ||
|
|
||
| Starring an expression (rather than a name) in a slice | ||
|
|
||
| >>> def returns_list(): | ||
| ... return [1, 2, 3] | ||
| >>> A[returns_list()] | ||
| A[[1, 2, 3]] | ||
| >>> A[returns_list(), 4] | ||
| A[([1, 2, 3], 4)] | ||
| >>> A[*returns_list()] | ||
| A[(1, 2, 3)] | ||
| >>> A[*returns_list(), 4] | ||
| A[(1, 2, 3, 4)] | ||
| >>> A[0, *returns_list()] | ||
| A[(0, 1, 2, 3)] | ||
| >>> A[*returns_list(), *returns_list()] | ||
| A[(1, 2, 3, 1, 2, 3)] | ||
|
|
||
| Slices that should fail | ||
|
|
||
| >>> A[:*b] | ||
| Traceback (most recent call last): | ||
| ... | ||
| SyntaxError: invalid syntax | ||
| >>> A[*b:] | ||
| Traceback (most recent call last): | ||
| ... | ||
| SyntaxError: invalid syntax | ||
| >>> A[*b:*b] | ||
| Traceback (most recent call last): | ||
| ... | ||
| SyntaxError: invalid syntax | ||
| >>> A[**b] | ||
| Traceback (most recent call last): | ||
| ... | ||
| SyntaxError: invalid syntax | ||
|
|
||
| *args annotated as starred expression | ||
|
|
||
| >>> def f1(*args: *b): pass | ||
| >>> f1.__annotations__ | ||
| {'args': StarredB} | ||
|
|
||
| >>> def f2(*args: *b, arg1): pass | ||
| >>> f2.__annotations__ | ||
| {'args': StarredB} | ||
|
|
||
| >>> def f3(*args: *b, arg1: int): pass | ||
| >>> f3.__annotations__ | ||
| {'args': StarredB, 'arg1': <class 'int'>} | ||
|
|
||
| >>> def f4(*args: *b, arg1: int = 2): pass | ||
| >>> f4.__annotations__ | ||
| {'args': StarredB, 'arg1': <class 'int'>} | ||
|
|
||
| Other uses of starred expressions as annotations should fail | ||
|
|
||
| >>> def f6(x: *b): pass | ||
| Traceback (most recent call last): | ||
| ... | ||
| SyntaxError: invalid syntax | ||
| >>> x: *b | ||
| Traceback (most recent call last): | ||
| ... | ||
| SyntaxError: invalid syntax | ||
|
|
||
| """ | ||
|
|
||
| __test__ = {'doctests' : doctests} | ||
|
|
||
| def test_main(verbose=False): | ||
| from test import support | ||
| from test import test_pep646 | ||
| support.run_doctest(test_pep646, verbose) | ||
|
|
||
| if __name__ == "__main__": | ||
| test_main(verbose=True) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.