Merge Python 2 and 3 shutil (#2259)

* Drop support for Python 3.3
* Merge Python 2 and 3 shutil
* Marked some arguments optional
* Changed callback return type from None to Any for more flexibility
This commit is contained in:
Sebastian Rittau
2018-06-28 05:42:58 +02:00
committed by Jelle Zijlstra
parent 86883d3df9
commit b261b228ba
3 changed files with 126 additions and 144 deletions

View File

@@ -1,26 +0,0 @@
from typing import List, Iterable, Callable, IO, AnyStr, Any, Optional, Tuple, Sequence
class Error(EnvironmentError): ...
class SpecialFileError(EnvironmentError): ...
class ExecError(EnvironmentError): ...
def copyfileobj(fsrc: IO[AnyStr], fdst: IO[AnyStr], length: int = ...) -> None: ...
def copyfile(src: unicode, dst: unicode) -> None: ...
def copymode(src: unicode, dst: unicode) -> None: ...
def copystat(src: unicode, dst: unicode) -> None: ...
def copy(src: unicode, dst: unicode) -> None: ...
def copy2(src: unicode, dst: unicode) -> None: ...
def ignore_patterns(*patterns: AnyStr) -> Callable[[AnyStr, List[AnyStr]], Iterable[AnyStr]]: ...
def copytree(src: AnyStr, dst: AnyStr, symlinks: bool = ...,
ignore: Optional[Callable[[AnyStr, List[AnyStr]], Iterable[AnyStr]]] = ...) -> None: ...
def rmtree(path: AnyStr, ignore_errors: bool = ...,
onerror: Callable[[Any, AnyStr, Any], None] = ...) -> None: ...
def move(src: unicode, dst: unicode) -> None: ...
def get_archive_formats() -> List[Tuple[str, str]]: ...
def register_archive_format(name: str, function: Callable[..., Any],
extra_args: Sequence[Tuple[str, Any]] = ...,
description: str = ...) -> None: ...
def unregister_archive_format(name: str) -> None: ...
def make_archive(base_name: AnyStr, format: str, root_dir: unicode = ...,
base_dir: unicode = ..., verbose: int = ..., dry_run: int = ...,
owner: str = ..., group: str = ..., logger: Any = ...) -> AnyStr: ...

126
stdlib/2and3/shutil.pyi Normal file
View File

@@ -0,0 +1,126 @@
import os
import sys
# 'bytes' paths are not properly supported: they don't work with all functions,
# sometimes they only work partially (broken exception messages), and the test
# cases don't use them.
from typing import (
List, Iterable, Callable, Any, Tuple, Sequence, NamedTuple, IO,
AnyStr, Optional, Union, Set, TypeVar, overload, Type
)
if sys.version_info >= (3, 6):
_Path = Union[str, os.PathLike[str]]
_AnyStr = str
_AnyPath = TypeVar("_AnyPath", str, os.PathLike[str])
# Return value of some functions that may either return a path-like object that was passed in or
# a string
_PathReturn = Any
elif sys.version_info >= (3,):
_Path = str
_AnyStr = str
_AnyPath = str
_PathReturn = str
else:
_Path = unicode
_AnyStr = TypeVar("_AnyStr", str, unicode)
_AnyPath = TypeVar("_AnyPath", str, unicode)
_PathReturn = Type[None]
if sys.version_info >= (3,):
class Error(OSError): ...
class SameFileError(Error): ...
class SpecialFileError(OSError): ...
class ExecError(OSError): ...
class ReadError(OSError): ...
class RegistryError(Exception): ...
else:
class Error(EnvironmentError): ...
class SpecialFileError(EnvironmentError): ...
class ExecError(EnvironmentError): ...
def copyfileobj(fsrc: IO[AnyStr], fdst: IO[AnyStr],
length: int = ...) -> None: ...
if sys.version_info >= (3,):
def copyfile(src: _Path, dst: _AnyPath, *,
follow_symlinks: bool = ...) -> _AnyPath: ...
def copymode(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> None: ...
def copystat(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> None: ...
def copy(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> _PathReturn: ...
def copy2(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> _PathReturn: ...
else:
def copyfile(src: _Path, dst: _Path) -> None: ...
def copymode(src: _Path, dst: _Path) -> None: ...
def copystat(src: _Path, dst: _Path) -> None: ...
def copy(src: _Path, dst: _Path) -> _PathReturn: ...
def copy2(src: _Path, dst: _Path) -> _PathReturn: ...
def ignore_patterns(*patterns: _Path) -> Callable[[Any, List[_AnyStr]], Set[_AnyStr]]: ...
if sys.version_info >= (3,):
_IgnoreFn = Union[None, Callable[[str, List[str]], Iterable[str]], Callable[[_Path, List[str]], Iterable[str]]]
def copytree(src: _Path, dst: _Path, symlinks: bool = ...,
ignore: _IgnoreFn = ...,
copy_function: Callable[[str, str], None] = ...,
ignore_dangling_symlinks: bool = ...) -> _PathReturn: ...
else:
_IgnoreFn = Union[None, Callable[[AnyStr, List[AnyStr]], Iterable[AnyStr]]]
def copytree(src: AnyStr, dst: AnyStr, symlinks: bool = ...,
ignore: _IgnoreFn = ...) -> _PathReturn: ...
if sys.version_info >= (3,):
@overload
def rmtree(path: bytes, ignore_errors: bool = ...,
onerror: Optional[Callable[[Any, str, Any], Any]] = ...) -> None: ...
@overload
def rmtree(path: _AnyPath, ignore_errors: bool = ...,
onerror: Optional[Callable[[Any, _AnyPath, Any], Any]] = ...) -> None: ...
else:
def rmtree(path: _AnyPath, ignore_errors: bool = ...,
onerror: Optional[Callable[[Any, _AnyPath, Any], Any]] = ...) -> None: ...
if sys.version_info >= (3, 5):
_CopyFn = Union[Callable[[str, str], None], Callable[[_Path, _Path], None]]
def move(src: _Path, dst: _Path,
copy_function: _CopyFn = ...) -> _PathReturn: ...
else:
def move(src: _Path, dst: _Path) -> _PathReturn: ...
if sys.version_info >= (3,):
_ntuple_diskusage = NamedTuple('usage', [('total', int),
('used', int),
('free', int)])
def disk_usage(path: _Path) -> _ntuple_diskusage: ...
def chown(path: _Path, user: Optional[str] = ...,
group: Optional[str] = ...) -> None: ...
def which(cmd: _Path, mode: int = ...,
path: Optional[_Path] = ...) -> Optional[str]: ...
def make_archive(base_name: _AnyStr, format: str, root_dir: Optional[_Path] = ...,
base_dir: Optional[_Path] = ..., verbose: bool = ...,
dry_run: bool = ..., owner: Optional[str] = ..., group: Optional[str] = ...,
logger: Optional[Any] = ...) -> _AnyStr: ...
def get_archive_formats() -> List[Tuple[str, str]]: ...
def register_archive_format(name: str, function: Callable[..., Any],
extra_args: Optional[Sequence[Union[Tuple[str, Any], List[Any]]]] = ...,
description: str = ...) -> None: ...
def unregister_archive_format(name: str) -> None: ...
if sys.version_info >= (3,):
# Should be _Path once http://bugs.python.org/issue30218 is fixed
def unpack_archive(filename: str, extract_dir: Optional[_Path] = ...,
format: Optional[str] = ...) -> None: ...
def register_unpack_format(name: str, extensions: List[str], function: Any,
extra_args: Sequence[Tuple[str, Any]] = ...,
description: str = ...) -> None: ...
def unregister_unpack_format(name: str) -> None: ...
def get_unpack_formats() -> List[Tuple[str, List[str], str]]: ...
def get_terminal_size(fallback: Tuple[int, int] = ...) -> os.terminal_size: ...

View File

@@ -1,118 +0,0 @@
# Stubs for shutil
import os
import sys
# Based on http://docs.python.org/3.2/library/shutil.html
# 'bytes' paths are not properly supported: they don't work with all functions,
# sometimes they only work partially (broken exception messages), and the test
# cases don't use them.
from typing import (
List, Iterable, Callable, Any, Tuple, Sequence, NamedTuple, IO,
AnyStr, Optional, Union
)
if sys.version_info >= (3, 6):
_Path = Union[str, os.PathLike[str]]
# Return value of some functions that may either return a path-like object that was passed in or
# a string
_PathReturn = Any
else:
_Path = str
_PathReturn = str
def copyfileobj(fsrc: IO[AnyStr], fdst: IO[AnyStr],
length: int = ...) -> None: ...
if sys.version_info >= (3, 3):
def copyfile(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> _PathReturn: ...
def copymode(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> None: ...
def copystat(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> None: ...
def copy(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> _PathReturn: ...
def copy2(src: _Path, dst: _Path, *,
follow_symlinks: bool = ...) -> _PathReturn: ...
else:
def copyfile(src: _Path, dst: _Path) -> None: ...
def copymode(src: _Path, dst: _Path) -> None: ...
def copystat(src: _Path, dst: _Path) -> None: ...
def copy(src: _Path, dst: _Path) -> None: ...
def copy2(src: _Path, dst: _Path) -> None: ...
def ignore_patterns(*patterns: _Path) -> Callable[[_Path, List[str]],
Iterable[str]]: ...
_IgnoreFn = Union[None, Callable[[str, List[str]], Iterable[str]], Callable[[_Path, List[str]], Iterable[str]]]
if sys.version_info >= (3, 3):
def copytree(src: _Path, dst: _Path, symlinks: bool = ...,
ignore: _IgnoreFn = ...,
copy_function: Callable[[str, str], None] = ...,
ignore_dangling_symlinks: bool = ...) -> _PathReturn: ...
else:
def copytree(src: str, dst: str, symlinks: bool = ...,
ignore: _IgnoreFn = ...,
copy_function: Callable[[str, str], None] = ...,
ignore_dangling_symlinks: bool = ...) -> None: ...
def rmtree(path: _Path, ignore_errors: bool = ...,
onerror: Callable[[Any, Any, Any], None] = ...) -> None: ...
if sys.version_info >= (3, 5):
def move(src: _Path, dst: _Path,
copy_function: Union[Callable[[str, str], None], Callable[[_Path, _Path], None]] = ...) -> _PathReturn: ...
elif sys.version_info >= (3, 3):
def move(src: _Path, dst: _Path) -> str: ...
else:
def move(src: _Path, dst: _Path) -> None: ...
if sys.version_info >= (3, 3):
_ntuple_diskusage = NamedTuple('usage', [('total', int),
('used', int),
('free', int)])
def disk_usage(path: _Path) -> _ntuple_diskusage: ...
def chown(path: _Path, user: Optional[str] = ...,
group: Optional[str] = ...) -> None: ...
def which(cmd: _Path, mode: int = ...,
path: Optional[_Path] = ...) -> Optional[str]: ...
if sys.version_info >= (3, 4):
class Error(OSError): ...
class SameFileError(Error): ...
class SpecialFileError(OSError): ...
class ExecError(OSError): ...
class ReadError(OSError): ...
else:
class Error(EnvironmentError): ...
class SpecialFileError(EnvironmentError): ...
class ExecError(EnvironmentError): ...
class ReadError(EnvironmentError): ...
class RegistryError(Exception): ...
def make_archive(base_name: str, format: str, root_dir: _Path = ...,
base_dir: _Path = ..., verbose: bool = ...,
dry_run: bool = ..., owner: str = ..., group: str = ...,
logger: Any = ...) -> str: ...
def get_archive_formats() -> List[Tuple[str, str]]: ...
# TODO function is a callback that receives keyword arguments; should make it not use Any
# once we have support for callable types with keyword args
def register_archive_format(name: str, function: Any,
extra_args: Sequence[Tuple[str, Any]] = ...,
description: str = ...) -> None: ...
def unregister_archive_format(name: str) -> None: ...
# Should be _Path once http://bugs.python.org/issue30218 is fixed
def unpack_archive(filename: str, extract_dir: _Path = ...,
format: str = ...) -> None: ...
def register_unpack_format(name: str, extensions: List[str], function: Any,
extra_args: Sequence[Tuple[str, Any]] = ...,
description: str = ...) -> None: ...
def unregister_unpack_format(name: str) -> None: ...
def get_unpack_formats() -> List[Tuple[str, List[str], str]]: ...
if sys.version_info >= (3, 3):
def get_terminal_size(fallback: Tuple[int, int] = ...) -> os.terminal_size: ...