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.

Author Nophke
Recipients Nophke
Date 2019-06-02.01:18:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1559438292.33.0.601764456145.issue37130@roundup.psfhosted.org>
In-reply-to
Content
Hi guys!

I'm new to python and working on my first real project with it....
I'm sorry if it's not the right place for posting this.

I noticed that that pathlib.with_name() method does not accept to give a name to a path that does not already have one.

It seems a bit inconsistent knowing that the Path constructor does not require one...

>>> Path()
PosixPath('.')

>>> Path().resolve()
PosixPath('/home/nono')

but:
 
>>> Path().with_name('dodo')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/pathlib.py", line 819, in with_name
    raise ValueError("%r has an empty name" % (self,))
ValueError: PosixPath('.') has an empty name

whereas if you do:

>>> Path().resolve().with_name('dodo')
PosixPath('/home/dodo')

I first tought "explicit is better than implicit" and then why not allways use resolve first! That was not a big deal but then I tried:

>>> Path('..').with_name('dudu').resolve()
PosixPath('/home/nono/dudu')

( ! )

>>> Path('..').resolve().with_name('dudu')
PosixPath('/dudu')

It seems that the dots and slashes are in fact not really interpreted leading to:

>>> Path('../..').with_name('dudu')
PosixPath('../dudu')

>>> Path('../..').with_name('dudu').resolve()
PosixPath('/home/dudu')

( ! )
 
>>> Path('../..').resolve().with_name('dudu')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/pathlib.py", line 819, in with_name
    raise ValueError("%r has an empty name" % (self,))
ValueError: PosixPath('/') has an empty name

Even if the doc briefly tells about this, I found this behavior quite disturbing....

I don't know what could be the correct answer to this,
maybe making Path('..') as invalid as Path('.'),
or adding a few more lines in the doc...

Sincerly yours,
History
Date User Action Args
2019-06-02 01:18:12Nophkesetrecipients: + Nophke
2019-06-02 01:18:12Nophkesetmessageid: <1559438292.33.0.601764456145.issue37130@roundup.psfhosted.org>
2019-06-02 01:18:12Nophkelinkissue37130 messages
2019-06-02 01:18:11Nophkecreate