This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Assignment expression in assert causes SyntaxError
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, vstinner, Михаил Кыштымов
Priority: normal Keywords:

Created on 2020-03-09 07:26 by Михаил Кыштымов, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg363698 - (view) Author: Михаил Кыштымов (Михаил Кыштымов) Date: 2020-03-09 07:26
Assignment expression in assert causes SyntaxError

Minimal case:
assert var := None

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

assert (var := None)

My use case:
my_dict = dict()
assert value := my_dict.get('key')
msg363699 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-09 08:14
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?
msg363700 - (view) Author: Михаил Кыштымов (Михаил Кыштымов) Date: 2020-03-09 08:21
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.

workaraund does not look as cool because of parentheses.

again, if this is "as intended" i'm fine with closing issue.
msg363701 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-03-09 08:23
> 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.

Python works as expected, I suggest to close the issue as not a bug.
msg363702 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-09 08:25
I'm going to close this, since I can't imagine this restriction being relaxed.
msg363703 - (view) Author: Михаил Кыштымов (Михаил Кыштымов) Date: 2020-03-09 08:25
Date User Action Args
2022-04-11 14:59:27adminsetgithub: 84090
2020-07-28 10:18:58serhiy.storchakalinkissue41417 superseder
2020-03-09 08:25:09Михаил Кыштымовsetmessages: + msg363703
2020-03-09 08:25:00eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg363702

stage: resolved
2020-03-09 08:23:02vstinnersetnosy: + vstinner
messages: + msg363701
2020-03-09 08:21:48Михаил Кыштымовsetmessages: + msg363700
2020-03-09 08:14:11eric.smithsetnosy: + eric.smith
messages: + msg363699
2020-03-09 07:26:04Михаил Кыштымовcreate