Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All standard library functions accepting str paths must accept PathLike as well #102425

Open
arhadthedev opened this issue Mar 4, 2023 · 6 comments
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir topic-IO type-feature A feature request or enhancement

Comments

@arhadthedev
Copy link
Member

Inspired by gh-3484 that tried to fix this for wave.open() only but failed.

We need a centralized and universal approach instead so everything accepting str paths could automatically accept PathLike for free.

For this, we can amend stdlib functions to wrap their path arguments into pathlib.PurePath introduced in Python 3.4. Unlike os.fspath, it throws a nicely formatted exception for us:

  • os.fspath from the mentioned bpo-31412: wave.open takes a path-like object #3484:

    cpython/Lib/aifc.py

    Lines 912 to 918 in 4a82d65

    try:
    f = os.fspath(f)
    except TypeError:
    if not hasattr(f, 'read') and not hasattr(f, 'write'):
    raise TypeError('open() takes str, a path-like object, '
    + 'or an open file-like object, '
    + f'not {type(f).__name__!r}') from None

  • PurePath:

    >>> import pathlib
    >>> pathlib.PurePath(16)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "D:\Oleg\cpython\Lib\pathlib.py", line 267, in __new__
        return cls._from_parts(args)
               ^^^^^^^^^^^^^^^^^^^^^
      File "D:\Oleg\cpython\Lib\pathlib.py", line 316, in _from_parts
        drv, root, parts = self._parse_args(args)
                           ^^^^^^^^^^^^^^^^^^^^^^
      File "D:\Oleg\cpython\Lib\pathlib.py", line 300, in _parse_args
        a = os.fspath(a)
            ^^^^^^^^^^^^
    TypeError: expected str, bytes or os.PathLike object, not int
@arhadthedev arhadthedev added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir extension-modules C modules in the Modules dir topic-IO labels Mar 4, 2023
@arhadthedev arhadthedev changed the title All std methods and functions accepting str paths must accept PathLike as well All standard library functions accepting str paths must accept PathLike as well Mar 4, 2023
@ericvsmith
Copy link
Member

It’s not clear to me what you’re proposing to change. Can you provide specifics?

How would PurePath(16)’s behavior change?

@encukou
Copy link
Member

encukou commented Mar 6, 2023

We need a centralized and universal approach instead so everything accepting str paths could automatically accept PathLike for free.

os.fspath is meant to do just that.

Note that importing pathlib and creating PurePath are relatively expensive operations (compared to os.fspath with strings). When the niceties they provide aren't useful, it's better to avoid them.

Unlike os.fspath, it throws a nicely formatted exception for us:

How can the error message from os.fspath be improved?

@hauntsaninja
Copy link
Contributor

Unlike os.fspath, it throws a nicely formatted exception for us

I'm confused, pathlib.PurePath's error message in the thing you've posted is literally coming straight from os.fspath

@arhadthedev
Copy link
Member Author

Closing in favor of gh-103659.

@arhadthedev arhadthedev closed this as not planned Won't fix, can't repro, duplicate, stale Apr 21, 2023
@arhadthedev
Copy link
Member Author

Reopening after #103659 (comment):

BTW, I don't think #102425 applies here, the os.environ values are strings some of which happen to look like paths (or integers).

However, I'll return to this issue much later.

@arhadthedev arhadthedev reopened this Apr 21, 2023
@ronaldoussoren
Copy link
Contributor

No need to reopen this for my comment on #103659 , I tried to say that this issue is not a reason for changing the behaviour of os.environ with PathLike objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir topic-IO type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants