From da46c4b9f7b4dd590c8223ee860d33d28c965e79 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Fri, 19 May 2023 09:10:09 -0700 Subject: [PATCH] pathlib: Add __rtruediv__ magic method to pathlib.Path. MicroPython now supports this behaviour of __rtruediv__. --- python-stdlib/pathlib/pathlib.py | 3 +++ python-stdlib/pathlib/tests/test_pathlib.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/python-stdlib/pathlib/pathlib.py b/python-stdlib/pathlib/pathlib.py index d01d81d3..e0f96137 100644 --- a/python-stdlib/pathlib/pathlib.py +++ b/python-stdlib/pathlib/pathlib.py @@ -47,6 +47,9 @@ class Path: def __truediv__(self, other): return Path(self._path, str(other)) + def __rtruediv__(self, other): + return Path(other, self._path) + def __repr__(self): return f'{type(self).__name__}("{self._path}")' diff --git a/python-stdlib/pathlib/tests/test_pathlib.py b/python-stdlib/pathlib/tests/test_pathlib.py index c52cd970..e632e124 100644 --- a/python-stdlib/pathlib/tests/test_pathlib.py +++ b/python-stdlib/pathlib/tests/test_pathlib.py @@ -322,3 +322,14 @@ class TestPathlib(unittest.TestCase): self.assertTrue(Path("foo/test").with_suffix(".tar") == Path("foo/test.tar")) self.assertTrue(Path("foo/bar.bin").with_suffix(".txt") == Path("foo/bar.txt")) self.assertTrue(Path("bar.txt").with_suffix("") == Path("bar")) + + def test_rtruediv(self): + """Works as of micropython ea7031f""" + res = "foo" / Path("bar") + self.assertTrue(res == Path("foo/bar")) + + def test_rtruediv_inplace(self): + """Works as of micropython ea7031f""" + res = "foo" + res /= Path("bar") + self.assertTrue(res == Path("foo/bar"))