Skip to content
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

Syntactic sugar PEP draft #1

Closed
wants to merge 24 commits into from
Closed

Syntactic sugar PEP draft #1

wants to merge 24 commits into from

Conversation

joshuabambrick
Copy link
Owner

@joshuabambrick joshuabambrick commented Nov 11, 2023

Context: python#3531

Basic requirements (all PEP Types)

  • Read and followed PEP 1 & PEP 12
  • File created from the latest PEP template
  • PEP has next available number, & set in filename (pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) and PEP header
  • Title clearly, accurately and concisely describes the content in 79 characters or less
  • Core dev/PEP editor listed as Author or Sponsor, and formally confirmed their approval
  • Author, Status (Draft), Type and Created headers filled out correctly
  • PEP-Delegate, Topic, Requires and Replaces headers completed if appropriate
  • Required sections included
    • Abstract (first section)
    • Copyright (last section; exact wording from template required)
  • Code is well-formatted (PEP 7/PEP 8) and is in code blocks, with the right lexer names if non-Python
  • PEP builds with no warnings, pre-commit checks pass and content displays as intended in the rendered HTML
  • Authors/sponsor added to .github/CODEOWNERS for the PEP

Standards Track requirements

  • PEP topic discussed in a suitable venue with general agreement that a PEP is appropriate
  • Suggested sections included (unless not applicable)
    • Motivation
    • Rationale
    • Specification
    • Backwards Compatibility
    • Security Implications
    • How to Teach This
    • Reference Implementation
    • Rejected Ideas
    • Open Issues
  • Python-Version set to valid (pre-beta) future Python version, if relevant
  • Any project stated in the PEP as supporting/endorsing/benefiting from the PEP formally confirmed such
  • Right before or after initial merging, PEP discussion thread created and linked to in Discussions-To and Post-History

Copy link
Owner Author

@joshuabambrick joshuabambrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other PEPs seem to use a max line length of 80; I'm unsure if that is a recommended practice or just a result of the text editors used by the authors.

Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 28-Oct-2023
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear exactly what date this should be but presumably ~ the date that we create the PR to python/peps

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Created header records the date that the PEP was assigned a number

https://peps.python.org/pep-0001/#pep-header-preamble

So yes, that will be when opening the PR.

Lines saved 290 247 935 794
===================================================================== ================ ============== =============== =====================

.. _a: https://github.com/python/cpython/pull/111423/
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to move these to the bottom


The syntax is ugly
--------------------------------------------
This objection is by far the most common. On the contrary, we argue that:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may want a less argumentative term than 'argue', e.g. 'believe' or 'contend'.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there's a lovely pun in calling the word argue "argumentative", it's also not too much of a problem here (eg imagine a mathematician saying "in the debate about pi vs tau, we argue that the elegance of measuring a circle by its diameter outweighs the elegance of radial measures being proportions of an entire turn").

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, thanks


Recommendations
========================
As with any other language feature, the programmer should be judicious about whether using this feature improves their code based on the context. We do not advocate for enforcing a rule of using this feature in all cases where it may be applicable.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want some more concrete recommendations.

Context:

I would appreciate a few examples, in any PEP for this, which show when it is not appropriate. I think that would be enough to guide people away from overzealously trying to enforce this usage on everyone.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the programmer should be judicious about whether using this feature improves their code based on the context

Sounds weird to me. How about "the programmer needs to exercise their own judgement..."?

Although I think the entire paragraph is a bit too defensive and maybe not needed?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't follow up on your comment although I did reword the paragraph. Let me know if you still l

Although I think the entire paragraph is a bit too defensive and maybe not needed?

The context behind adding this was this section of the discussion. There were a few who noted that the universal use of this syntax in all applicable contexts was not advisable based on my linked PR. I generally agree and thought that adding a comment to indicate that we aren't recommending that the use of this feature be enforced universally would help address that concern. There were suggestions at giving some examples as to where it would be most useful but my sense is that these are best left to the discretion of the programmer.

peps/pep-9999.rst Outdated Show resolved Hide resolved
@hugovk
Copy link

hugovk commented Nov 11, 2023

