-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
_StrPath
is dead; long live _StrPath
#12690
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
"""What follows is awful and will be gone in Sphinx 9. | ||
|
||
Instances of _StrPath should not be constructed except in Sphinx itself. | ||
Consumers of Sphinx APIs should prefer using ``pathlib.Path`` objects | ||
where possible. _StrPath objects can be treated as equivalent to ``Path``, | ||
save that ``_StrPath.replace`` is overriden with ``str.replace``. | ||
|
||
To continue treating path-like objects as strings, use ``os.fspath``, | ||
or explicit string coercion. | ||
|
||
In Sphinx 9, ``Path`` objects will be expected and returned in all instances | ||
that ``_StrPath`` is currently used. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import sys | ||
import warnings | ||
from pathlib import Path, PosixPath, PurePath, WindowsPath | ||
from typing import Any | ||
|
||
from sphinx.deprecation import RemovedInSphinx90Warning | ||
|
||
_STR_METHODS = frozenset(str.__dict__) | ||
_PATH_NAME = Path().__class__.__name__ | ||
|
||
_MSG = ( | ||
'Sphinx 8 will drop support for representing paths as strings. ' | ||
'Use "pathlib.Path" or "os.fspath" instead.' | ||
) | ||
|
||
# https://docs.python.org/3/library/stdtypes.html#typesseq-common | ||
# https://docs.python.org/3/library/stdtypes.html#string-methods | ||
|
||
if sys.platform == 'win32': | ||
class _StrPath(WindowsPath): | ||
def replace( # type: ignore[override] | ||
self, old: str, new: str, count: int = -1, /, | ||
) -> str: | ||
# replace exists in both Path and str; | ||
# in Path it makes filesystem changes, so we use the safer str version | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__().replace(old, new, count) # NoQA: PLC2801 | ||
|
||
def __getattr__(self, item: str) -> Any: | ||
if item in _STR_METHODS: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return getattr(self.__str__(), item) | ||
msg = f'{_PATH_NAME!r} has no attribute {item!r}' | ||
raise AttributeError(msg) | ||
|
||
def __add__(self, other: str) -> str: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__() + other | ||
|
||
def __bool__(self) -> bool: | ||
if not self.__str__(): | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return False | ||
return True | ||
|
||
def __contains__(self, item: str) -> bool: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return item in self.__str__() | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if isinstance(other, PurePath): | ||
return super().__eq__(other) | ||
if isinstance(other, str): | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__() == other | ||
return NotImplemented | ||
|
||
def __hash__(self) -> int: | ||
return super().__hash__() | ||
|
||
def __getitem__(self, item: int | slice) -> str: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__()[item] | ||
|
||
def __len__(self) -> int: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return len(self.__str__()) | ||
else: | ||
class _StrPath(PosixPath): | ||
def replace( # type: ignore[override] | ||
self, old: str, new: str, count: int = -1, /, | ||
) -> str: | ||
# replace exists in both Path and str; | ||
# in Path it makes filesystem changes, so we use the safer str version | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__().replace(old, new, count) # NoQA: PLC2801 | ||
|
||
def __getattr__(self, item: str) -> Any: | ||
if item in _STR_METHODS: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return getattr(self.__str__(), item) | ||
msg = f'{_PATH_NAME!r} has no attribute {item!r}' | ||
raise AttributeError(msg) | ||
|
||
def __add__(self, other: str) -> str: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__() + other | ||
|
||
def __bool__(self) -> bool: | ||
if not self.__str__(): | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return False | ||
return True | ||
|
||
def __contains__(self, item: str) -> bool: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return item in self.__str__() | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if isinstance(other, PurePath): | ||
return super().__eq__(other) | ||
if isinstance(other, str): | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__() == other | ||
return NotImplemented | ||
|
||
def __hash__(self) -> int: | ||
return super().__hash__() | ||
|
||
def __getitem__(self, item: int | slice) -> str: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return self.__str__()[item] | ||
|
||
def __len__(self) -> int: | ||
warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) | ||
return len(self.__str__()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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 this say Sphinx 9?
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.
Thanks, done in 46ce447.
A