[Python-checkins] gh-74033: Fix bug when Path takes and ignores **kwargs (GH-19632)

miss-islington webhook-mailer at python.org
Fri Jan 13 19:05:49 EST 2023


https://github.com/python/cpython/commit/080cb27829aa1a461d80504b32c362cbc14a5a75
commit: 080cb27829aa1a461d80504b32c362cbc14a5a75
branch: main
author: Yurii Karabas <1998uriyyo at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-01-13T16:05:43-08:00
summary:

gh-74033: Fix bug when Path takes and ignores **kwargs (GH-19632)



Fix a bug where `Path` takes and ignores `**kwargs` by adding to `PurePath`  class `__init__` method which can take only positional arguments.

Automerge-Triggered-By: GH:brettcannon

files:
A Misc/NEWS.d/next/Library/2020-04-18-17-45-03.bpo-29847.Uxtbq0.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index a0678f61b632..ae7a62f8a4cd 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -713,6 +713,10 @@ class Path(PurePath):
     __slots__ = ()
 
     def __new__(cls, *args, **kwargs):
+        if kwargs:
+            msg = ("support for supplying keyword arguments to pathlib.PurePath "
+                   "is deprecated and scheduled for removal in Python {remove}")
+            warnings._deprecated("pathlib.PurePath(**kwargs)", msg, remove=(3, 14))
         if cls is Path:
             cls = WindowsPath if os.name == 'nt' else PosixPath
         self = cls._from_parts(args)
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 7d4d782cf5f0..1fe242b7f6ab 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -2571,6 +2571,11 @@ def test_complex_symlinks_relative(self):
     def test_complex_symlinks_relative_dot_dot(self):
         self._check_complex_symlinks(os.path.join('dirA', '..'))
 
+    def test_passing_kwargs_deprecated(self):
+        with self.assertWarns(DeprecationWarning):
+            self.cls(foo="bar")
+
+
 class WalkTests(unittest.TestCase):
 
     def setUp(self):
diff --git a/Misc/NEWS.d/next/Library/2020-04-18-17-45-03.bpo-29847.Uxtbq0.rst b/Misc/NEWS.d/next/Library/2020-04-18-17-45-03.bpo-29847.Uxtbq0.rst
new file mode 100644
index 000000000000..010d775a0d98
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-18-17-45-03.bpo-29847.Uxtbq0.rst
@@ -0,0 +1 @@
+Fix a bug where :class:`pathlib.Path` accepted and ignored keyword arguments. Patch provided by Yurii Karabas.



More information about the Python-checkins mailing list