-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/compile: Implement PEP 572, assignment expressions. #4908
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0fd91e3
py/compile: Convert scope test to SCOPE_IS_COMP_LIKE macro.
dpgeorge 1783950
py/compile: Implement PEP 572, assignment expressions with := operator.
dpgeorge 2c5993c
ports: Disable MICROPY_PY_ASSIGN_EXPR in bare-arm and minimal ports.
dpgeorge e0fe8ea
tests/basics: Add tests for assignment operator :=.
dpgeorge a3c89cf
tests/cpydiff: Add CPy diff test for assignment expression behaviour.
dpgeorge 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 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 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 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 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 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 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 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 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,29 @@ | ||
(x := 4) | ||
print(x) | ||
|
||
if x := 2: | ||
print(True) | ||
print(x) | ||
|
||
print(4, x := 5) | ||
print(x) | ||
|
||
x = 1 | ||
print(x, x := 5, x) | ||
print(x) | ||
|
||
|
||
def foo(): | ||
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10))) | ||
return hit | ||
|
||
|
||
hit = 123 | ||
print(foo()) | ||
print(hit) # shouldn't be changed by foo | ||
|
||
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10))) | ||
print(hit) # should be changed by above | ||
|
||
print([((m := k + 1), k * m) for k in range(4)]) | ||
print(m) |
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,14 @@ | ||
4 | ||
True | ||
2 | ||
4 5 | ||
5 | ||
1 5 5 | ||
5 | ||
any True | ||
8 | ||
123 | ||
any True | ||
8 | ||
[(1, 0), (2, 2), (3, 6), (4, 12)] | ||
4 |
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,16 @@ | ||
# test SyntaxError with := operator | ||
|
||
def test(code): | ||
try: | ||
print(eval(code)) | ||
except SyntaxError: | ||
print('SyntaxError') | ||
|
||
test("x := 1") | ||
test("((x, y) := 1)") | ||
|
||
# these are currently all allowed in MicroPython, but not in CPython | ||
test("([i := i + 1 for i in range(4)])") | ||
test("([i := -1 for i, j in [(1, 2)]])") | ||
test("([[(i := j) for i in range(2)] for j in range(2)])") | ||
test("([[(j := i) for i in range(2)] for j in range(2)])") |
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,6 @@ | ||
SyntaxError | ||
SyntaxError | ||
[1, 2, 3, 4] | ||
[-1] | ||
[[0, 0], [1, 1]] | ||
[[0, 1], [0, 1]] |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
tok(4) | ||
[ 4] rule(22) (n=4) | ||
id(i) | ||
[ 4] rule(44) (n=1) | ||
[ 4] rule(45) (n=1) | ||
NULL | ||
[ 5] rule(8) (n=0) | ||
NULL | ||
|
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,7 @@ | ||
""" | ||
categories: Syntax,Operators | ||
description: MicroPython allows using := to assign to the variable of a comprehension, CPython raises a SyntaxError. | ||
cause: MicroPython is optimised for code size and doesn't check this case. | ||
workaround: Do not rely on this behaviour if writing CPython compatible code. | ||
""" | ||
print([i := -1 for i in range(4)]) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is used, it's part of the big table referenced by grammar.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, text editor search failure