|
| 1 | +"""What follows is awful and will be gone in Sphinx 9. |
| 2 | +
|
| 3 | +Instances of _StrPath should not be constructed except in Sphinx itself. |
| 4 | +Consumers of Sphinx APIs should prefer using ``pathlib.Path`` objects |
| 5 | +where possible. _StrPath objects can be treated as equivalent to ``Path``, |
| 6 | +save that ``_StrPath.replace`` is overriden with ``str.replace``. |
| 7 | +
|
| 8 | +To continue treating path-like objects as strings, use ``os.fspath``, |
| 9 | +or explicit string coercion. |
| 10 | +
|
| 11 | +In Sphinx 9, ``Path`` objects will be expected and returned in all instances |
| 12 | +that ``_StrPath`` is currently used. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import sys |
| 18 | +import warnings |
| 19 | +from pathlib import Path, PosixPath, PurePath, WindowsPath |
| 20 | +from typing import Any |
| 21 | + |
| 22 | +from sphinx.deprecation import RemovedInSphinx90Warning |
| 23 | + |
| 24 | +_STR_METHODS = frozenset(str.__dict__) |
| 25 | +_PATH_NAME = Path().__class__.__name__ |
| 26 | + |
| 27 | +_MSG = ( |
| 28 | + 'Sphinx 8 will drop support for representing paths as strings. ' |
| 29 | + 'Use "pathlib.Path" or "os.fspath" instead.' |
| 30 | +) |
| 31 | + |
| 32 | +# https://docs.python.org/3/library/stdtypes.html#typesseq-common |
| 33 | +# https://docs.python.org/3/library/stdtypes.html#string-methods |
| 34 | + |
| 35 | +if sys.platform == 'win32': |
| 36 | + class _StrPath(WindowsPath): |
| 37 | + def replace( # type: ignore[override] |
| 38 | + self, old: str, new: str, count: int = -1, /, |
| 39 | + ) -> str: |
| 40 | + # replace exists in both Path and str; |
| 41 | + # in Path it makes filesystem changes, so we use the safer str version |
| 42 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 43 | + return self.__str__().replace(old, new, count) # NoQA: PLC2801 |
| 44 | + |
| 45 | + def __getattr__(self, item: str) -> Any: |
| 46 | + if item in _STR_METHODS: |
| 47 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 48 | + return getattr(self.__str__(), item) |
| 49 | + msg = f'{_PATH_NAME!r} has no attribute {item!r}' |
| 50 | + raise AttributeError(msg) |
| 51 | + |
| 52 | + def __add__(self, other: str) -> str: |
| 53 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 54 | + return self.__str__() + other |
| 55 | + |
| 56 | + def __bool__(self) -> bool: |
| 57 | + if not self.__str__(): |
| 58 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 59 | + return False |
| 60 | + return True |
| 61 | + |
| 62 | + def __contains__(self, item: str) -> bool: |
| 63 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 64 | + return item in self.__str__() |
| 65 | + |
| 66 | + def __eq__(self, other: object) -> bool: |
| 67 | + if isinstance(other, PurePath): |
| 68 | + return super().__eq__(other) |
| 69 | + if isinstance(other, str): |
| 70 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 71 | + return self.__str__() == other |
| 72 | + return NotImplemented |
| 73 | + |
| 74 | + def __hash__(self) -> int: |
| 75 | + return super().__hash__() |
| 76 | + |
| 77 | + def __getitem__(self, item: int | slice) -> str: |
| 78 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 79 | + return self.__str__()[item] |
| 80 | + |
| 81 | + def __len__(self) -> int: |
| 82 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 83 | + return len(self.__str__()) |
| 84 | +else: |
| 85 | + class _StrPath(PosixPath): |
| 86 | + def replace( # type: ignore[override] |
| 87 | + self, old: str, new: str, count: int = -1, /, |
| 88 | + ) -> str: |
| 89 | + # replace exists in both Path and str; |
| 90 | + # in Path it makes filesystem changes, so we use the safer str version |
| 91 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 92 | + return self.__str__().replace(old, new, count) # NoQA: PLC2801 |
| 93 | + |
| 94 | + def __getattr__(self, item: str) -> Any: |
| 95 | + if item in _STR_METHODS: |
| 96 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 97 | + return getattr(self.__str__(), item) |
| 98 | + msg = f'{_PATH_NAME!r} has no attribute {item!r}' |
| 99 | + raise AttributeError(msg) |
| 100 | + |
| 101 | + def __add__(self, other: str) -> str: |
| 102 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 103 | + return self.__str__() + other |
| 104 | + |
| 105 | + def __bool__(self) -> bool: |
| 106 | + if not self.__str__(): |
| 107 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 108 | + return False |
| 109 | + return True |
| 110 | + |
| 111 | + def __contains__(self, item: str) -> bool: |
| 112 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 113 | + return item in self.__str__() |
| 114 | + |
| 115 | + def __eq__(self, other: object) -> bool: |
| 116 | + if isinstance(other, PurePath): |
| 117 | + return super().__eq__(other) |
| 118 | + if isinstance(other, str): |
| 119 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 120 | + return self.__str__() == other |
| 121 | + return NotImplemented |
| 122 | + |
| 123 | + def __hash__(self) -> int: |
| 124 | + return super().__hash__() |
| 125 | + |
| 126 | + def __getitem__(self, item: int | slice) -> str: |
| 127 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 128 | + return self.__str__()[item] |
| 129 | + |
| 130 | + def __len__(self) -> int: |
| 131 | + warnings.warn(_MSG, RemovedInSphinx90Warning, stacklevel=2) |
| 132 | + return len(self.__str__()) |
0 commit comments