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

Simplified Path declarations #2246

Merged
merged 2 commits into from
Nov 27, 2023
Merged

Conversation

HalfWhitt
Copy link
Contributor

This might very well be a solution for a non-problem, but I noticed that a lot of paths in the codebase are written with all slash operators, like this:

Path.home() / "does" / "not" / "exist" / "image.jpg"
Path("does") / "not" / "exist" / "image.jpg"

All this PR does is simplify all such instances , e.g.:

Path.home() / "does/not/exist/image.jpg"
Path("does/not/exist/image.jpg")

This avoids creating and discarding intermediate Path objects along the way, and at least personally I find it more readable too.

In the event that this is an intentional style decision, I could submit a PR that instead changes all of the latter style to the former, for consistency.

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@freakboy3742
Copy link
Member

In the event that this is an intentional style decision, I could submit a PR that instead changes all of the latter style to the former, for consistency.

So ... it was intentional, for two reasons:

  1. Having the / operator provides an easy split point for Black to work on, rather than needing to split long paths.
  2. Windows :-)

(1) is a relatively minor cosmetic detail; but (2) is more substantial. I was under the impression that using an explicit / operator avoided any potential ambiguity around Windows path separators.

However, if that's not the case, and you can provide documented evidence that the forward-slash handling in Path objects is intentionally cross-platform, then I don't object to collapsing the paths to a simpler "single string" form where it clearly improves readability.

@HalfWhitt
Copy link
Contributor Author

HalfWhitt commented Nov 27, 2023

I'm pretty certain that treating / inside strings as a platform-independent separator is one of benefits of pathlib over all the os.path.join shenanigans it replaces.

Oddly enough, looking now I see that this isn't explicitly claimed in the docs, but it is demonstrated by the examples, for instance:

The string representation of a path is the raw filesystem path itself (in native form, e.g. with backslashes under Windows), which you can pass to any function taking a file path as a string:

p = PurePath('/etc')
str(p)
'/etc'
p = PureWindowsPath('c:/Program Files')
str(p)
'c:\\Program Files'

Many other examples there show creation of Windows paths using forward slashes.

(Rest in peace, os.path.join. Please don't come back.)

@HalfWhitt
Copy link
Contributor Author

Not sure if a more-explicit-but-less-authoritative source is relevant, but Trey Hunner talks about it in the second of his pair of pathlib-prozelytizing posts that I liked sharing around, especially when pathlib was quite new and many hadn't used it yet.

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

The combination of a weak docs reference, Trey's opinion, and a test suite passing on Windows is enough to convince me. Thanks for the cleanup!

@freakboy3742 freakboy3742 merged commit e168ed5 into beeware:main Nov 27, 2023
@HalfWhitt HalfWhitt deleted the simplify-paths branch November 27, 2023 01:58
@mhsmith
Copy link
Member

mhsmith commented Nov 27, 2023

Thanks for doing this – if you feel ambitious, you could try doing the same in Briefcase. 😄

@HalfWhitt
Copy link
Contributor Author

Don't mind if I do : )

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.

3 participants