python / cpython

The Python programming language
https://www.python.org
Other
63.17k stars 30.25k forks source link

Raise TypeError for non-paths in `posixpath.relpath()` #117584

Closed nineteendo closed 6 months ago

nineteendo commented 6 months ago

Bug report

Bug description:

>>> import posixpath
>>> posixpath.relpath(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen posixpath>", line 509, in relpath
ValueError: no path specified

Expected: TypeError like npath.relpath().

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

erlend-aasland commented 6 months ago

Changing the exception type is a backwards-incompatible change; some people will need to rewrite their exception handling to adapt to the new behaviour. Do the pros outweigh the cons?

nineteendo commented 6 months ago

This is the same thing we did with non-iterables in ntpath.commonpath(): #117335 And it matches the error thrown by ntpath.relpath(), which better communicates what's wrong.

erlend-aasland commented 6 months ago

I'll defer to Serhiy, who is particularly interested in such cases. cc. @serhiy-storchaka

serhiy-storchaka commented 6 months ago

We usually do not bother with checking arguments more than necessary, especially in the Python code (which do not crash if you use wrong types or values). If the code happens to work outside the purposed area, well, someone can use it. If it fails with a weird error -- just do not do it. But in this case the change has zero performance cost, and TypeError is more suitable for None, 0 and [] than ValueError. It should be different from the error for '' and b''.