8000 bpo-34775: Return NotImplemented in PurePath division. (GH-9509) (GH-… · python/cpython@4adcaf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4adcaf8

Browse files
miss-islingtonaiudirog
authored andcommitted
bpo-34775: Return NotImplemented in PurePath division. (GH-9509) (GH-15172)
(cherry picked from commit 4c69be2) Co-authored-by: aiudirog <aiudirog@gmail.com>
1 parent e471a54 commit 4adcaf8

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

Lib/pathlib.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,10 +907,16 @@ def joinpath(self, *args):
907907
return self._make_child(args)
908908

909909
def __truediv__(self, key):
910-
return self._make_child((key,))
910+
try:
911+
return self._make_child((key,))
912+
except TypeError:
913+
return NotImplemented
911914

912915
def __rtruediv__(self, key):
913-
return self._from_parts([key] + self._parts)
916+
try:
917+
return self._from_parts([key] + self._parts)
918+
except TypeError:
919+
return NotImplemented
914920

915921
@property
916922
def parent(self):

Lib/test/test_pathlib.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@ 10000 @ -2329,5 +2329,46 @@ def check():
23292329
check()
23302330

23312331

2332+
class CompatiblePathTest(unittest.TestCase):
2333+
"""
2334+
Test that a type can be made compatible with PurePath
2335+
derivatives by implementing division operator overloads.
2336+
"""
2337+
2338+
class CompatPath:
2339+
"""
2340+
Minimum viable class to test PurePath compatibility.
2341+
Simply uses the division operator to join a given
2342+
string and the string value of another object with
2343+
a forward slash.
2344+
"""
2345+
def __init__(self, string):
2346+
self.string = string
2347+
2348+
def __truediv__(self, other):
2349+
return type(self)(f"{self.string}/{other}")
2350+
2351+
def __rtruediv__(self, other):
2352+
return type(self)(f"{other}/{self.string}")
2353+
2354+
def test_truediv(self):
2355+
result = pathlib.PurePath("test") / self.CompatPath("right")
2356+
self.assertIsInstance(result, self.CompatPath)
2357+
self.assertEqual(result.string, "test/right")
2358+
2359+
with self.assertRaises(TypeError):
2360+
# Verify improper operations still raise a TypeError
2361+
pathlib.PurePath("test") / 10
2362+
2363+
def test_rtruediv(self):
2364+
result = self.CompatPath("left") / pathlib.PurePath("test")
2365+
self.assertIsInstance(result, self.CompatPath)
2366+
self.assertEqual(result.string, "left/test")
2367+
2368+
with self.assertRaises(TypeError):
2369+
# Verify improper operations still raise a TypeError
2370+
10 / pathlib.PurePath("test")
2371+
2372+
23322373
if __name__ == "__main__":
23332374
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Division handling of PurePath now returns NotImplemented instead of raising
2+
a TypeError when passed something other than an instance of str or PurePath.
3+
Patch by Roger Aiudi.

0 commit comments

Comments
 (0)
0