Title: add __fspath__ support to os.path by JelleZijlstra · Pull Request #2053 · python/typeshed · GitHub
Open Graph Title: add __fspath__ support to os.path by JelleZijlstra · Pull Request #2053 · python/typeshed
X Title: add __fspath__ support to os.path by JelleZijlstra · Pull Request #2053 · python/typeshed
Description: Fixes #1997, #2068. This is tricky because we need to get the return values right (see #1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures correctly using a series of overloads. A few other changes in here: Added splitunc, which according to https://docs.python.org/3/library/os.path.html should exist in both Unix and Windows. Made the second argument to os.path.curdir Optional to match the implementation. Fixed os.path.split, whose previous Path-aware signature triggered python/mypy#3644. I couldn't come up with a correct signature for os.path.commonpath, so I am falling back to using Any as its return type. I used the following test program to make sure mypy accepted the signatures used here: import os from os.path import abspath, commonpath, relpath, split, join from typing import Generic, AnyStr, Any class PL(os.PathLike[AnyStr]): def __init__(self, path: AnyStr) -> None: self.path: AnyStr = path def __fspath__(self) -> AnyStr: return self.path bad_path: PL[Any] = PL('') reveal_type(abspath('hello')) # str reveal_type(abspath(b'hello')) # bytes reveal_type(abspath(PL('hello'))) # str reveal_type(abspath(PL(b'hello'))) # bytes reveal_type(commonpath(['hello', PL('hello')])) # Any (couldn't get it work correctly) reveal_type(commonpath([b'hello', PL(b'hello')])) # Any reveal_type(relpath('hello')) # str reveal_type(relpath('hello', PL('hello'))) # str reveal_type(relpath(b'hello', b'hello')) # bytes reveal_type(relpath(PL('hello'))) # str reveal_type(relpath(PL(b'hello'), b'hello')) # bytes reveal_type(relpath(bad_path, 'hello')) # str reveal_type(relpath(bad_path, bad_path)) # Any reveal_type(split('hello')) # (str, str) reveal_type(split(b'hello')) # (bytes, bytes) reveal_type(split(PL('hello'))) # (str, str) reveal_type(split(PL(b'hello'))) # (bytes, bytes) reveal_type(join('hello', PL('hello'))) # str reveal_type(join(PL('hello'), 'hello')) # str reveal_type(join(b'hello', PL(b'hello'))) # bytes reveal_type(join(b'hello', PL('hello'))) # error I made the same changes to the Python 2 stub in order to keep the two consistent.
Open Graph Description: Fixes #1997, #2068. This is tricky because we need to get the return values right (see #1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures...
X Description: Fixes #1997, #2068. This is tricky because we need to get the return values right (see #1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures...
Opengraph URL: https://github.com/python/typeshed/pull/2053
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:737e6972-aa36-de1d-15a2-8ff988c64dac |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C0D2:1C3DC0:3EA15F:544263:6B002AA6 |
| html-safe-nonce | 259c9237a7e9162a24a266492787a0ceab06b8253af5361a547f6d36f742995a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMEQyOjFDM0RDMDozRUExNUY6NTQ0MjYzOjZCMDAyQUE2IiwidmlzaXRvcl9pZCI6IjU0ODEyOTEyOTk5MzM5MjQwMDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2737f3a6ea54f3e3a38361578e112abd4a42eafba3be7fa8c4833693174a5a5d |
| hovercard-subject-tag | pull_request:181630773 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/typeshed/pull/2053/files |
| twitter:image | https://avatars.githubusercontent.com/u/906600?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/906600?s=400&v=4 |
| og:image:alt | Fixes #1997, #2068. This is tricky because we need to get the return values right (see #1960 for prior attempts) and we often run into python/mypy#3644. I found that I could express most signatures... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width