Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Re-export._replace as.replace inurllib.parse result types#136083

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
thejcannon wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromthejcannon:patch-2

Conversation

thejcannon
Copy link
Contributor

I do Python interviewing, and part of one of our interviews has folks parsing URLs.

I've noticed people gloss over the._replace at the end of the examples inhttps://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse and I'm somewhat convinced that's due to the fact that subconsciously PEP 8 (and beyond) has trained folks to assume leading underscore is not "public" ( a good convention).

This isn't a debate aboutnamedtuple. I assume it uses._replace so thatit doesn't preclude folks from usingnamedtuple with a field namedreplace.

But for concretenamedtuple subclasses which are known not to have areplace field, it'd be nice if the callers could just use.replace like all the other "normal" classes.

(Then there's the issue with linters complaining about usage of using a method that starts with an underscore, but that's not my motivation so I'll just point it out and move on)

(I also don't want this to devolve into a conversation about "ok but what about ". Perfect is the enemy og good)

@thejcannon
Copy link
ContributorAuthor

I've threatened@gpshead with this change a few times, so I'll tag him (not knowing what exactly happens to random PRs againstcpython)

@picnixz
Copy link
Member

picnixz commentedJun 28, 2025
edited
Loading

Please open an issue for such change and add a NEWS entry so that this change is announced (it affects public classes since they inherit from those private ones). I didn't look at what you did though so I don't know if this change will be accepted. But the workflow should be: issue -> discussion -> PR (in that order, possibly skipping discussion if there is a clear issue). TiA.

I assume it uses ._replace so that it doesn't preclude folks from using namedtuple with a field named replace

Yes.

(Then there's the issue with linters complaining about usage of using a method that starts with an underscore, but that's not my motivation so I'll just point it out and move on)

AFAIK, pylint doesn't have this issue because it treats such special methods for namedtuples as public (and theyare publicly documented).

brianschubert reacted with thumbs up emoji

@LamentXU123

This comment was marked as duplicate.

gpshead added a commit to gpshead/cpython that referenced this pull requestJun 29, 2025
…n alternate to ._replaceInspired bypython#136083 because one offs for this kind of thing feel like they'dcreate more of a mess than allowing it for anyone?Signifiantly written by Claude Sonnet 4 in Claude Code.
@gpsheadgpshead marked this pull request as draftJune 29, 2025 05:56
@gpshead
Copy link
Member

(A)

One could ask for this as a more general feature of namedtuple...https://github.com/python/cpython/compare/main...gpshead:cpython:namedtuple-with_replace_method?expand=1 would do that.

Butthat begs the question why._source and._field_defaults don't get the same treatment if added to enable.replace to officially exist?

SO...

(B)

I wholly agree that APIs suggesting actually using ._ prefixed things are cursed. But the other bigger picture is that namedtuple was often a convenient useful mistake of an API. They got used nicely in places where the APIs used to be... actual tuples without names. So they're a transition. Better than before!But decade(s) later, we all look at these and question why they even behave like tuples. Especially when we need to add more fields and cannot because it breaks existing code's fixedun, pack, ing = assignment(). So an annoying to craft but proper API compatible replacement type winds up being desired and recreated: An actual object (non-frozen dataclass like I suppose) with .fields as attributes. Thatalso behaves as a sequence for indexing and unpacking purposes for compatibility's sake impacting the older fields that line up with those indices.

If we want a properly named method, we should probably adopt a common form of those instead of modifying namedtuple or hacking this into some places and not others that use namedtuple in an inconsistent manner.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@thejcannon@picnixz@LamentXU123@gpshead

[8]ページ先頭

©2009-2025 Movatter.jp