Skip to content

Allow named expressions in generator comprehensions#4109

Merged
youknowone merged 2 commits into
RustPython:mainfrom
astral-sh:charlie/named-expr-generator
Aug 21, 2022
Merged

Allow named expressions in generator comprehensions#4109
youknowone merged 2 commits into
RustPython:mainfrom
astral-sh:charlie/named-expr-generator

Conversation

@charliermarsh
Copy link
Copy Markdown
Collaborator

Summary

See: #4108.

I noticed this when trying to parse the CPython codebase itself, which has this test (on which RustPython throws a syntax error):

def test_named_expression_scope_in_genexp(self):
    a = 1
    b = [1, 2, 3, 4]
    genexp = (c := i + a for i in b)

    self.assertNotIn("c", locals())
    for idx, elem in enumerate(genexp):
        self.assertEqual(elem, b[idx] + a)

Copy link
Copy Markdown
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Because we don't have related cpython unittest for this grammer,
Adding a test under extra_tests/snippets/syntax_generator.py will ensure both CPython and RustPython have same behavior

@charliermarsh charliermarsh force-pushed the charlie/named-expr-generator branch from 55cf065 to 32030a0 Compare August 21, 2022 15:07
@charliermarsh
Copy link
Copy Markdown
Collaborator Author

Done!

...
test_snippets.py::SampleTestCase::test_rustpython_syntax_generator PASSED
...

Copy link
Copy Markdown
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@youknowone youknowone merged commit 1b58f00 into RustPython:main Aug 21, 2022
@youknowone youknowone linked an issue Aug 21, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syntax error with named expressions in generators

2 participants