Skip to content

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

Closed

Description

@nineteendo

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

Activity

  1. added
    type-bug
    on Apr 6, 2024
  2. erlend-aasland commented on Apr 6, 2024

    @erlend-aasland
    Contributor

    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?

  3. nineteendo commented on Apr 6, 2024

    @nineteendo
    ContributorAuthor

    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.

  4. erlend-aasland commented on Apr 6, 2024

    @erlend-aasland
    Contributor

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

  5. added a commit that references this issue on Apr 7, 2024
  6. added
    3.13
    on Apr 7, 2024
  7. serhiy-storchaka commented on Apr 7, 2024

    @serhiy-storchaka
    Member

    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''.

  8. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions