From af2fadc180609f9102dbea465e36ed45b3701de6 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 21 Mar 2017 22:26:21 -0700 Subject: [PATCH] More fixes related to PathLike (#1061) * Make DirEntry generic over AnyStr, and inherit from PathLike if it exists * Make pathlib require PathLike[str] (when it exists) --- stdlib/3.4/pathlib.pyi | 6 +++--- stdlib/3/os/__init__.pyi | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/stdlib/3.4/pathlib.pyi b/stdlib/3.4/pathlib.pyi index f424803c1..0051266be 100644 --- a/stdlib/3.4/pathlib.pyi +++ b/stdlib/3.4/pathlib.pyi @@ -7,7 +7,7 @@ import sys _P = TypeVar('_P', bound='PurePath') if sys.version_info >= (3, 6): - _PurePathBase = os.PathLike + _PurePathBase = os.PathLike[str] else: _PurePathBase = object @@ -25,7 +25,7 @@ class PurePath(_PurePathBase): elif sys.version_info < (3, 6): def __new__(cls: Type[_P], *args: Union[str, PurePath]) -> _P: ... else: - def __new__(cls: Type[_P], *args: Union[str, os.PathLike]) -> _P: ... + def __new__(cls: Type[_P], *args: Union[str, os.PathLike[str]]) -> _P: ... def __hash__(self) -> int: ... def __lt__(self, other: PurePath) -> bool: ... def __le__(self, other: PurePath) -> bool: ... @@ -99,7 +99,7 @@ class Path(PurePath): def __new__(cls: Type[_P], *args: Union[str, PurePath], **kwargs: Any) -> _P: ... else: - def __new__(cls: Type[_P], *args: Union[str, os.PathLike], + def __new__(cls: Type[_P], *args: Union[str, os.PathLike[str]], **kwargs: Any) -> _P: ... def absolute(self: _P) -> _P: ... diff --git a/stdlib/3/os/__init__.pyi b/stdlib/3/os/__init__.pyi index 817e50540..a4f5ccb06 100644 --- a/stdlib/3/os/__init__.pyi +++ b/stdlib/3/os/__init__.pyi @@ -118,13 +118,27 @@ if sys.version_info >= (3, 6): _PathType = Union[bytes, Text] -if sys.version_info >= (3, 5): - class DirEntry: +if sys.version_info >= (3, 6): + class DirEntry(PathLike[AnyStr]): # This is what the scandir interator yields # The constructor is hidden - name = '' - path = '' + name = ... # type: AnyStr + path = ... # type: AnyStr + def inode(self) -> int: ... + def is_dir(self, follow_symlinks: bool = ...) -> bool: ... + def is_file(self, follow_symlinks: bool = ...) -> bool: ... + def is_symlink(self) -> bool: ... + def stat(self) -> stat_result: ... + + def __fspath__(self) -> AnyStr: ... +elif sys.version_info >= (3, 5): + class DirEntry(Generic[AnyStr]): + # This is what the scandir interator yields + # The constructor is hidden + + name = ... # type: AnyStr + path = ... # type: AnyStr def inode(self) -> int: ... def is_dir(self, follow_symlinks: bool = ...) -> bool: ... def is_file(self, follow_symlinks: bool = ...) -> bool: ... @@ -308,9 +322,9 @@ if sys.version_info >= (3, 3): def rmdir(path: _PathType) -> None: ... if sys.version_info >= (3, 5): @overload - def scandir(path: str = ...) -> Iterator[DirEntry]: ... + def scandir(path: str = ...) -> Iterator[DirEntry[str]]: ... @overload - def scandir(path: bytes) -> Iterator[DirEntry]: ... + def scandir(path: bytes) -> Iterator[DirEntry[bytes]]: ... def stat(path: _PathType) -> stat_result: ... def stat_float_times(newvalue: Union[bool, None] = ...) -> bool: ... def statvfs(path: _PathType) -> statvfs_result: ... # Unix only