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)
This commit is contained in:
Guido van Rossum
2017-03-21 22:26:21 -07:00
committed by Jelle Zijlstra
parent 7587e7f1c1
commit af2fadc180
2 changed files with 23 additions and 9 deletions

View File

@@ -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: ...

View File

@@ -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