[New-bugs-announce] [issue20639] pathlib.PurePath.with_suffix() does not allow removing the suffix

July Tikhonov report at bugs.python.org
Sun Feb 16 14:28:50 CET 2014


New submission from July Tikhonov:

The changeset ef2b2ddd27c8 restricted the argument of Path.with_suffix() too much, and caused some strange behavior.

Case 1: removing suffix completely is disallowed now.
The following code worked before the fix:

>>> pathlib.PurePath('a', 'b.c').with_suffix('')
PurePosixPath('a/b')

but now fails with ValueError:

>>> pathlib.PurePath('a', 'b.c').with_suffix('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/july/source/python/Lib/pathlib.py", line 760, in with_suffix
    raise ValueError("Invalid suffix %r" % (suffix))
ValueError: Invalid suffix ''

It was the only one obvious way of removing the suffix, and I think it should remain so. (BTW: There is a XXX note in the code questioning if Path.with_suffix(None) should remove the suffix.)

Case 2: while the output is now always a correct Path, the suffix can still contain separator.
The following code produced incorrect path before the fix:

>>> pathlib.PurePath('a', 'b.c').with_suffix('./.s/.')
PurePosixPath('a/b./.s/.')
>>> _.parts
('a', 'b./.s/.')

Now, the produced path is correct, but the code itself is still allowed:

>>> pathlib.PurePath('a', 'b.c').with_suffix('./.s/.')
PurePosixPath('a/b.s')

while I would expect it to fail with ValueError.

Attached: proposed test patch.

----------
components: Library (Lib)
files: pathlib-with_suffix-test.diff
keywords: patch
messages: 211316
nosy: july, pitrou
priority: normal
severity: normal
status: open
title: pathlib.PurePath.with_suffix() does not allow removing the suffix
type: behavior
versions: Python 3.4, Python 3.5
Added file: http://bugs.python.org/file34099/pathlib-with_suffix-test.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20639>
_______________________________________


More information about the New-bugs-announce mailing list