Skip to content

Commit 7dab30d

Browse files
committed
Fix bug when PurePath takes and ignores **kwargs
1 parent fc740ec commit 7dab30d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Lib/pathlib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ class PurePath(object):
650650
'_str', '_hash', '_pparts', '_cached_cparts',
651651
)
652652

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

663+
def __init__(self, *_):
664+
pass # bpo-29847
665+
663666
def __reduce__(self):
664667
# Using the parts tuple helps share interned path parts
665668
# when pickling related paths.
@@ -1071,9 +1074,6 @@ def __new__(cls, *args, **kwargs):
10711074
self._init()
10721075
return self
10731076

1074-
def __init__(self, *_):
1075-
pass # bpo-29847
1076-
10771077
def _init(self,
10781078
# Private non-constructor arguments
10791079
template=None,

Lib/test/test_pathlib.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,21 @@ def test_pickling_common(self):
681681
self.assertEqual(hash(pp), hash(p))
682682
self.assertEqual(str(pp), str(p))
683683

684+
def test_kwargs(self):
685+
with self.assertRaisesRegex(TypeError, 'got an unexpected keyword argument'):
686+
self.cls(arg=None)
687+
688+
def test_subclass_kwargs(self):
689+
class _PathSubclass(self.cls):
690+
_flavour = self.cls()._flavour
691+
692+
def __init__(self, *args, **kwargs):
693+
self.kwargs = kwargs
694+
695+
_kwargs = {"a": 1, "b": 2}
696+
p = _PathSubclass(**_kwargs)
697+
self.assertEqual(p.kwargs, _kwargs)
698+
684699

685700
class PurePosixPathTest(_BasePurePathTest, unittest.TestCase):
686701
cls = pathlib.PurePosixPath
@@ -2300,21 +2315,6 @@ def test_complex_symlinks_relative(self):
23002315
def test_complex_symlinks_relative_dot_dot(self):
23012316
self._check_complex_symlinks(os.path.join('dirA', '..'))
23022317

2303-
def test_kwargs(self):
2304-
with self.assertRaisesRegex(TypeError, 'got an unexpected keyword argument'):
2305-
self.cls(arg=None)
2306-
2307-
def test_subclass_kwargs(self):
2308-
class _PathSubclass(self.cls):
2309-
_flavour = self.cls()._flavour
2310-
2311-
def __init__(self, *args, **kwargs):
2312-
self.kwargs = kwargs
2313-
2314-
_kwargs = {"a": 1, "b": 2}
2315-
p = _PathSubclass(**_kwargs)
2316-
self.assertEqual(p.kwargs, _kwargs)
2317-
23182318

23192319
class PathTest(_BasePathTest, unittest.TestCase):
23202320
cls = pathlib.Path

0 commit comments

Comments
 (0)