-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-74453: Deprecate os.path.commonprefix #144436
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
Changes from 3 commits
f381a5e
e8ca19c
cc320d1
879d6ec
1f2086b
166c39d
f58e68f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,8 @@ | |
| """ | ||
|
|
||
| __all__ = ["ascii_letters", "ascii_lowercase", "ascii_uppercase", "capwords", | ||
| "digits", "hexdigits", "octdigits", "printable", "punctuation", | ||
| "whitespace", "Formatter", "Template"] | ||
| "commonprefix", "digits", "hexdigits", "octdigits", "printable", | ||
| "punctuation", "whitespace", "Formatter", "Template"] | ||
|
|
||
| import _string | ||
|
|
||
|
|
@@ -48,6 +48,27 @@ def capwords(s, sep=None): | |
| return (sep or ' ').join(map(str.capitalize, s.split(sep))) | ||
|
|
||
|
|
||
| def commonprefix(m, /): | ||
| """Given a list of strings, returns the longest common leading component.""" | ||
| if not m: | ||
| return '' | ||
| # Note that previously this function was in the 'os.path' module, hence the | ||
| # handling for paths. Maintain compatibility so users have a 1-to-1 drop-in. | ||
| # Some people pass in a list of pathname parts to operate in an OS-agnostic | ||
| # fashion; don't try to translate in that case as that's an abuse of the | ||
| # API and they are already doing what they need to be OS-agnostic and so | ||
| # they most likely won't be using an os.PathLike object in the sublists. | ||
| if not isinstance(m[0], (list, tuple)): | ||
| import os | ||
| m = tuple(map(os.fspath, m)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really fond of this. string.commonprefix() should be specific to strings and should not care about pathlike objects.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is only done for backwards compatibility. I would be happy to get rid of it, but I figured that this would be not allowed considering we're doing a "rename", not a new API.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no backwards compatibility here. We create a new function in a module that is not related to filepaths at all. I do not consider this as a rename, I really consider this as a new API. Instead, I would keep the implementation of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this new We're not doing anyone favours by changing the behaviour and making them [have to decide whether they need to] jump through extra hoops. Chances are they should be using I doubt there's significant performance improvement from changing it either.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me this isn't about performance, it's about semantics. |
||
| s1 = min(m) | ||
| s2 = max(m) | ||
| for i, c in enumerate(s1): | ||
| if c != s2[i]: | ||
| return s1[:i] | ||
| return s1 | ||
|
|
||
|
|
||
| #################################################################### | ||
| _sentinel_dict = {} | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.