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

PEP 769: Add a 'default' keyword argument to 'attrgetter' and 'itemgetter' #4179

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

facundobatista
Copy link
Member

@facundobatista facundobatista commented Dec 22, 2024

Selected number: 769

This PEP is about adding a default keyword argument to attrgetter and itemgetter (from the operator module`).

Thanks!

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

📚 Documentation preview 📚: https://pep-previews--4179.org.readthedocs.build/pep-0769/

@facundobatista facundobatista requested a review from a team as a code owner December 22, 2024 16:22
@hugovk hugovk added the new-pep A new draft PEP submitted for initial review label Dec 22, 2024
@hugovk
Copy link
Member

hugovk commented Dec 22, 2024

Hello!

Selected number: 790

You can use 790 if you like, but we normally pick the next one in sequence, which currently would be 769. Would you prefer to use 769?

I've also updated the PR description to include the new PEP checklist, please could you work down it and check them off as appropriate?

Thanks!

@hugovk
Copy link
Member

hugovk commented Dec 22, 2024

Please could you also wrap lines to 79 columns? https://peps.python.org/pep-0012/#general

If you have prettier installed, this can do it:

prettier --prose-wrap=always --print-width=79 -w filename.rst

@facundobatista facundobatista changed the title Add PEP about default in attrgetter and itemgetter. PEP 790: Add a 'default' keyword argument to 'attrgetter' and 'itemgetter' Dec 25, 2024
@facundobatista
Copy link
Member Author

Hello @hugovk !

Thanks for the help and info.

  • I've chosen 790 because the biggest one 0xxx was 0789, I didn't search for gaps. I'm fine with using 769, however I have a question: the branch is named add-pep-790, if I rename it (git branch -m add-pep-769) will we get a new PR? is that ok?

  • I wrapped all lines.

  • I ticked all the items in the description, except:

    • "PEP has next available number...": I may change the number, see first question above

    • "PEP builds with no warnings": I'm in that process, will confirm when it's done

    • "Reference Implementation": I may include it after first PEP review pass, when we decide on a detail listed in open issues...

    • "Right before or after initial merging, PEP discussion thread created and linked": I'll create it after merge

I'm working now in making all linters go green.

Thanks again for all the help!

@facundobatista
Copy link
Member Author

Linters and builders fixed!

@hugovk
Copy link
Member

hugovk commented Dec 26, 2024

You're welcome!

  • I've chosen 790 because the biggest one 0xxx was 0789, I didn't search for gaps. I'm fine with using 769, however I have a question: the branch is named add-pep-790, if I rename it (git branch -m add-pep-769) will we get a new PR? is that ok?

Let's go for 769 then.

If you make a new branch, you'll need to make a new PR. But the branch name isn't too important and the branch will be deleted after merge, so I'd just keep the current one.

@facundobatista facundobatista changed the title PEP 790: Add a 'default' keyword argument to 'attrgetter' and 'itemgetter' PEP 769: Add a 'default' keyword argument to 'attrgetter' and 'itemgetter' Dec 26, 2024
@facundobatista
Copy link
Member Author

Cool, thanks! I renamed it to 769, then. Also updated PR title and description.

Let me know when this is ready for the discussion in the forum, and I'll open a thread there.

Thanks!

peps/pep-0790.rst Outdated Show resolved Hide resolved
peps/pep-0790.rst Outdated Show resolved Hide resolved
@@ -650,6 +650,7 @@ peps/pep-0768.rst @pablogsal
peps/pep-0777.rst @warsaw
# ...
peps/pep-0789.rst @njsmith
peps/pep-0790.rst @facundobatista
Copy link
Member

Choose a reason for hiding this comment

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

And move this up a few lines:

Suggested change
peps/pep-0790.rst @facundobatista
peps/pep-0767.rst @facundobatista

except (TypeError, IndexError, KeyError):
value = XYZ

However, this would be not as eficient as we'd want for particular cases,
Copy link
Member

Choose a reason for hiding this comment

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

typo:

Suggested change
However, this would be not as eficient as we'd want for particular cases,
However, this would be not as efficient as we'd want for particular cases,

Comment on lines 145 to 146
About Posible Implementations
-----------------------------
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
About Posible Implementations
-----------------------------
About Possible Implementations
------------------------------

Comment on lines 288 to 289
retrieve the item with a default. See examples in the About Posible
Implementations subsection before.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
retrieve the item with a default. See examples in the About Posible
Implementations subsection before.
retrieve the item with a default. See examples in the About Possible
Implementations subsection above.

Or we could add a reference?

For example, add this above:

+.. _PEP 769 About Possible Implementations:
 
 About Posible Implementations
 -----------------------------

And then:

Suggested change
retrieve the item with a default. See examples in the About Posible
Implementations subsection before.
retrieve the item with a default. See examples in the `About Possible
Implementations <PEP 769 About Possible Implementations_>`__ subsection above.

individual default values to multiple attributes or items. While some
discussions considered allowing multiple defaults, the increased
complexity and potential for confusion led to favoring a single default
value for all cases (more about this below in Rejected Ideas).
Copy link
Member

Choose a reason for hiding this comment

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

We could add similar reference here if we want to hyperlink it.


For the case of ``attrgetter`` is quite direct: it implies using
``getattr`` catching a possible ``AttributeError``. So
``attrgetter("name", default=XYZ)(obj)`` would be like:
Copy link
Member

Choose a reason for hiding this comment

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

So the following code is shown as code, not blockquote:

Suggested change
``attrgetter("name", default=XYZ)(obj)`` would be like:
``attrgetter("name", default=XYZ)(obj)`` would be like::

value = XYZ

Better performance, more complicated to implement and explain. This is
the first case in the Open Issues section later.
Copy link
Member

Choose a reason for hiding this comment

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

This could also be a linked reference.

except (TypeError, IndexError, KeyError):
resutl = default

(however see previous open issue about special case for dictionaries)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(however see previous open issue about special case for dictionaries)
(However see previous open issue about special case for dictionaries.)

@facundobatista
Copy link
Member Author

Thanks! There I followed all your suggestions. I'm not 100% sure if the linked sections is fine, Github is not rendering those correctly but I understand that it may be a Github thing not supporting ReST 100% ok.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-pep A new draft PEP submitted for initial review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants