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 — GH-100479: Add optional `blueprint` argument to `pathlib.PurePath` by barneygale · Pull Request #100481 · python/cpython

Conversation

@barneygale

@barneygale barneygale commented Dec 23, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Add optional blueprint argument to pathlib.PurePath and Path. This argument is supplied whenever a derivative path is created, such as from PurePath.parent. Subclasses may use this to pass information between paths.

  • Issue: Support for sharing state between pathlib subclasses #100479

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@barneygale
Add pathlib.PurePath.makepath(); unify path object construction
a6fdd0e
@bedevere-bot bedevere-bot added the awaiting review label Dec 23, 2022
@barneygale barneygale changed the title Add pathlib.PurePath.makepath(); unify path object construction gh-100479: Add pathlib.PurePath.makepath(); unify path object construction Dec 23, 2022
@bedevere-bot bedevere-bot mentioned this pull request Dec 23, 2022
Support for sharing state between pathlib subclasses #100479
Closed
merwok
merwok reviewed Dec 24, 2022
View reviewed changes
Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Fix reST role name.
b061747
@barneygale

barneygale commented Dec 24, 2022

Copy link
Copy Markdown
Contributor Author

Hey @serhiy-storchaka, I believe you're pretty familiar with the pathlib internals. Penny for your initial impressions of this patch? It does cut out a key part of the initial pathlib design: the idea of skipping re-parsing and re-normalizing paths in case where we can be sure it's not necessary (e.g. in path.parents, path.iterdir()). I'd love to be able to convince you this is a good idea!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Move call to os.getcwd() back into Path.cwd()
99eb8b1
@barneygale
barneygale force-pushed the gh-100479-add-makepath branch from 19bf6ff to 99eb8b1 Compare December 24, 2022 17:57
@merwok

This comment was marked as resolved.

Sign in to view
@barneygale

This comment was marked as resolved.

Sign in to view
This was referenced Jan 5, 2023
Path takes and ignores **kwargs #74033
Closed
pathlib.Path._from_parsed_parts should call cls.__new__(cls) #76846
Closed
subclasses of pathlib.PurePosixPath never call __init__ or __new__ #85281
Closed
@barneygale
Merge branch 'main' into pythongh-100479-add-makepath
4759d01
@hauntsaninja hauntsaninja added the topic-pathlib label Jan 7, 2023
@barneygale
Merge branch 'main' into pythongh-100479-add-makepath
ef6f4c3
@barneygale barneygale changed the title gh-100479: Add pathlib.PurePath.makepath(); unify path object construction gh-100479: Add pathlib.PurePath.makepath() Apr 3, 2023
@barneygale
Add news blurb.
595b8ae
@barneygale
barneygale marked this pull request as ready for review April 3, 2023 21:05
@barneygale

barneygale commented Apr 3, 2023

Copy link
Copy Markdown
Contributor Author

This is now ready for review! My previous comment no longer applies, as we fixed the pathlib construction weirdness in #102789. This change should be pretty performance-neutral now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale barneygale mentioned this pull request Apr 3, 2023
bpo-24132: Add pathlib._AbstractPath #31085
Closed
zmievsa
zmievsa approved these changes Apr 4, 2023
View reviewed changes

@zmievsa zmievsa left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

After looking through the changes, this is an obvious approve from my side: the changes make a lot sense and we have been waiting for them for a long time.

My biggest concern is that we're adding a breaking change to _from_parsed_parts which is not a big problem on its own because the interface is private, but I would still take a look around for some relatively popular libraries that might actually be dependent on that interface.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Apr 4, 2023
@barneygale

barneygale commented Apr 5, 2023

Copy link
Copy Markdown
Contributor Author

My biggest concern is that we're adding a breaking change to _from_parsed_parts which is not a big problem on its own because the interface is private, but I would still take a look around for some relatively popular libraries that might actually be dependent on that interface.

Well, it's undocumented and underscore-prefixed. We've been breaking other things like that over the last few months, e.g. removing Path._accessor. It's hard to make progress without doing this.

AlexWaygood and zmievsa reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
AlexWaygood self-requested a review April 5, 2023 22:43
barneygale added 3 commits April 9, 2023 18:45
@barneygale
Merge branch 'main' into pythongh-100479-add-makepath
dcfe70a
@barneygale
Add whatsnew entry
117fe4b
@barneygale
Merge branch 'main' into pythongh-100479-add-makepath
e75dedc
@AlexWaygood

AlexWaygood commented Apr 24, 2023

Copy link
Copy Markdown
Member

I'm also slightly concerned that this design might be a little fragile. The following situation seems fairly plausible:

1. Barney takes a break from open source for a few months

2. During Barney's break, a contributor files a PR to add `awesome_new_method` to pathlib. The method is so awesome, it's immediately clear to the core dev team that we desperately need the method.

3. Alex merges the PR, but because Barney's on an open-source break, there's nobody to remind us that the new method should really be calling `self.makepath(*args)` instead of `self.__class__(*args)`. Support for sharing state between pathlib subclasses is now broken :(

Is there a way we can guard against this?

Hm. I expect a user will spot that it doesn't work and log a bug. It doesn't have any wider consequences, I don't think.

Okay, I chatted with Brandt at the sprint, and he persuaded me that I was worrying too much about this hypothetical scenario :)

barneygale reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

