GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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' #129515

New issue
Copy link
New issue
Copy link
Closed
Closed
Syntax error on '{z} if z is not None else pass'#129515
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-featureA feature request or enhancementA feature request or enhancement

Description

@none-of-my-names-is-available
none-of-my-names-is-available
opened on Jan 31, 2025
Issue body actions

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

  • gh-129515: Clarify syntax error messages for conditional expressions #129880

Activity

Next
  1. none-of-my-names-is-available
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 31, 2025
  2. hoodmane commented on Jan 31, 2025

    @hoodmane
    hoodmane
    on Jan 31, 2025
    Contributor
    More actions

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

    @ericvsmith
    ericvsmith
    on Jan 31, 2025
    Member
    More actions

    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.

  4. hoodmane commented on Jan 31, 2025

    @hoodmane
    hoodmane
    on Jan 31, 2025
    Contributor
    More actions

    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
  5. tomasr8 commented on Jan 31, 2025

    @tomasr8
    tomasr8
    on Jan 31, 2025
    Member
    More actions

    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

  6. StanFromIreland commented on Jan 31, 2025

    @StanFromIreland
    StanFromIreland
    on Jan 31, 2025
    Member
    More actions

    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.

  7. encukou
    added
    topic-parser
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 31, 2025
  8. terryjreedy
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Jan 31, 2025
  9. github-project-automation
    added this to docs issueson Jan 31, 2025
  10. github-project-automation
    moved this to Todo in docs issueson Jan 31, 2025
  11. none-of-my-names-is-available commented on Jan 31, 2025

    @none-of-my-names-is-available
    none-of-my-names-is-available
    on Jan 31, 2025 via email
    Author
    More actions
    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> +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.
  12. terryjreedy
    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
  13. terryjreedy
    removed
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Jan 31, 2025
  14. terryjreedy commented on Jan 31, 2025

    @terryjreedy
    terryjreedy
    on Jan 31, 2025
    Member
    More actions

    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.

  15. 1 remaining item

  16. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Jan 31, 2025
  17. sergey-miryanov commented on Feb 8, 2025

    @sergey-miryanov
    sergey-miryanov
    on Feb 8, 2025
    Contributor
    More actions

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

  18. StanFromIreland commented on Feb 8, 2025

    @StanFromIreland
    StanFromIreland
    on Feb 8, 2025
    Member
    More actions

    @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
  19. sergey-miryanov
    added 2 commits that reference this issue on Feb 9, 2025

    pythongh-129515: Clarify syntax error messages for conditional expres…

    4dbba6b

    Merge branch 'main' into pythongh-129515-ifexp-syntax-error

    aa3f17d
  20. sergey-miryanov commented on Feb 10, 2025

    @sergey-miryanov
    sergey-miryanov
    on Feb 10, 2025
    Contributor
    More actions

    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.

  21. sergey-miryanov
    added 2 commits that reference this issue on Feb 10, 2025

    Merge branch 'pythongh-129515-ifexp-syntax-error' of github.com:serge…

    b43b3a2

    Merge branch 'main' into pythongh-129515-ifexp-syntax-error

    48e4796
  22. pablogsal
    added a commit that references this issue on Feb 10, 2025

    Merge branch 'main' into pythongh-129515-ifexp-syntax-error

    8afe66a
  23. sergey-miryanov
    added 4 commits that reference this issue on Feb 11, 2025

    Merge branch 'main' into pythongh-129515-ifexp-syntax-error

    7e4ed1b

    Merge branch 'main' into pythongh-129515-ifexp-syntax-error

    2eaa968

    Merge branch 'main' into pythongh-129515-ifexp-syntax-error

    63b4400

    Merge branch 'pythongh-129515-ifexp-syntax-error' of github.com:serge…

    c468622
  24. pablogsal
    added a commit that references this issue on Feb 18, 2025

    gh-129515: Clarify syntax error messages for conditional expressions (#…

    bcc9a5d
  25. pablogsal
    closed this as completedon 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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • docs issues
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.