You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Syntax error on '{z} if z is not None else pass' #129515
pass is a statement. lval |= expression is a type of statement. expression if expression else expression is a type of expression. a if b else pass has a statement in a location where the syntax requires an expression so it's a SyntaxError.
The place you've used pass requires an expression, and pass is a statement, not an expression.
It's possible the documentation could be tweaked to make this clearer. Maybe it should say "where a statement is required", although maybe that's too subtle for the tutorial.
Hi Stan,
thank you for your solution, but a more obvious one (in my eyes a a beginner in python) is:
x |= {z} if z is not None else set ()
An implicit if without else clause is not so obvious, if it was a real assignment.
This is why I think that the comment suggesting a more elaborate error message would be good.
But thank you,
Günter Becker
Am 31.01.2025 um 17:25 schrieb Stan Ulbrych:
If you still want a working one line solution
z is not None and x.add(z)
as if z is None it will short circuit.
—
Reply to this email directly, view it on GitHub<#129515 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BC3NZRPVTEPKLGFFAEC7MED2NOPXTAVCNFSM6AAAAABWHRK44CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMRXG42DCOJSHE>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
--
[X]<https://www.risa.eu/en/index.php>
RISA Sicherheitsanalysen GmbH
www.risa.de<http://www.risa.de>
+49 (30) 31 57 06 – 0
Xantener Straße 11
10707 Berlin, Germany
[X]<https://www.linkedin.com/company/risa-sicherheitsanalysen-gmbh/> [X] ***@***.***>
Dr. Günter Becker
Mitglied der Geschäftsleitung
Entwicklung und Anwendung probabilistischer Modelle
***@***.******@***.***>
+49 (30) 31 57 06 – 15
Geschäftsführer: Prof. Dr.-Ing. Leonidas Camarinopoulos
Eingetragen beim Amtsgericht Charlottenburg HRB 33234
ISO 9001:2015 zertifiziert durch den TÜV Rheinland
[X]<https://www.linkedin.com/in/stephanos-camarinopoulos-9945046/>
This email, including any attachments, is the confidential property of RISA Sicherheitsanalysen GmbH and is intended solely for the use of the individual or entity to whom it is addressed. If you have received this message in mistake, please notify the sender immediately and delete the message from your system. Kindly refrain from copying or distributing the contained information.
changed the title [-]Why syntax error on: x |= {z} if z is not None else pass[/-][+]Syntax error on '{z} if z is not None else pass'[/+]on Jan 31, 2025
I fixed the title to focus on the syntactically incorrect expression. An expression must evaluate to a value and pass has no value. The proper null value for a set is set(), not None. {z} if z is not None else set() would be the proper expression in context. Given set x, the statement if z is not None: x.add(z) looks even better to me for adding z if not None.
The pass reference entry says " It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed". The tutorial entry, both now and in 3.10, says essentially the same: "It can be used when a statement is required syntactically but the program requires no action." @none-of-my-names-is-available, you apparently misread statement as code. No doc fix is needed.
I think a better error message would be nice if possible.
The following expressions will be handled by a=disjunction 'if' b=disjunction 'else' c=invalid_ifexp_orelse_stmt and generate the message "Syntax Error: statement given where 'orelse' expression required" instead of just "Syntax Error: invalid syntax":
1 if True else pass
1 if True else return
1 if True else raise Exception('test')
1 if True else del smth
1 if True else yield 2
1 if True else assert False
1 if True else break
1 if True else continue
The following expressions will be handled by a=invalid_ifexp_body_stmt 'if' b=disjunction 'else' c=expression and generate the message "Syntax Error: statement given where 'body' expression required":
pass if True else 1
break if True else 1
continue if True else 1
The followingexpressions will be handled by a=invalid_ifexp_body_stmt 'if' b=disjunction 'else' c=invalid_ifexp_orelse_stmt and generate the message "Syntax Error: statement given where 'body' expression required":
pass if True else pass
break if True else continue
Activity
hoodmane commentedon Jan 31, 2025
passis a statement.lval |= expressionis a type of statement.expression if expression else expressionis a type of expression.a if b else passhas a statement in a location where the syntax requires an expression so it's aSyntaxError.ericvsmith commentedon Jan 31, 2025
The place you've used
passrequires an expression, andpassis a statement, not an expression.It's possible the documentation could be tweaked to make this clearer. Maybe it should say "where a statement is required", although maybe that's too subtle for the tutorial.
hoodmane commentedon Jan 31, 2025
Yeah would be fantastic if the
SyntaxErrorhere said something like "statement given where expression required".tomasr8 commentedon Jan 31, 2025
It might be possible to add a new
invalid_rule to the parser for this caseStanFromIreland commentedon Jan 31, 2025
If you still want a working one line solution
z is not None and x.add(z)as if z is
Noneit will short circuit.none-of-my-names-is-available commentedon Jan 31, 2025
[-]Why syntax error on: x |= {z} if z is not None else pass[/-][+]Syntax error on '{z} if z is not None else pass'[/+]terryjreedy commentedon Jan 31, 2025
I fixed the title to focus on the syntactically incorrect expression. An expression must evaluate to a value and
passhas no value. The proper null value for a set isset(), notNone.{z} if z is not None else set()would be the proper expression in context. Given set x, the statementif z is not None: x.add(z)looks even better to me for adding z if not None.The
passreference entry says " It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed". The tutorial entry, both now and in 3.10, says essentially the same: "It can be used when a statement is required syntactically but the program requires no action." @none-of-my-names-is-available, you apparently misreadstatementascode. No doc fix is needed.I think a better error message would be nice if possible.
1 remaining item
sergey-miryanov commentedon Feb 8, 2025
If no one else is doing it, I can take it.
StanFromIreland commentedon Feb 8, 2025
@sergey-miryanov Feel free to take on any issue where no one else has mentioned they plan to do so and there is no one assigned.
pythongh-129515: Clarify syntax error messages for conditional expres…
Merge branch 'main' into pythongh-129515-ifexp-syntax-error
sergey-miryanov commentedon Feb 10, 2025
Linked PR added the following rules and messages.
The following expressions will be handled by
a=disjunction 'if' b=disjunction 'else' c=invalid_ifexp_orelse_stmtand generate the message "Syntax Error: statement given where 'orelse' expression required" instead of just "Syntax Error: invalid syntax":The following expressions will be handled by
a=invalid_ifexp_body_stmt 'if' b=disjunction 'else' c=expressionand generate the message "Syntax Error: statement given where 'body' expression required":The followingexpressions will be handled by
a=invalid_ifexp_body_stmt 'if' b=disjunction 'else' c=invalid_ifexp_orelse_stmtand generate the message "Syntax Error: statement given where 'body' expression required":Please take a look.
Merge branch 'pythongh-129515-ifexp-syntax-error' of github.com:serge…
Merge branch 'main' into pythongh-129515-ifexp-syntax-error
Merge branch 'main' into pythongh-129515-ifexp-syntax-error
Merge branch 'main' into pythongh-129515-ifexp-syntax-error
Merge branch 'main' into pythongh-129515-ifexp-syntax-error
Merge branch 'main' into pythongh-129515-ifexp-syntax-error
Merge branch 'pythongh-129515-ifexp-syntax-error' of github.com:serge…
gh-129515: Clarify syntax error messages for conditional expressions (#…