FWIW, I am open to alternatives to this new method. There are things we can do like pass an opaque object around, or derive a new type to 'bind' the context. I went back and forth with these, and in the end went with this approach as it appears to be the simplest. But happy to look again if you have suggestions :)

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Gobot1234

Gobot1234 commented Apr 24, 2023

Copy link
Copy Markdown
Contributor

Also as a bit of a testimonial I really like the design and have the changes for something I locally implemented for this PR and it makes my code significantly nicer

barneygale reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood reviewed Apr 24, 2023
View reviewed changes
Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

barneygale added 2 commits April 25, 2023 15:24
@barneygale
Restore _PathParents
ae48454
@barneygale
Add note to parents about potential reference cycle.
e7a8fe3
AlexWaygood
AlexWaygood approved these changes Apr 25, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The implementation and the docs here look great. Thanks for addressing my concerns, and for your patience :)

My only remaining reservation is about the name of the method itself. I think I would still prefer "newpath" -- but if you'd like more opinions, you could always start a thread on Discourse or Discord :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@barneygale

barneygale commented Apr 25, 2023

Copy link
Copy Markdown
Contributor Author

Thanks so much for the thorough review! I'll open a thread about the method name shortly.

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Replace makepath() method with template initialiser argument.
7f12faa
@barneygale barneygale changed the title gh-100479: Add pathlib.PurePath.makepath() gh-100479: Add optional template argument to pathlib.PurePath Apr 25, 2023
@barneygale
barneygale requested a review from AlexWaygood April 25, 2023 17:17
AlexWaygood
AlexWaygood reviewed Apr 25, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Wow, using the template argument instead of adding the makepath method makes it so much easier for me to understand what's going on! This is a great improvement!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2023-04-03-22-02-35.gh-issue-100479.kNBjQm.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Apr 25, 2023

Copy link
Copy Markdown
Contributor Author

Yeah, I was missing the wood for the trees there. Massive improvement.

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale @AlexWaygood
Apply suggestions from code review
687c764
Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood
AlexWaygood approved these changes Apr 25, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM. I guess the only remaining question is whether "template" is the best name for this new parameter. I'm fine with "template", but you might want to head back to Discourse if you want some more bikeshedding :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
barneygale added 2 commits April 25, 2023 18:46
@barneygale
Fix docs for other classes.
d7e326a
@barneygale
Pass template to super() to support diamond inheritance.
a65d499
AlexWaygood
AlexWaygood reviewed Apr 26, 2023
View reviewed changes
Comment thread Doc/library/pathlib.rst Outdated
class MyPath(PurePosixPath):
def __init__(self, *pathsegments, template=None, session_id=None):
super().__init__(*pathsegments)
super().__init__(*pathsegments, template=template)

@AlexWaygood AlexWaygood Apr 26, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Should we add a test to make sure diamond inheritance works?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@barneygale barneygale Apr 26, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm not sure. There's only one place in pathlib.py where we call super(), and that only exists because we need to raise a deprecation warning when additional arguments are supplied to pathlib.Path(). The Path.__init__() method will be removed in 3.14, at which point it will be impossible for the test to fail.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@barneygale barneygale Apr 26, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Perhaps a hidden .. doctest:: block would be best?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@barneygale
Fixed missed template argument to super().
d4b15d7
barneygale
barneygale commented Apr 26, 2023
View reviewed changes
Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

AA-Turner
AA-Turner reviewed Apr 27, 2023
View reviewed changes
Comment thread Doc/library/pathlib.rst Outdated
Comment on lines +153 to +156
The optional *template* argument may provide another path object. It is
supplied whenever a new path object is created from an existing one, such
as in :attr:`parent` or :meth:`relative_to`. Subclasses may use this to
pass information between path objects. For example::

@AA-Turner AA-Turner Apr 27, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Does it make sense to specify that template: Self | None here? I.e. that if template is not None, it will be an instance of the current (user-defined) class.

A

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@barneygale
template --> blueprint
958b183
@barneygale barneygale changed the title gh-100479: Add optional template argument to pathlib.PurePath gh-100479: Add optional blueprint argument to pathlib.PurePath Apr 27, 2023
@barneygale barneygale mentioned this pull request Apr 28, 2023
GH-100479: Add pathlib.PurePath.with_segments() #103975
Merged
@barneygale

barneygale commented Apr 28, 2023

Copy link
Copy Markdown
Contributor Author

I've created a branch/PR that uses a __newpath__() method, for comparison's sake:

  • GH-100479: Add pathlib.PurePath.with_segments() #103975
AlexWaygood and Gobot1234 reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
barneygale marked this pull request as draft April 28, 2023 18:11
@barneygale barneygale changed the title gh-100479: Add optional blueprint argument to pathlib.PurePath GH-100479: Add optional blueprint argument to pathlib.PurePath Apr 29, 2023
@barneygale
Merge branch 'main' into pythongh-100479-add-makepath
1e10188
@barneygale

barneygale commented May 4, 2023

Copy link
Copy Markdown
Contributor Author

Closing in favour of #103975.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale barneygale closed this May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merwok merwok merwok left review comments
@AA-Turner AA-Turner AA-Turner left review comments
@AlexWaygood AlexWaygood AlexWaygood approved these changes
+2 more reviewers
@zmievsa zmievsa zmievsa approved these changes
@Gobot1234 Gobot1234 Gobot1234 left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review topic-pathlib

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@barneygale @merwok @AlexWaygood @Gobot1234 @AA-Turner @zmievsa @hauntsaninja @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.