Other PEPs seem to use a max line length of 80; I'm unsure if that is a recommended practice or just a result of the text editors used by the authors.

It's recommended by PEP 12:

Lines should usually not extend past column 79, excepting URLs and similar circumstances.

https://peps.python.org/pep-0012/#general

So that's max length of 80, given a start in column 0.

@hugovk
Copy link

hugovk commented Nov 11, 2023

Tip: enable GitHub Actions for your fork to enable some linting:

https://github.com/joshuabambrick/peps/actions

@Rosuav
Copy link

Rosuav commented Nov 11, 2023

I think you might have created a PR in your own fork, rather than creating one in the PEPs repository. Once you've dealt with any current suggestions, I recommend remaking the PR (from the same branch in the same fork). The link to do so should, if I've crafted this correctly, be this:

python/peps@main...joshuabambrick:peps:sugar

At the same time, you could claim yourself a PEP number. We have existing PEPs up to 733 (ahh, my old home bus route... good times), and PEP 734 is claimed by PR python#3523, so PEP 735 would be yours.

------------------------------------
A common problem is that semantically identical variables have different names depending on their contexts. This syntax would encourage authors to use the same variable name when calling a function as the argument name, which would increase consistency of variable names used and hence also *readability*.

Naturally extends to dictionaries
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The heading "Naturally extends" will imply that this syntax could be used in a dictionary display, but what you're proposing is not that. Not sure how to word it, but what you're giving is an elegant use of the dict() constructor to achieve this goal without extending anything syntactically.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed this to 'Applicability to dictionary construction'

peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
The feature adds another way of doing things
--------------------------------------------

The same argument can be made against all syntax changes. This is a simple syntactic sugar, much as ``x += 1`` is sugar for ``x = x + 1``. This isn't tantamount to a 'new way' of passing arguments but a more readable notation for the same way.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notably, x += 1 is not mere syntactic sugar for x = x + 1; this can be demonstrated with lists, where x = [1,2]; x += [3,4] will mutate the list in place, but x = x + [3,4] will not. Here's an example where it really truly is syntactic sugar: https://peps.python.org/pep-3135/ Although this particular one, while quite popular, has raised some eyebrows for being too magical and not something to be replicated.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't directly reply to this. You are right to point it out.

I tried to address your comment by adding 'when x is an integer'.

I'd be disinclined to point to a more contested syntactic sugar (in fact, I think I'd agree that 3135 is too magical) and I think x += 1 is an example of a widely popular syntactic sugar which some could argue introduces 'another way of doing things'. Plus, while technically __iadd__ can be overridden arbitrarily, in practice it almost always is used to offer syntactic sugar for something which could be written another way.

Please do let me know if you still don't like the wording.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason this "feels wrong" is that adding another way of doing things isn't the problem. Python's Zen does not say "there's only one way to do things, you do it our way or you don't do it at all". The Zen says that there is one OBVIOUS way to do things. Given that Python was Turing-complete before this proposal (or any other), by definition ANY proposal is adding another way of doing things. Maybe this entire paragraph is arguing against something that doesn't matter?

Perhaps, if you want to argue about syntactic sugar's benefit, the matrix multiplication operator would be a good place to start. Numpy users for a long time were able to do both types of multiplication, but one of them had an operator and the other was done with a method. Could you do what you wanted? Sure. Was it the way you wanted to write your code? No. And when code isn't as expressive as you'd like, there's the possibility of bugs slipping in.

The benefit of code being obvious is precisely that, in fact: bugs become visible. It is trivially easy to see when you have simple code that's doing a simple task, and you can equally easily see if it's failing to do that task. This proposal achieves that by raising from the concrete "take variable spamination and pass it as the named parameter spamination" to the abstract "pass named parameter spamination as itself".

peps/pep-9999.rst Outdated Show resolved Hide resolved
Copy link

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run linting locally with pip install pre-commit && pre-commit run --all-files

peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
.. [12] Shorten Keyword Arguments with Implicit Notation: foo(a=a, b=b) to foo(.a, .b) (2023)
https://discuss.python.org/t/shorten-keyword-arguments-with-implicit-notation-foo-a-a-b-b-to-foo-a-b/33080
.. [13] Syntactic sugar to encourage use of named arguments (2023)
https://discuss.python.org/t/syntactic-sugar-to-encourage-use-of-named-arguments/36217
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where appropriate, it would be better to move these references into inline links.

