8000 pathlib ABCs: raise `UnsupportedOperation` directly. (#114776) · python/cpython@1667c28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1667c28

Browse files
authored
pathlib ABCs: raise UnsupportedOperation directly. (#114776)
Raise `UnsupportedOperation` directly, rather than via an `_unsupported()` helper, to give human readers and IDEs/typecheckers/etc a bigger hint that these methods are abstract.
1 parent a06b606 commit 1667c28

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

Lib/pathlib/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,8 @@ class Path(_abc.PathBase, PurePath):
514514
as_uri = PurePath.as_uri
515515

516516
@classmethod
517-
def _unsupported(cls, method_name):
518-
msg = f"{cls.__name__}.{method_name}() is unsupported on this system"
519-
raise UnsupportedOperation(msg)
517+
def _unsupported_msg(cls, attribute):
518+
return f"{cls.__name__}.{attribute} is unsupported on this system"
520519

521520
def __init__(self, *args, **kwargs):
522521
if kwargs:

Lib/pathlib/_abc.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -149,39 +149,39 @@ class PathModuleBase:
149149
"""
150150

151151
@classmethod
152-
def _unsupported(cls, attr):
153-
raise UnsupportedOperation(f"{cls.__name__}.{attr} is unsupported")
152+
def _unsupported_msg(cls, attribute):
153+
return f"{cls.__name__}.{attribute} is unsupported"
154154

155155
@property
156156
def sep(self):
157157
"""The character used to separate path components."""
158-
self._unsupported('sep')
158+
raise UnsupportedOperation(self._unsupported_msg('sep'))
159159

160160
def join(self, path, *paths):
161161
"""Join path segments."""
162-
self._unsupported('join()')
162+
raise UnsupportedOperation(self._unsupported_msg('join()'))
163163

164164
def split(self, path):
165165
"""Split the path into a pair (head, tail), where *head* is everything
166166
before the final path separator, and *tail* is everything after.
167167
Either part may be empty.
168168
"""
169-
self._unsupported('split()')
169+
raise UnsupportedOperation(self._unsupported_msg('split()'))
170170

171171
def splitdrive(self, path):
172172
"""Split the path into a 2-item tuple (drive, tail), where *drive* is
173173
a device name or mount point, and *tail* is everything after the
174174
drive. Either part may be empty."""
175-
self._unsupported('splitdrive()')
175+
raise UnsupportedOperation(self._unsupported_msg('splitdrive()'))
176176

177177
def normcase(self, path):
178178
"""Normalize the case of the path."""
179-
self._unsupported('normcase()')
179+
raise UnsupportedOperation(self._unsupported_msg('normcase()'))
180180

181181
def isabs(self, path):
182182
"""Returns whether the path is absolute, i.e. unaffected by the
183183
current directory or drive."""
184-
self._unsupported('isabs()')
184+
raise UnsupportedOperation(self._unsupported_msg('isabs()'))
185185

186186

187187
class PurePathBase:
@@ -505,16 +505,15 @@ class PathBase(PurePathBase):
505505
_max_symlinks = 40
506506

507507
@classmethod
508-
def _unsupported(cls, method_name):
509-
msg = f"{cls.__name__}.{method_name}() is unsupported"
510-
raise UnsupportedOperation(msg)
508+
def _unsupported_msg(cls, attribute):
509+
return f"{cls.__name__}.{attribute} is unsupported"
511510

512511
def stat(self, *, follow_symlinks=True):
513512
"""
514513
Return the result of the stat() system call on this path, like
515514
os.stat() does.
516515
"""
517-
self._unsupported("stat")
516+
raise UnsupportedOperation(self._unsupported_msg('stat()'))
518517

519518
def lstat(self):
520519
"""
@@ -703,7 +702,7 @@ def open(self, mode='r', buffering=-1, encoding=None,
703702
Open the file pointed by this path and return a file object, as
704703
the built-in open() function does.
705704
"""
706-
self._unsupported("open")
705+
raise UnsupportedOperation(self._unsupported_msg('open()'))
707706

708707
def read_bytes(self):
709708
"""
@@ -744,7 +743,7 @@ def iterdir(self):
744743
The children are yielded in arbitrary order, and the
745744
special entries '.' and '..' are not included.
746745
"""
747-
self._unsupported("iterdir")
746+
raise UnsupportedOperation(self._unsupported_msg('iterdir()'))
748747

749748
def _scandir(self):
750749
# Emulate os.scandir(), which returns an object that can be used as a
@@ -871,7 +870,7 @@ def absolute(self):
871870
872871
Use resolve() to resolve symlinks and remove '..' segments.
873872
"""
874-
self._unsupported("absolute")
873+
raise UnsupportedOperation(self._unsupported_msg('absolute()'))
875874

876875
@classmethod
877876
def cwd(cls):
@@ -886,7 +885,7 @@ def expanduser(self):
886885
""" Return a new path with expanded ~ and ~user constructs
887886
(as returned by os.path.expanduser)
888887
"""
889-
self._unsupported("expanduser")
888+
raise UnsupportedOperation(self._unsupported_msg('expanduser()'))
890889

891890
@classmethod
892891
def home(cls):
@@ -898,7 +897,7 @@ def readlink(self):
898897
"""
899898
Return the path to which the symbolic link points.
900899
"""
901-
self._unsupported("readlink")
900+
raise UnsupportedOperation(self._unsupported_msg('readlink()'))
902901
readlink._supported = False
903902

904903
def resolve(self, strict=False):
@@ -973,27 +972,27 @@ def symlink_to(self, target, target_is_directory=False):
973972
Make this path a symlink pointing to the target path.
974973
Note the order of arguments (link, target) is the reverse of os.symlink.
975974
"""
976-
self._unsupported("symlink_to")
975+
raise UnsupportedOperation(self._unsupported_msg('symlink_to()'))
977976

978977
def hardlink_to(self, target):
979978
"""
980979
Make this path a hard link pointing to the same file as *target*.
981980
982981
Note the order of arguments (self, target) is the reverse of os.link's.
983982
"""
984-
self._unsupported("hardlink_to")
983+
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
985984

986985
def touch(self, mode=0o666, exist_ok=True):
987986
"""
988987
Create this file with the given access mode, if it doesn't exist.
989988
"""
990-
self._unsupported("touch")
989+
raise UnsupportedOperation(self._unsupported_msg('touch()'))
991990

992991
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
993992
"""
994993
Create a new directory at this given path.
995994
"""
996-
self._unsupported("mkdir")
995+
raise UnsupportedOperation(self._unsupported_msg('mkdir()'))
997996

998997
def rename(self, target):
999998
"""
@@ -1005,7 +1004,7 @@ def rename(self, target):
10051004
10061005
Returns the new Path instance pointing to the target path.
10071006
"""
1008-
self._unsupported("rename")
1007+
raise UnsupportedOperation(self._unsupported_msg('rename()'))
10091008

10101009
def replace(self, target):
10111010
"""
@@ -1017,13 +1016,13 @@ def replace(self, target):
10171016
10181017
Returns the new Path instance pointing to the target path.
10191018
"""
1020-
self._unsupported("replace")
1019+
raise UnsupportedOperation(self._unsupported_msg('replace()'))
10211020

10221021
def chmod(self, mode, *, follow_symlinks=True):
10231022
"""
10241023
Change the permissions of the path, like os.chmod().
10251024
"""
1026-
self._unsupported("chmod")
1025+
raise UnsupportedOperation(self._unsupported_msg('chmod()'))
10271026

10281027
def lchmod(self, mode):
10291028
"""
@@ -1037,31 +1036,31 @@ def unlink(self, missing_ok=False):
10371036
Remove this file or link.
10381037
If the path is a directory, use rmdir() instead.
10391038
"""
1040-
self._unsupported("unlink")
1039+
raise UnsupportedOperation(self._unsupported_msg('unlink()'))
10411040

10421041
def rmdir(self):
10431042
"""
10441043
Remove this directory. The directory must be empty.
10451044
"""
1046-
self._unsupported("rmdir")
1045+
raise UnsupportedOperation(self._unsupported_msg('rmdir()'))
10471046

10481047
def owner(self, *, follow_symlinks=True):
10491048
"""
10501049
Return the login name of the file owner.
10511050
"""
1052-
self._unsupported("owner")
1051+
raise UnsupportedOperation(self._unsupported_msg('owner()'))
10531052

10541053
def group(self, *, follow_symlinks=True):
10551054
"""
10561055
Return the group name of the file gid.
10571056
"""
1058-
self._unsupported("group")
1057+
raise UnsupportedOperation(self._unsupported_msg('group()'))
10591058

10601059
@classmethod
10611060
def from_uri(cls, uri):
10621061
"""Return a new path from the given 'file' URI."""
1063-
cls._unsupported("from_uri")
1062+
raise UnsupportedOperation(cls._unsupported_msg('from_uri()'))
10641063

10651064
def as_uri(self):
10661065
"""Return the path as a URI."""
1067-
self._unsupported("as_uri")
1066+
raise UnsupportedOperation(self._unsupported_msg('as_uri()'))

0 commit comments

Comments
 (0)
0