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

Restore os.path methods overload workaround #12837

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions stdlib/@tests/test_cases/check_os_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import annotations
from _typeshed import StrOrBytesPath
from os import PathLike
from os.path import abspath, expanduser, expandvars
from typing_extensions import assert_type
from typing import AnyStr, Union


def test_str_path(str_path: StrOrBytesPath) -> None:
# These methods are currently overloaded to work around python/mypy#17952 & python/mypy#11880
# Let's ensure that they'll still work with a StrOrBytesPath if the workaround is removed

assert_type(abspath(str_path), Union[str, bytes])
assert_type(expanduser(str_path), Union[str, bytes])
assert_type(expandvars(str_path), Union[str, bytes])


# See python/mypy#17952
class MyPathMissingGeneric(PathLike): # type: ignore # Explicitly testing w/ missing type argument
def __init__(self, path: str | bytes) -> None:
super().__init__()
self.path = path

def __fspath__(self) -> str | bytes:
return self.path


# MyPathMissingGeneric could also be fixed by users by adding the missing generic annotation
class MyPathGeneric(PathLike[AnyStr]):
def __init__(self, path: AnyStr) -> None:
super().__init__()
self.path: AnyStr = path

def __fspath__(self) -> AnyStr:
return self.path


class MyPathStr(PathLike[str]):
def __init__(self, path: str) -> None:
super().__init__()
self.path = path

def __fspath__(self) -> str:
return self.path


abspath(MyPathMissingGeneric("."))
expanduser(MyPathMissingGeneric("."))
expandvars(MyPathMissingGeneric("."))

abspath(MyPathGeneric("."))
expanduser(MyPathGeneric("."))
expandvars(MyPathGeneric("."))

abspath(MyPathStr("."))
expanduser(MyPathStr("."))
expandvars(MyPathStr("."))
16 changes: 13 additions & 3 deletions stdlib/posixpath.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ pathsep: LiteralString
defpath: LiteralString
devnull: LiteralString

def abspath(path: PathLike[AnyStr] | AnyStr) -> AnyStr: ...
# Overloads are necessary to work around python/mypy#17952 & python/mypy#11880
@overload
def abspath(path: PathLike[AnyStr]) -> AnyStr: ...
@overload
def abspath(path: AnyStr) -> AnyStr: ...
@overload
def basename(p: PathLike[AnyStr]) -> AnyStr: ...
@overload
Expand All @@ -86,8 +90,14 @@ def basename(p: AnyOrLiteralStr) -> AnyOrLiteralStr: ...
def dirname(p: PathLike[AnyStr]) -> AnyStr: ...
@overload
def dirname(p: AnyOrLiteralStr) -> AnyOrLiteralStr: ...
def expanduser(path: PathLike[AnyStr] | AnyStr) -> AnyStr: ...
def expandvars(path: PathLike[AnyStr] | AnyStr) -> AnyStr: ...
@overload
def expanduser(path: PathLike[AnyStr]) -> AnyStr: ...
@overload
def expanduser(path: AnyStr) -> AnyStr: ...
@overload
def expandvars(path: PathLike[AnyStr]) -> AnyStr: ...
@overload
def expandvars(path: AnyStr) -> AnyStr: ...
@overload
def normcase(s: PathLike[AnyStr]) -> AnyStr: ...
@overload
Expand Down
Loading