summaryrefslogtreecommitdiff
path: root/Lib/test/test_pathlib.py
diff options
context:
space:
mode:
authoraiudirog <aiudirog@gmail.com>2019-08-08 01:41:10 -0400
committerSerhiy Storchaka <storchaka@gmail.com>2019-08-08 08:41:10 +0300
commit4c69be22df3852f17873a74d015528d9a8ae92d6 (patch)
treed0f30d3f81f17f85594408e9e5705a678c98f851 /Lib/test/test_pathlib.py
parent0378d98678f3617fd44d9a6266e7c17ebce62755 (diff)
downloadcpython-git-4c69be22df3852f17873a74d015528d9a8ae92d6.tar.gz
bpo-34775: Return NotImplemented in PurePath division. (GH-9509)
Diffstat (limited to 'Lib/test/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 069467a845..f74524d992 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -2329,5 +2329,46 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase):
check()
+class CompatiblePathTest(unittest.TestCase):
+ """
+ Test that a type can be made compatible with PurePath
+ derivatives by implementing division operator overloads.
+ """
+
+ class CompatPath:
+ """
+ Minimum viable class to test PurePath compatibility.
+ Simply uses the division operator to join a given
+ string and the string value of another object with
+ a forward slash.
+ """
+ def __init__(self, string):
+ self.string = string
+
+ def __truediv__(self, other):
+ return type(self)(f"{self.string}/{other}")
+
+ def __rtruediv__(self, other):
+ return type(self)(f"{other}/{self.string}")
+
+ def test_truediv(self):
+ result = pathlib.PurePath("test") / self.CompatPath("right")
+ self.assertIsInstance(result, self.CompatPath)
+ self.assertEqual(result.string, "test/right")
+
+ with self.assertRaises(TypeError):
+ # Verify improper operations still raise a TypeError
+ pathlib.PurePath("test") / 10
+
+ def test_rtruediv(self):
+ result = self.CompatPath("left") / pathlib.PurePath("test")
+ self.assertIsInstance(result, self.CompatPath)
+ self.assertEqual(result.string, "left/test")
+
+ with self.assertRaises(TypeError):
+ # Verify improper operations still raise a TypeError
+ 10 / pathlib.PurePath("test")
+
+
if __name__ == "__main__":
unittest.main()