feat: make PebbleClient file methods also accept pathlib.PurePath#2097
Conversation
781c4ae to
762a4da
Compare
benhoyt
left a comment
There was a problem hiding this comment.
Looks reasonable to me, thanks. We should probably have unit tests that exercise this with a pathlib.Path instance, at least for the pebble.py methods.
| def pull( | ||
| self, path: str | pathlib.PurePath, *, encoding: str | None = 'utf-8' | ||
| ) -> BinaryIO | TextIO: | ||
| path = str(path) |
There was a problem hiding this comment.
I think this type and implementation makes sense given the existing types.
However, I'm just curious: for new code, do you know if using the type os.PathLike and path = os.fspath(path) be the more general approach?
There was a problem hiding this comment.
Yes, that would be the 'right' way to do it, though annoyingly os.PathLike is generic because __fspath__ can return str or bytes, so we'd probably want to use os.PathLike[str].
Also for new code I'd tend towards using path = pathlib.Path(path) internally where possible.
There was a problem hiding this comment.
Also for new code I'd tend towards using
path = pathlib.Path(path)internally where possible.
In that case, would it not be better to make that change here too? It's more invasive, but I doubt significantly so, and if it's the better choice, wouldn't we rather do that now? It seems unlikely it would happen in the future.
There was a problem hiding this comment.
For these pebble methods specifically, I don't think there's a benefit in working with Path objects for the arguments, since they're typically immediately just used as values in a dict for a request.
Merge branch 'main' into 25-10+feat+make-pebble-accept-pathlib-path
EDIT: I guess this opening paragraph is wrong, since the
However, the current tests exercising this seem to be Harness based, so they're using Harness's mock pebble client instead of the real one. Would it be worth perhaps parametrizing (or just duplicating) a number of the |
benhoyt
left a comment
There was a problem hiding this comment.
Ah, in that case, the tests we have already is probably fine.
tonyandrewmeyer
left a comment
There was a problem hiding this comment.
I do think we should have some new tests here (I would guess there would be existing pebble ones that could just be parametrized). Partly because of your edit to the earlier comment, but mostly because I feel that since pebble.py can be used as a standalone Python Pebble API module, it shouldn't rely on Ops tests for test coverage.
For consistency, do you think exec's working_dir should also have this change?
| def pull( | ||
| self, path: str | pathlib.PurePath, *, encoding: str | None = 'utf-8' | ||
| ) -> BinaryIO | TextIO: | ||
| path = str(path) |
There was a problem hiding this comment.
Also for new code I'd tend towards using
path = pathlib.Path(path)internally where possible.
In that case, would it not be better to make that change here too? It's more invasive, but I doubt significantly so, and if it's the better choice, wouldn't we rather do that now? It seems unlikely it would happen in the future.
This would be more consistent overall. The reason I didn't cover this parameter is because I think it would be fine to merge this PR without adding this additional feature. However, since we won't be merging without adding additional tests, it shouldn't be much work to also add
|
…tible Revert "feat: use os.fspath for converting Paths to strings" This reverts commit 3a6886c.
|
I've opted not to bundle switching from It's technically not backwards compatible. Previously if someone used If we want to switch, I think it should be a separate PR, so it can be more easily reverted if it causes trouble. |
tonyandrewmeyer
left a comment
There was a problem hiding this comment.
Thanks for adding the tests! Looks good to me.
I got tripped up while working on #2087 with methods like
ops.pebble.PebbleClient.pushonly accepting astrfor theirpathargument, while the wrapper methods likeops.Container.pushacceptstr | pathlib.PurePath. Nowadays pretty much everything you're likely to run into in thestdlibthat operates on file paths will accept path-like objects, so it seems nice to have the same consistency here.PebbleClientmethods to also acceptpathlib.PurePath, immediately converting the argument tostrto avoid the signature change affecting their implementation._TestingPebbleClienthave been updated the same way.ops.Containercalls to these methods have been updated to not convert thepathargument tostrsince it will be converted after calling anyway.