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.

classification
Title: pathlib: support relative path construction
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: cybergrind, eric.smith, pitrou
Priority: normal Keywords: patch

Created on 2021-08-22 15:07 by cybergrind, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27890 closed cybergrind, 2021-08-22 15:13
Messages (3)
msg400075 - (view) Author: Kirill Pinchuk (cybergrind) * Date: 2021-08-22 15:07
Hi. I've been using this snippet for years and believe that it would be a nice addition to pathlib's functionality.

Basically, it allows constructing path relative to the current file (instead of cwd). Comes quite handy when you're working with deeply nested resources like file fixtures in tests and many other cases.

```
    @classmethod
    def relative(cls, path, depth=1):
        """
        Return path that is constructed relatively to caller file.
        """
        base = Path(sys._getframe(depth).f_code.co_filename).parent
        return (base / path).resolve()
```
msg400108 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-08-22 22:55
To be clear, by "file", you mean python source file, or I guess .pyc file if only that exists. I can't say I've given it much thought. What about built-in modules? Or frozen modules?

I'm not sure this is a great idea. In general, we've frowned on code that uses sys._getframe, and in particular that looks arbitrarily up the stack.

At the very least, you should find out if other python implementations can use this function.

I suggest bringing this up on the python-ideas mailing list.
msg400128 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2021-08-23 10:30
I am rejecting this for the reasons explained in the Github PR:

"""This seems like an extremely specific API. Perhaps you want to propose it for inclusion in pytest, but I don't think it belongs in pathlib.

(also, it's not difficult to spell out `Path(__file__)`)"""
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89142
2021-08-23 10:30:45pitrousetstatus: open -> closed

nosy: + pitrou
messages: + msg400128

resolution: rejected
stage: patch review -> resolved
2021-08-22 22:55:13eric.smithsetnosy: + eric.smith
messages: + msg400108
2021-08-22 15:13:55cybergrindsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26344
2021-08-22 15:07:54cybergrindcreate