Skip to content

Syntax error on '{z} if z is not None else pass' #129515

Closed

Description

Bug 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

Activity

  1. hoodmane commented on Jan 31, 2025

    @hoodmane
    Contributor

    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 syntax
  2. ericvsmith commented on Jan 31, 2025

    @ericvsmith
    Member

    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.

  3. hoodmane commented on Jan 31, 2025

    @hoodmane
    Contributor

    Yeah would be fantastic if the SyntaxError here said something like "statement given where expression required".

  4. tomasr8 commented on Jan 31, 2025

    @tomasr8
    Member

    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

  5. StanFromIreland commented on Jan 31, 2025

    @StanFromIreland
    Member

    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.

  6. added and removed
    type-bugAn unexpected behavior, bug, or error
    on Jan 31, 2025
  7. none-of-my-names-is-available commented on Jan 31, 2025

    @none-of-my-names-is-available
    Author
  8. 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
  9. terryjreedy commented on Jan 31, 2025

    @terryjreedy
    Member

    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.

  10. 1 remaining item

  11. sergey-miryanov commented on Feb 8, 2025

    @sergey-miryanov
    Contributor

    If no one else is doing it, I can take it.

  12. StanFromIreland commented on Feb 8, 2025

    @StanFromIreland
    Member

    @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.

  13. added 2 commits that reference this issue on Feb 9, 2025
  14. sergey-miryanov commented on Feb 10, 2025

    @sergey-miryanov
    Contributor

    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.

  15. added 2 commits that reference this issue on Feb 10, 2025
  16. added a commit that references this issue on Feb 10, 2025
  17. added 4 commits that reference this issue on Feb 11, 2025
  18. added a commit that references this issue on Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions