Skip to content

Replace overloads in os.path stubs with Unions of TypeVars #9474

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import sys
from collections.abc import Awaitable, Callable, Iterable, Set as AbstractSet
from os import PathLike
from types import FrameType, TracebackType
from typing import Any, AnyStr, Generic, Protocol, TypeVar, Union
from typing import Any, Generic, Protocol, TypeVar, Union
from typing_extensions import Final, Literal, LiteralString, TypeAlias, final

_KT = TypeVar("_KT")
Expand Down Expand Up @@ -129,7 +129,7 @@ class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra,

StrPath: TypeAlias = str | PathLike[str] # stable
BytesPath: TypeAlias = bytes | PathLike[bytes] # stable
GenericPath: TypeAlias = AnyStr | PathLike[AnyStr]
GenericPath: TypeAlias = AnyStr_co | PathLike[AnyStr_co]
StrOrBytesPath: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes] # stable

OpenTextModeUpdating: TypeAlias = Literal[
Expand Down
22 changes: 5 additions & 17 deletions stdlib/ntpath.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import BytesPath, StrPath
from _typeshed import AnyOrLiteralStr, GenericPath
from genericpath import (
commonprefix as commonprefix,
exists as exists,
Expand All @@ -13,7 +13,6 @@ from genericpath import (
sameopenfile as sameopenfile,
samestat as samestat,
)
from os import PathLike

# Re-export common definitions from posixpath to reduce duplication
from posixpath import (
Expand Down Expand Up @@ -42,7 +41,7 @@ from posixpath import (
splitext as splitext,
supports_unicode_filenames as supports_unicode_filenames,
)
from typing import AnyStr, overload
from typing import AnyStr
from typing_extensions import LiteralString

__all__ = [
Expand Down Expand Up @@ -91,24 +90,13 @@ altsep: LiteralString
# First parameter is not actually pos-only,
# but must be defined as pos-only in the stub or cross-platform code doesn't type-check,
# as the parameter name is different in posixpath.join()
@overload
def join(__path: LiteralString, *paths: LiteralString) -> LiteralString: ...
@overload
def join(__path: StrPath, *paths: StrPath) -> str: ...
@overload
def join(__path: BytesPath, *paths: BytesPath) -> bytes: ...
def join(__path: GenericPath[AnyOrLiteralStr], *paths: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...

if sys.platform == "win32":
if sys.version_info >= (3, 10):
@overload
def realpath(path: PathLike[AnyStr], *, strict: bool = ...) -> AnyStr: ...
@overload
def realpath(path: AnyStr, *, strict: bool = ...) -> AnyStr: ...
def realpath(path: GenericPath[AnyStr], *, strict: bool = ...) -> AnyStr: ...
else:
@overload
def realpath(path: PathLike[AnyStr]) -> AnyStr: ...
@overload
def realpath(path: AnyStr) -> AnyStr: ...
def realpath(path: GenericPath[AnyStr]) -> AnyStr: ...

else:
realpath = abspath
86 changes: 17 additions & 69 deletions stdlib/posixpath.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import AnyOrLiteralStr, BytesPath, StrOrBytesPath, StrPath
from _typeshed import AnyOrLiteralStr, GenericPath, StrOrBytesPath
from collections.abc import Sequence
from genericpath import (
commonprefix as commonprefix,
Expand All @@ -14,8 +14,7 @@ from genericpath import (
sameopenfile as sameopenfile,
samestat as samestat,
)
from os import PathLike
from typing import AnyStr, overload
from typing import AnyStr
from typing_extensions import LiteralString

__all__ = [
Expand Down Expand Up @@ -71,81 +70,30 @@ defpath: LiteralString
devnull: LiteralString

# Overloads are necessary to work around python/mypy#3644.
@overload
def abspath(path: PathLike[AnyStr]) -> AnyStr: ...
@overload
def abspath(path: AnyStr) -> AnyStr: ...
@overload
def basename(p: PathLike[AnyStr]) -> AnyStr: ...
@overload
def basename(p: AnyOrLiteralStr) -> AnyOrLiteralStr: ...
@overload
def dirname(p: PathLike[AnyStr]) -> AnyStr: ...
@overload
def dirname(p: AnyOrLiteralStr) -> AnyOrLiteralStr: ...
@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
def normcase(s: AnyOrLiteralStr) -> AnyOrLiteralStr: ...
@overload
def normpath(path: PathLike[AnyStr]) -> AnyStr: ...
@overload
def normpath(path: AnyOrLiteralStr) -> AnyOrLiteralStr: ...
@overload
def commonpath(paths: Sequence[LiteralString]) -> LiteralString: ...
@overload
def commonpath(paths: Sequence[StrPath]) -> str: ...
@overload
def commonpath(paths: Sequence[BytesPath]) -> bytes: ...
def abspath(path: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...
def basename(p: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...
def dirname(p: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...
def expanduser(path: GenericPath[AnyStr]) -> AnyStr: ...
def expandvars(path: GenericPath[AnyStr]) -> AnyStr: ...
def normcase(s: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...
def normpath(path: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...
def commonpath(paths: Sequence[GenericPath[AnyOrLiteralStr]]) -> AnyOrLiteralStr: ...

# First parameter is not actually pos-only,
# but must be defined as pos-only in the stub or cross-platform code doesn't type-check,
# as the parameter name is different in ntpath.join()
@overload
def join(__a: LiteralString, *paths: LiteralString) -> LiteralString: ...
@overload
def join(__a: StrPath, *paths: StrPath) -> str: ...
@overload
def join(__a: BytesPath, *paths: BytesPath) -> bytes: ...
def join(__a: GenericPath[AnyOrLiteralStr], *paths: GenericPath[AnyOrLiteralStr]) -> AnyOrLiteralStr: ...

if sys.version_info >= (3, 10):
@overload
def realpath(filename: PathLike[AnyStr], *, strict: bool = ...) -> AnyStr: ...
@overload
def realpath(filename: AnyStr, *, strict: bool = ...) -> AnyStr: ...
def realpath(filename: GenericPath[AnyStr], *, strict: bool = ...) -> AnyStr: ...

else:
@overload
def realpath(filename: PathLike[AnyStr]) -> AnyStr: ...
@overload
def realpath(filename: AnyStr) -> AnyStr: ...
def realpath(filename: GenericPath[AnyStr]) -> AnyStr: ...

@overload
def relpath(path: LiteralString, start: LiteralString | None = ...) -> LiteralString: ...
@overload
def relpath(path: BytesPath, start: BytesPath | None = ...) -> bytes: ...
@overload
def relpath(path: StrPath, start: StrPath | None = ...) -> str: ...
@overload
def split(p: PathLike[AnyStr]) -> tuple[AnyStr, AnyStr]: ...
@overload
def split(p: AnyOrLiteralStr) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
@overload
def splitdrive(p: PathLike[AnyStr]) -> tuple[AnyStr, AnyStr]: ...
@overload
def splitdrive(p: AnyOrLiteralStr) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
@overload
def splitext(p: PathLike[AnyStr]) -> tuple[AnyStr, AnyStr]: ...
@overload
def splitext(p: AnyOrLiteralStr) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
def relpath(path: GenericPath[AnyOrLiteralStr], start: GenericPath[AnyOrLiteralStr] | None = ...) -> AnyOrLiteralStr: ...
def split(p: GenericPath[AnyOrLiteralStr]) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
def splitdrive(p: GenericPath[AnyOrLiteralStr]) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
def splitext(p: GenericPath[AnyOrLiteralStr]) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
def isabs(s: StrOrBytesPath) -> bool: ...
def islink(path: StrOrBytesPath | int) -> bool: ...
def ismount(path: StrOrBytesPath | int) -> bool: ...
Expand Down