GeezerVison is showing github.com — Syntax error on '{z} if z is not None else pass' · Issue #129515 · python/cpython
Syntax error on '{z} if z is not None else pass' #129515DescriptionBug report
Bug description:
My question is in the title.
In the tutorial, I read: Use 'pass' in places, where code is required syntactically, but none is needed.
In order to save indentations, I used the conditional assignement in some segment of code. But I only want to include z in the set x, if it is not None, because later, the None disturbs.
So my thesis is: The code in the title is syntactically correct, but still, I get a syntax error.
Thank you for your kind consideration
CPython versions tested on:
3.10
Operating systems tested on:
Windows
Linked PRs
Activityhoodmane commented on Jan 31, 2025 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.
>>> a if b else pass
File "<stdin>", line 1
a if b else pass
^^^^
SyntaxError: invalid syntaxericvsmith commented on Jan 31, 2025 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. hoodmane commented on Jan 31, 2025 Yeah would be fantastic if the SyntaxError here said something like "statement given where expression required". Reacted by Tomas R., Stan Ulbrych and Alex Waygood tomasr8 commented on Jan 31, 2025
Yeah would be fantastic if the SyntaxError here said something like "statement given where expression required".
It might be possible to add a new invalid_ rule to the parser for this case StanFromIreland commented on Jan 31, 2025 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. none-of-my-names-is-available commented on Jan 31, 2025
…
--
[X]< https://www.risa.eu/en/index.php>
RISA Sicherheitsanalysen GmbH
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, 2025terryjreedy commented 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. sergey-miryanov commented on Feb 8, 2025 If no one else is doing it, I can take it. StanFromIreland commented on 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. Reacted by Sergey Miryanov sergey-miryanov commented on 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_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
Please take a look.
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|