-
Notifications
You must be signed in to change notification settings - Fork 2k
/
_pathlib.py
48 lines (40 loc) · 2.08 KB
/
_pathlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""What follows is awful and will be gone in Sphinx 8"""
from __future__ import annotations
import sys
import warnings
from pathlib import Path, PosixPath, WindowsPath
from sphinx.deprecation import RemovedInSphinx80Warning
_STR_METHODS = frozenset(str.__dict__)
_PATH_NAME = Path().__class__.__name__
if sys.platform == 'win32':
class _StrPath(WindowsPath):
def replace(self, old, new, count=-1, /):
# replace exists in both Path and str;
# in Path it makes filesystem changes, so we use the safer str version
warnings.warn('Sphinx 8 will drop support for representing paths as strings. '
'Use "pathlib.Path" or "os.fspath" instead.',
RemovedInSphinx80Warning, stacklevel=2)
return str(self).replace(old, new, count)
def __getattr__(self, item):
if item in _STR_METHODS:
warnings.warn('Sphinx 8 will drop support for representing paths as strings. '
'Use "pathlib.Path" or "os.fspath" instead.',
RemovedInSphinx80Warning, stacklevel=2)
return getattr(str(self), item)
msg = f'{_PATH_NAME!r} has no attribute {item!r}'
raise AttributeError(msg)
else:
class _StrPath(PosixPath):
def replace(self, old, new, count=-1, /):
warnings.warn('Sphinx 8 will drop support for representing paths as strings. '
'Use "pathlib.Path" or "os.fspath" instead.',
RemovedInSphinx80Warning, stacklevel=2)
return str(self).replace(old, new, count)
def __getattr__(self, item):
if item in _STR_METHODS:
warnings.warn('Sphinx 8 will drop support for representing paths as strings. '
'Use "pathlib.Path" or "os.fspath" instead.',
RemovedInSphinx80Warning, stacklevel=2)
return getattr(str(self), item)
msg = f'{_PATH_NAME!r} has no attribute {item!r}'
raise AttributeError(msg)