This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Path takes and ignores **kwargs
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: JelleZijlstra, Jim Fasarakis-Hilliard, brett.cannon, jstasiak, pitrou, remi.lapeyre, serhiy.storchaka, uriyyo
Priority: normal Keywords: patch

Created on 2017-03-18 15:22 by JelleZijlstra, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 13399 closed jsaporta, 2019-05-18 00:26
PR 19632 open uriyyo, 2020-04-21 09:51
Messages (9)
msg289817 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2017-03-18 15:22
pathlib.Path.__new__ takes **kwargs, but doesn't do anything with them (https://github.com/python/cpython/blob/master/Lib/pathlib.py#L979). This doesn't appear to be documented.

This feature should presumably be either documented or removed (probably removed unless I'm missing some reason for having it).

Brief discussion on a typeshed PR at https://github.com/python/typeshed/pull/991#discussion-diff-105813974R100
msg289896 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-20 18:40
Yep, kwargs should be dropped since it isn't used or documented: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath (probably just a hold-over from when it did in some earlier version of the code).
msg289897 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2017-03-20 18:47
Thanks, I'll add a PR. This doesn't need to be documented, right?
msg289902 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-20 19:15
The support of **kwargs in Path.__new__ is needed if you want to implement a subclass of Path with __init__ accepting keyword arguments (and since Path constructor takes variable number of positional arguments, new arguments should be keyword-only).

>>> import pathlib
>>> class MyPath(pathlib.PosixPath):
...     def __init__(self, *args, spam=False):
...         self.spam = spam
... 
>>> p = MyPath('/', spam=True)
>>> p
MyPath('/')
>>> p.spam
True

Removing **kwargs from Path.__new__ will break the above example.

>>> MyPath('/', spam=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() got an unexpected keyword argument 'spam'
msg289944 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-21 17:12
Shoot, that's too bad. I guess we should document it then so people are aware that keyword arguments are ignored, else we will break subclasses. There's also an unfortunate difference between PurePath and Path as PurePath doesn't have this quirk.
msg289945 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-21 17:25
I don't know whether it was the intension of Antoine or just an oversight. I don't know whether it is used in the wild. But we can at least raise a TypeError for concrete classes PosixPath and WindowsPath if ignoring keyword arguments is a problem. Many extension types don't take keyword arguments, but their subclasses accept and ignore keyword arguments. For example:

>>> filter(None, [], foo=123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: filter() does not take keyword arguments
>>> class X(filter): pass
... 
>>> X(None, [], foo=123)
<__main__.X object at 0xb6fdcacc>
msg289946 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-03-21 17:32
> The support of **kwargs in Path.__new__ is needed if you want to implement a subclass of Path with __init__ accepting keyword arguments

I don't remember exactly, but I think this was the intention indeed.  There was originally an openat-using subclass, and IIRC it took additional parameters (such as the directory fd). That got scrapped quite early in the process, so we can remove the **kwargs thing now.
msg289998 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-03-22 16:32
Then I vote for Serhiy's idea of simply raising an exception in the concrete subclasses when a keyword argument is given.
msg369520 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-05-21 11:56
PurePath subclasses cannot support kwargs as __new__() does not accept **kwargs:


>>> from pathlib import PurePath
>>> class MyPurePath(PurePath):
...     def __init__(self, *args, **kargs): pass
... 
>>> MyPurePath('foo', spam=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() got an unexpected keyword argument 'spam'


The behaviour for this should probably be made the same for both Path and PurePath.
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74033
2020-06-10 22:05:47remi.lapeyresetversions: + Python 3.8, Python 3.9, Python 3.10
2020-05-21 11:56:21remi.lapeyresetnosy: + remi.lapeyre
messages: + msg369520
2020-04-21 09:51:40uriyyosetnosy: + uriyyo
pull_requests: + pull_request18959
2019-05-18 00:26:03jsaportasetkeywords: + patch
stage: patch review
pull_requests: + pull_request13309
2019-05-17 22:19:41jstasiaksetnosy: + jstasiak
2017-03-22 16:32:48brett.cannonsetmessages: + msg289998
2017-03-21 17:32:27pitrousetmessages: + msg289946
2017-03-21 17:25:34serhiy.storchakasetmessages: + msg289945
2017-03-21 17:12:24brett.cannonsetmessages: + msg289944
2017-03-20 19:15:26serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg289902
2017-03-20 18:47:30JelleZijlstrasetmessages: + msg289897
2017-03-20 18:40:51brett.cannonsetversions: + Python 3.7
nosy: + brett.cannon

messages: + msg289896

components: + Library (Lib)
type: behavior
2017-03-20 04:15:56Jim Fasarakis-Hilliardsetnosy: + Jim Fasarakis-Hilliard
2017-03-18 15:22:47JelleZijlstracreate