Skip to content

Document better assignment expression parentheses requirements #111944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lysnikolaou opened this issue Nov 10, 2023 · 12 comments
Closed

Document better assignment expression parentheses requirements #111944

lysnikolaou opened this issue Nov 10, 2023 · 12 comments
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement

Comments

@lysnikolaou
Copy link
Member

lysnikolaou commented Nov 10, 2023

Feature or enhancement

Proposal:

Should we maybe thinks about allowing unparenthesized top-level assignment expressions. It came up on ipython/ipython#14214 that people would like to have something like that, especially in REPLs, in order to be able to assign a value to a variable and have it be displayed afterwards. I played around with the parser a bit and the change to allow this is very straight-forward.

We could also discuss adding it behind a flag or only enable it on the REPL.

Behavior on main:

❯ ./python.exe
Python 3.13.0a1+ (heads/main:baeb7718f8, Nov 10 2023, 12:05:38) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a := 3
  File "<stdin>", line 1
    a := 3
      ^^
SyntaxError: invalid syntax

New behavior:

❯ ./python.exe
Python 3.13.0a1+ (heads/main-dirty:baeb7718f8, Nov 10 2023, 12:20:20) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a := 3
3

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@lysnikolaou lysnikolaou added the type-feature A feature request or enhancement label Nov 10, 2023
@terryjreedy terryjreedy added type-bug An unexpected behavior, bug, or error and removed type-feature A feature request or enhancement labels Nov 10, 2023
@lysnikolaou
Copy link
Member Author

@terryjreedy I don't think the type-bug label applies here. The current behavior is correct according to the specification in PEP 572.

@lysnikolaou lysnikolaou added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 10, 2023
@terryjreedy terryjreedy added type-feature A feature request or enhancement docs Documentation in the Doc dir labels Nov 10, 2023
@terryjreedy
Copy link
Member

Top level assignment expressions work when parenthesized.

>>> (a := 3)
3
>>> a
3

According to the expression doc
this should not be needed. The discrepancy would usually be seen as a bug.

According to the PEP, the behavior is intended, with the parenthesized form discouraged. See the first example under exceptional cases. So it seems that the doc should be fixed unless it is intended that the discouraged form not be documented. But I believe this would be unusual.

To support a feature addition, argue against the PEP's prohibition rationale.

@gvanrossum

@terryjreedy
Copy link
Member

Bug is for the doc not including 'top level code' in the list of places where parentheses are required. This list is characterized as all-inclusive by saying 'not needed' everywhere else.

@lysnikolaou lysnikolaou changed the title Allow top-level assignment expression Allow unparenthesized top-level assignment expression Nov 10, 2023
@lysnikolaou
Copy link
Member Author

I'm sorry, I might not have made myself clear.

I understand all that. I agree that the doc should be improved to include that top-level walruses need parentheses. But that should probably be a different issue. What I would like to do with this one, is start a discussion on whether we should lift that restriction.

The PEP says:

Unparenthesized assignment expressions are prohibited at the top level of an expression statement. [...] This rule is included to simplify the choice for the user between an assignment statement and an assignment expression – there is no syntactic position where both are valid.

I agree with the concept of "There should be one way to do it", but I would think that, in the context of a REPL, assignment statements and assignment expressions do two slightly different things. And I'm asking whether others agree so that this can be lifted, since it's also a very easy change to implement on the parser side.

@gvanrossum
Copy link
Member

I am not in favor of lifting the restriction. I don’t want there to be two top level forms of assignment.

@lysnikolaou
Copy link
Member Author

lysnikolaou commented Nov 10, 2023

I am not in favor of lifting the restriction. I don’t want there to be two top level forms of assignment.

What if it could only be enabled with an ast.PyCF_ALLOW_TOP_LEVEL_WALRUS kind of flag in ast.parse/compile in single mode? Would that make a difference?

I'm asking because I generally agree with the sentiment. It's only in the context of a REPL, where I think this could be a slight improvement.

@gvanrossum
Copy link
Member

That would only confuse users — what’s valid in one context wouldn’t be valid in another context.

@AlexWaygood
Copy link
Member

That would only confuse users — what’s valid in one context wouldn’t be valid in another context.

I agree -- I think we should keep the REPL consistent with Python in other contexts, wherever possible

@ericvsmith
Copy link
Member

I don't think there's much chance of this succeeding, but if you want to pursue it you should open up a discussion on https://discuss.python.org/c/ideas/6

terryjreedy added a commit to terryjreedy/cpython that referenced this issue Nov 11, 2023
Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
@terryjreedy
Copy link
Member

I labelled as I did because the code and doc issues are not independent. If we change 'main' to allow >>> a:=3, then the doc becomes correct in 'main' and should not be changed there. If status quo continues, as I currently support and also expect, then the doc should be changed in main as well as in maintenance versions. PR submitted.

@terryjreedy
Copy link
Member

There are two separately mentioned parenthesis requirements in the PEP missing from the Reference manual: (a := 1) instead of a := 1 for an expression statement, and b = (a := 1) instead of a = b: = 1 for an assignment expression as the right-hand side of an assignment statement. Eliminating the latter has not been requested and IMO definitely should not be. My PR adds both requirements. I plan to merge it within a day, and if the former restriction were to be dropped sometime, the doc could be revised again along with the code revision. (And that would only be a feature change for 'main'.)

@terryjreedy terryjreedy changed the title Allow unparenthesized top-level assignment expression Document better assignment expression parentheses requirements Nov 12, 2023
terryjreedy added a commit that referenced this issue Nov 13, 2023
gh-111944: Clarify where assignment expressions require ()s

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 13, 2023
…ythonGH-111977)

pythongh-111944: Clarify where assignment expressions require ()s

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
(cherry picked from commit 9a2f25d)

Co-authored-by: Terry Jan Reedy <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 13, 2023
…ythonGH-111977)

pythongh-111944: Clarify where assignment expressions require ()s

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
(cherry picked from commit 9a2f25d)

Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue Nov 13, 2023
…H-111977) (#112011)

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
(cherry picked from commit 9a2f25d)

Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue Nov 13, 2023
…H-111977) (#112010)

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
(cherry picked from commit 9a2f25d)

Co-authored-by: Terry Jan Reedy <[email protected]>
@lysnikolaou
Copy link
Member Author

I don't think there's much chance of this succeeding, but if you want to pursue it you should open up a discussion on https://discuss.python.org/c/ideas/6

Since there's consensus that this isn't something we want to support, I won't pursue this further.

Thanks folks for your input and @terryjreedy for fixing the docs issue.

aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…ython#111977)

pythongh-111944: Clarify where assignment expressions require ()s

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…ython#111977)

pythongh-111944: Clarify where assignment expressions require ()s

Augment the list of places where parentheses are
required around assignnment statements.  In particular,
'a := 0' and 'a = b := 1' are syntax errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants