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.

classification
Title: SyntaxError: assignment expression within assert
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Assignment expression in assert causes SyntaxError
View: 39909
Assigned To: Nosy List: gvanrossum, jan.cespivo, ronaldoussoren, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-07-28 07:59 by jan.cespivo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg374476 - (view) Author: Jan Češpivo (jan.cespivo) Date: 2020-07-28 07:59
Hi,

it should be useful if assignment expression works within assertion.

For example (real use-case in tests):

assert r := re.match(r"result is (\d+)", tested_text)
assert int(r.group(1)) == expected_number

I haven't found a mention about assertions in https://www.python.org/dev/peps/pep-0572/ so it isn't technically a bug but it might be omission (?).

Thx!
msg374478 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-07-28 08:10
I don't know if this limitation is intentional, but not that you can use an assignment expression when you enclose the expression with parenthesis:

>>> assert (a:=1)
>>> a
1
>>>
msg374479 - (view) Author: Jan Češpivo (jan.cespivo) Date: 2020-07-28 08:14
Hi Ronald,

thank you. It works! :)
msg374482 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-07-28 10:18
It is a duplicate of issue39909.
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85589
2020-07-28 10:18:58serhiy.storchakasetstatus: open -> closed

superseder: Assignment expression in assert causes SyntaxError

nosy: + serhiy.storchaka
messages: + msg374482
resolution: duplicate
stage: resolved
2020-07-28 08:14:46xtreaksetnosy: + gvanrossum
2020-07-28 08:14:35jan.cespivosetmessages: + msg374479
2020-07-28 08:10:23ronaldoussorensetnosy: + ronaldoussoren
messages: + msg374478
2020-07-28 07:59:37jan.cespivocreate