From 92d601753ec4cc202cdd18818f8ef40a999e853c Mon Sep 17 00:00:00 2001 From: aiudirog Date: Sun, 23 Sep 2018 02:59:51 -0400 Subject: [PATCH 1/6] Return NotImplemented in PurePath division. --- Lib/pathlib.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 89dffa5561a7a5..f68307182d3e89 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -889,10 +889,16 @@ def joinpath(self, *args): return self._make_child(args) def __truediv__(self, key): - return self._make_child((key,)) + try: + return self._make_child((key,)) + except TypeError: + return NotImplemented def __rtruediv__(self, key): - return self._from_parts([key] + self._parts) + try: + return self._from_parts([key] + self._parts) + except TypeError: + return NotImplemented @property def parent(self): From 4c6e33943319ea52fe152bb6e706afa27d1f37c1 Mon Sep 17 00:00:00 2001 From: aiudirog Date: Sun, 23 Sep 2018 03:19:05 -0400 Subject: [PATCH 2/6] Add news --- .../next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst diff --git a/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst b/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst new file mode 100644 index 00000000000000..786a36906b62e2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst @@ -0,0 +1,2 @@ +Division handling of PurePath now returns NotImplemented instead of raising +a TypeError when passed something other than an instance of str or PurePath. From 83829675b2507ac23082b66eadada7c20b117347 Mon Sep 17 00:00:00 2001 From: aiudirog Date: Mon, 25 Feb 2019 21:15:14 -0500 Subject: [PATCH 3/6] Add pathlib compatible path test cases --- Lib/test/test_pathlib.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 876eecccfd5fe3..bc765b2f352ce0 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2298,5 +2298,44 @@ def check(): check() +class CompatiblePathTest(unittest.TestCase): + """ + Test that a type can be made compatible with PurePath + derivatives by implementing division operator overloads. + """ + + class CompatPath: + """ + Simple class to store a string and uses combine that + string with other object's string values via division. + """ + 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() From 5fbdc2dff36e699db0175642c1fdc1b4529bd5c3 Mon Sep 17 00:00:00 2001 From: aiudirog Date: Wed, 7 Aug 2019 07:45:50 -0400 Subject: [PATCH 4/6] Add contributor name --- .../NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst b/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst index 786a36906b62e2..f99bf5b39f9519 100644 --- a/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst +++ b/Misc/NEWS.d/next/Library/2018-09-23-03-18-52.bpo-34775.vHeuHk.rst @@ -1,2 +1,3 @@ Division handling of PurePath now returns NotImplemented instead of raising a TypeError when passed something other than an instance of str or PurePath. +Patch by Roger Aiudi. From f40060a4b66657cf3444beac9971af61e9f94664 Mon Sep 17 00:00:00 2001 From: aiudirog Date: Wed, 7 Aug 2019 17:29:06 -0400 Subject: [PATCH 5/6] Rewrite CompatPath docs to make sense --- Lib/test/test_pathlib.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index bc765b2f352ce0..fed051ea47dd03 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2306,8 +2306,10 @@ class CompatiblePathTest(unittest.TestCase): class CompatPath: """ - Simple class to store a string and uses combine that - string with other object's string values via division. + 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 From d17b05d6932ac3333b9262f5ef897d3d503609d3 Mon Sep 17 00:00:00 2001 From: aiudirog Date: Wed, 7 Aug 2019 22:07:40 -0400 Subject: [PATCH 6/6] Remove trailing white space --- Lib/test/test_pathlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index fed051ea47dd03..9fd7495bea6612 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2307,8 +2307,8 @@ class CompatiblePathTest(unittest.TestCase): 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 + 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):