Two benefits:

  • Saves users a click
  • They can be a pain to maintain/renumber as the text evolves.

peps/pep-9999.rst Outdated Show resolved Hide resolved
@joshuabambrick
Copy link
Owner Author

You can run linting locally with pip install pre-commit && pre-commit run --all-files

Thanks for the tip. Now passing.

@joshuabambrick
Copy link
Owner Author

Thanks a lot both for the feedback. Will move this to a PR on the main repo when CI passes.

@joshuabambrick
Copy link
Owner Author

joshuabambrick commented Nov 28, 2023

Actually, before that, let me double check whether you have any tips for what to change (if anything) in CODEOWNERS

Update: having read around, I understand that it should be @gvanrossum who offered to sponsor the PEP. Please let me know if you have any issue with this before I create the PR.

@gvanrossum
Copy link

gvanrossum commented Nov 28, 2023

I’m fine with that. (Note that I’m neutral about the proposal, I just think that it is worth having a PEP for it.)

@boxed
Copy link

boxed commented Nov 28, 2023

Very good write up and good job summarizing the discussion so far! Thanks for your work on this.

@gvanrossum
Copy link

(Drive-by comment: "Syntactic sugar" is a very generic concept. This PEP provides syntactic sugar for a specific situation (keyword argument value is a variable with the same name as the keyword). Could you be more specific in the PR title?)

@joshuabambrick
Copy link
Owner Author

@gvanrossum

I’m fine with that. (Note that I’m neutral about the proposal, I just think that it is worth having a PEP for it.)

I understood that we needed a core dev as a sponsor, but apparently that requirement is a bit more relaxed in practice and it was suggested that we may not need one. Please let me know whether you'd prefer to remain as sponsor or not.

(Drive-by comment: "Syntactic sugar" is a very generic concept. This PEP provides syntactic sugar for a specific situation (keyword argument value is a variable with the same name as the keyword). Could you be more specific in the PR title?)

That's a good observation. I will try to find something more descriptive while remaining succinct.

@Rosuav
Copy link

Rosuav commented Dec 2, 2023

Possibly "Keyword argument shorthand notation"?

Copy link

@Rosuav Rosuav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly just "Shorthand syntax" rather than "Shorthand syntactic sugar"?

@hugovk
Copy link

hugovk commented Dec 2, 2023

https://peps.python.org/pep-0001/#submitting-a-PEP says we need at least one of:

  • core dev co-author
  • core dev sponsor
  • PEP editor sponsor
  • SC-appointed sponsor

Chris is a PEP editor, Guido is a PEP editor and core dev.

Right now we meet the criteria with Guido as sponsor.

We could also have Chris as sponsor instead of Guido, if you prefer.

(There's slightly different semantics to being a co-author - "I propose this PEP" - to a sponsor - "I will assist in the PEP process" - but let's not worry too much about this.)

PS maybe we should allow PEP editors to be co-author without a sponsor. After all, the sponsor is there as a PEP process guide, and a PEP editor should be fine without a guide.

@joshuabambrick
Copy link
Owner Author

joshuabambrick commented Dec 2, 2023

Ah I need to set up notifications for github comments.

Possibly just "Shorthand syntax" rather than "Shorthand syntactic sugar"?

I'm happy with 'syntax' over 'syntactic sugar' (my thinking was to emphasise that it is not a major new syntax, just a shorter way of writing something already supported but it's probably needlessly verbose).

Do you prefer to drop 'for keyword arguments at invocation'? My thinking there was to make the name more specific.

@Rosuav
Copy link

Rosuav commented Dec 2, 2023

Do you prefer to drop 'for keyword arguments at invocation'? My thinking there was to make the name more specific.

No no, keep that part, I just thought about abbreviating the beginning of it. Specific is definitely good.

@joshuabambrick joshuabambrick force-pushed the sugar branch 2 times, most recently from b471b1a to 177ef6a Compare December 2, 2023 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants