-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-43224: Implement PEP 646 grammar changes #31018
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
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7ecb7ba
bpo-43224: Implement PEP 646 grammar changes
mrahtz c0531ec
Add tests for del
mrahtz 479c175
Tweak news to better fit in with other PRs
mrahtz 091ceeb
Move error cases into test_syntax
mrahtz f1c9c77
Add an extra failure test
mrahtz c8d2534
Add more tests combining slicing with starring
mrahtz 5df1e97
Fix ast representation of slices combined with starring
mrahtz 677a326
Fix broken test in test_unparse
mrahtz 1543785
Add a check_src_roundtrip test for "a[1:2, *a]"
mrahtz e1ab6ea
fix whitespace (make patchcheck)
JelleZijlstra 18e7fa7
Merge remote-tracking branch 'upstream/main' into pep646-grammar
JelleZijlstra 72dd8b9
Add tests for AST changes
mrahtz 80bb5fa
Fix C unpack
mrahtz fbfd5b1
Fix typing.get_type_hints on variadic annotation
mrahtz 816098c
Fix typo
mrahtz 8962d26
Remove debug print
mrahtz 973e9cc
Fix potential shadowing in ForwardRef
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
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
This file contains hidden or bidir
10000
ectional 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
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,286 @@ | ||
doctests = """ | ||
|
||
Setup | ||
|
||
>>> class AClass: | ||
... def __init__(self): | ||
... self._setitem_name = None | ||
... self._setitem_val = None | ||
... self._delitem_name = None | ||
... def __setitem__(self, name, val): | ||
... self._delitem_name = None | ||
... self._setitem_name = name | ||
... self._setitem_val = val | ||
... def __repr__(self): | ||
... if self._setitem_name is not None: | ||
... return f"A[{self._setitem_name}]={self._setitem_val}" | ||
... elif self._delitem_name is not None: | ||
... return f"delA[{self._delitem_name}]" | ||
... def __getitem__(self, name): | ||
... return ParameterisedA(name) | ||
... def __delitem__(self, name): | ||
... self._setitem_name = None | ||
... self._delitem_name = name | ||
... | ||
>>> class ParameterisedA: | ||
... def __init__(self, name): | ||
... self._name = name | ||
... def __repr__(self): | ||
... return f"A[{self._name}]" | ||
... def __iter__(self): | ||
... for p in self._name: | ||
... yield p | ||
>>> class B: | ||
... def __iter__(self): | ||
... yield StarredB() | ||
... def __repr__(self): | ||
... return "B" | ||
>>> class StarredB: | ||
... def __repr__(self): | ||
... return "StarredB" | ||
>>> A = AClass() | ||
>>> b = B() | ||
|
||
Slices that are supposed to work, starring our custom B class | ||
|
||
>>> A[*b] | ||
A[(StarredB,)] | ||
>>> A[*b] = 1; A | ||
A[(StarredB,)]=1 | ||
>>> del A[*b]; A | ||
delA[(StarredB,)] | ||
|
||
>>> A[*b, *b] | ||
A[(StarredB, StarredB)] | ||
>>> A[*b, *b] = 1; A | ||
A[(StarredB, StarredB)]=1 | ||
>>> del A[*b, *b]; A | ||
delA[(StarredB, StarredB)] | ||
|
||
>>> A[b, *b] | ||
A[(B, StarredB)] | ||
>>> A[b, *b] = 1; A | ||
A[(B, StarredB)]=1 | ||
>>> del A[b, *b]; A | ||
delA[(B, StarredB)] | ||
|
||
>>> A[*b, b] | ||
A[(StarredB, B)] | ||
>>> A[*b, b] = 1; A | ||
A[(StarredB, B)]=1 | ||
>>> del A[*b, b]; A | ||
delA[(StarredB, B)] | ||
|
||
>>> A[b, b, *b] | ||
A[(B, B, StarredB)] | ||
>>> A[b, b, *b] = 1; A | ||
A[(B, B, StarredB)]=1 | ||
>>> del A[b, b, *b]; A | ||
delA[(B, B, StarredB)] | ||
|
||
>>> A[*b, b, b] | ||
A[(StarredB, B, B)] | ||
>>> A[*b, b, b] = 1; A | ||
A[(StarredB, B, B)]=1 | ||
>>> del A[*b, b, b]; A | ||
delA[(StarredB, B, B)] | ||
|
||
>>> A[b, *b, b] | ||
A[(B, StarredB, B)] | ||
>>> A[b, *b, b] = 1; A | ||
A[(B, StarredB, B)]=1 | ||
>>> del A[b, *b, b]; A | ||
delA[(B, StarredB, B)] | ||
|
||
>>> A[b, b, *b, b] | ||
A[(B, B, StarredB, B)] | ||
>>> A[b, b, *b, b] = 1; A | ||
A[(B, B, StarredB, B)]=1 | ||
>>> del A[b, b, *b, b]; A | ||
delA[(B, B, StarredB, B)] | ||
|
||
>>> A[b, *b, b, b] | ||
A[(B, StarredB, B, B)] | ||
>>> A[b, *b, b, b] = 1; A | ||
A[(B, StarredB, B, B)]=1 | ||
>>> del A[b, *b, b, b]; A | ||
delA[(B, StarredB, B, B)] | ||
|
||
>>> A[A[b, *b, b]] | ||
A[A[(B, StarredB, B)]] | ||
>>> A[A[b, *b, b]] = 1; A | ||
A[A[(B, StarredB, B)]]=1 | ||
>>> del A[A[b, *b, b]]; A | ||
delA[A[(B, StarredB, B)]] | ||
|
||
>>> A[*A[b, *b, b]] | ||
A[(B, StarredB, B)] | ||
>>> A[*A[b, *b, b]] = 1; A | ||
A[(B, StarredB, B)]=1 | ||
>>> del A[*A[b, *b, b]]; A | ||
delA[(B, StarredB, B)] | ||
|
||
>>> A[b, ...] | ||
A[(B, Ellipsis)] | ||
>>> A[b, ...] = 1; A | ||
A[(B, Ellipsis)]=1 | ||
>>> del A[b, ...]; A | ||
delA[(B, Ellipsis)] | ||
|
||
>>> A[*A[b, ...]] | ||
A[(B, Ellipsis)] | ||
>>> A[*A[b, ...]] = 1; A | ||
A[(B, Ellipsis)]=1 | ||
>>> del A[*A[b, ...]]; A | ||
delA[(B, Ellipsis)] | ||
|
||
Slices that are supposed to work, starring a list | ||
|
||
>>> l = [1, 2, 3] | ||
|
||
>>> A[*l] | ||
A[(1, 2, 3)] | ||
>>> A[*l] = 1; A | ||
A[(1, 2, 3)]=1 | ||
>>> del A[*l]; A | ||
delA[(1, 2, 3)] | ||
|
||
>>> A[*l, 4] | ||
A[(1, 2, 3, 4)] | ||
>>> A[*l, 4] = 1; A | ||
A[(1, 2, 3, 4)]=1 | ||
>>> del A[*l, 4]; A | ||
delA[(1, 2, 3, 4)] | ||
|
||
>>> A[0, *l] | ||
A[(0, 1, 2, 3)] | ||
>>> A[0, *l] = 1; A | ||
A[(0, 1, 2, 3)]=1 | ||
>>> del A[0, *l]; A | ||
delA[(0, 1, 2, 3)] | ||
|
||
>>> A[1:2, *l] | ||
A[(slice(1, 2, None), 1, 2, 3)] | ||
>>> A[1:2, *l] = 1; A | ||
A[(slice(1, 2, None), 1, 2, 3)]=1 | ||
>>> del A[1:2, *l]; A | ||
delA[(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] = 1; A | ||
A[(1, 2, 3)]=1 | ||
>>> del A[*t]; A | ||
delA[(1, 2, 3)] | ||
|
||
>>> A[*t, 4] | ||
A[(1, 2, 3, 4)] | ||
>>> A[*t, 4] = 1; A | ||
A[(1, 2, 3, 4)]=1 | ||
>>> del A[*t, 4]; A | ||
delA[(1, 2, 3, 4)] | ||
|
||
>>> A[0, *t] | ||
A[(0, 1, 2, 3)] | ||
>>> A[0, *t] = 1; A | ||
A[(0, 1, 2, 3)]=1 | ||
>>> del A[0, *t]; A | ||
delA[(0, 1, 2, 3)] | ||
|
||
>>> A[1:2, *t] | ||
A[(slice(1, 2, None), 1, 2, 3)] | ||
>>> A[1:2, *t] = 1; A | ||
A[(slice(1, 2, None), 1, 2, 3)]=1 | ||
>>> del A[1:2, *t]; A | ||
delA[(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()] = 1; A | ||
A[[1, 2, 3]]=1 | ||
>>> del A[returns_list()]; A | ||
delA[[1, 2, 3]] | ||
|
||
>>> A[returns_list(), 4] | ||
A[([1, 2, 3], 4)] | ||
>>> A[returns_list(), 4] = 1; A | ||
A[([1, 2, 3], 4)]=1 | ||
>>> del A[returns_list(), 4]; A | ||
delA[([1, 2, 3], 4)] | ||
|
||
>>> A[*returns_list()] | ||
A[(1, 2, 3)] | ||
>>> A[*returns_list()] = 1; A | ||
A[(1, 2, 3)]=1 | ||
>>> del A[*returns_list()]; A | ||
delA[(1, 2, 3)] | ||
|
||
>>> A[*returns_list(), 4] | ||
A[(1, 2, 3, 4)] | ||
>>> A[*returns_list(), 4] = 1; A | ||
A[(1, 2, 3, 4)]=1 | ||
>>> del A[*returns_list(), 4]; A | ||
delA[(1, 2, 3, 4)] | ||
|
||
>>> A[0, *returns_list()] | ||
A[(0, 1, 2, 3)] | ||
>>> A[0, *returns_list()] = 1; A | ||
A[(0, 1, 2, 3)]=1 | ||
>>> del A[0, *returns_list()]; A | ||
delA[(0, 1, 2, 3)] | ||
|
||
>>> A[*returns_list(), *returns_list()] | ||
A[(1, 2, 3, 1, 2, 3)] | ||
>>> A[*returns_list(), *returns_list()] = 1; A | ||
A[(1, 2, 3, 1, 2, 3)]=1 | ||
>>> del A[*returns_list(), *returns_list()]; A | ||
delA[(1, 2, 3, 1, 2, 3)] | ||
|
||
*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'>} | ||
|
||
>>> def f5(*args: *b = (1,)): pass | ||
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_syntax | ||
support.run_doctest(test_pep646_syntax, verbose) | ||
|
||
if __name__ == "__main__": | ||
test_main(verbose=True) |
Oops, something went wrong.
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.