8000 gh-74033: Fix bug when Path takes and ignores **kwargs by uriyyo · Pull Request #19632 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-74033: Fix bug when Path takes and ignores **kwargs #19632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class PurePath(object):
'_str', '_hash', '_pparts', '_cached_cparts',
)

def __new__(cls, *args):
def __new__(cls, *args, **kwargs):
"""Construct a PurePath from one or several strings and or existing
PurePath objects. The strings and path objects are combined so as
to yield a canonicalized path, which is incorporated into the
Expand All @@ -400,6 +400,9 @@ def __new__(cls, *args):
cls = PureWindowsPath if os.name == 'nt' else PurePosixPath
return cls._from_parts(args)

def __init__(self, *_):
pass # bpo-29847

def __reduce__(self):
# Using the parts tuple helps share interned path parts
# when pickling related paths.
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,22 @@ def test_pickling_common(self):
self.assertEqual(hash(pp), hash(p))
self.assertEqual(str(pp), str(p))

def test_kwargs(self):
with self.assertRaisesRegex(TypeError, 'got an unexpected keyword argument'):
self.cls(arg=None)

def test_subclass_kwargs(self):
# See bpo-29847
class _PathSubclass(self.cls):
_flavour = self.cls()._flavour

def __init__(self, *args, **kwargs):
self.kwargs = kwargs

_kwargs = {"a": 1, "b": 2}
p = _PathSubclass(**_kwargs)
self.assertEqual(p.kwargs, _kwargs)


class PurePosixPathTest(_BasePurePathTest, unittest.TestCase):
cls = pathlib.PurePosixPath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where :class:`pathlib.PurePath` and its subclasses take and ignore `**kwargs`. Patch provided by Yurii Karabas.
0