Skip to content

Use set comprehension for posixpath.commonpath() #117641

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

Closed
nineteendo opened this issue Apr 8, 2024 · 0 comments
Closed

Use set comprehension for posixpath.commonpath() #117641

nineteendo opened this issue Apr 8, 2024 · 0 comments
Labels
type-feature A feature request or enhancement

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Apr 8, 2024

Feature or enhancement

Proposal:

We can use a set comprehension to check if no absolute and relative paths are mixed:

-try:
-    isabs, = set(p[:1] == sep for p in paths)
-except ValueError:
-    raise ValueError("Can't mix absolute and relative paths") from None
+if len({p.startswith(sep) for p in paths}) != 1:
+    raise ValueError("Can't mix absolute and relative paths")
-prefix = sep if isabs else sep[:0]
+prefix = sep if paths[0].startswith(sep) else sep[:0]

This is faster and more readable.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants