Skip to content
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

Assignment expression in assert causes SyntaxError #84090

Closed
ghost opened this issue Mar 9, 2020 · 6 comments
Closed

Assignment expression in assert causes SyntaxError #84090

ghost opened this issue Mar 9, 2020 · 6 comments
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ghost
Copy link

ghost commented Mar 9, 2020

BPO 39909
Nosy @vstinner, @ericvsmith

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-03-09.08:25:00.808>
created_at = <Date 2020-03-09.07:26:04.755>
labels = ['interpreter-core', 'invalid', 'type-bug', '3.8']
title = 'Assignment expression in assert causes SyntaxError'
updated_at = <Date 2020-03-09.08:25:09.449>
user = None

bugs.python.org fields:

activity = <Date 2020-03-09.08:25:09.449>
actor = '\xd0\x9c\xd0\xb8\xd1\x85\xd0\xb0\xd0\xb8\xd0\xbb \xd0\x9a\xd1\x8b\xd1\x88\xd1\x82\xd1\x8b\xd0\xbc\xd0\xbe\xd0\xb2'
assignee = 'none'
closed = True
closed_date = <Date 2020-03-09.08:25:00.808>
closer = 'eric.smith'
components = ['Interpreter Core']
creation = <Date 2020-03-09.07:26:04.755>
creator = '\xd0\x9c\xd0\xb8\xd1\x85\xd0\xb0\xd0\xb8\xd0\xbb \xd0\x9a\xd1\x8b\xd1\x88\xd1\x82\xd1\x8b\xd0\xbc\xd0\xbe\xd0\xb2'
dependencies = []
files = []
hgrepos = []
issue_num = 39909
keywords = []
message_count = 6.0
messages = ['363698', '363699', '363700', '363701', '363702', '363703']
nosy_count = 3.0
nosy_names = ['vstinner', 'eric.smith', '\xd0\x9c\xd0\xb8\xd1\x85\xd0\xb0\xd0\xb8\xd0\xbb \xd0\x9a\xd1\x8b\xd1\x88\xd1\x82\xd1\x8b\xd0\xbc\xd0\xbe\xd0\xb2']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39909'
versions = ['Python 3.8']

@ghost
Copy link
Author

ghost commented Mar 9, 2020

Assignment expression in assert causes SyntaxError

Minimal case:

assert var := None

Error:

  File "<input>", line 1
    assert var := None
               ^
SyntaxError: invalid syntax

Workaround:

assert (var := None)

My use case:

my_dict = dict()
assert value := my_dict.get('key')

Repository owner added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 9, 2020
@ericvsmith
Copy link
Member

I believe this is by design, as is the same restriction on statement-level assignment expressions. And if requiring parens discourages the use of assignment expressions in asserts, I think that's a good thing.

Why not just use the workaround you've already identified?

@ghost
Copy link
Author

ghost commented Mar 9, 2020

if this is by design i'm fine with somebody closing this issue.

i've not found mention of such case in pep so i though it's not by design.
docs say assert just takes "expression" and "assignment expression" has "expression" in it's name.
https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement

workaraund does not look as cool because of parentheses.

again, if this is "as intended" i'm fine with closing issue.

@vstinner
Copy link
Member

vstinner commented Mar 9, 2020

assert var := None

It's good that this syntax is rejected: it looks like a typo (assert var == None). Moreover, it's part of of the PEP-572 design.
https://www.python.org/dev/peps/pep-0572/#exceptional-cases

Python works as expected, I suggest to close the issue as not a bug.

@ericvsmith
Copy link
Member

I'm going to close this, since I can't imagine this restriction being relaxed.

@ghost
Copy link
Author

ghost commented Mar 9, 2020

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants