-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
filecmp and tempfile can handle pathlib.Path #2267
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
Changes from all commits
76f9fdb
26cb0d4
96f612f
d36f114
84e1a45
3e64cad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
# Stubs for filecmp (Python 2/3) | ||
import sys | ||
import os | ||
from typing import AnyStr, Callable, Dict, Generic, Iterable, List, Optional, Sequence, Tuple, Union, Text | ||
|
||
DEFAULT_IGNORES = ... # type: List[str] | ||
|
||
def cmp(f1: Union[bytes, Text], f2: Union[bytes, Text], shallow: Union[int, bool] = ...) -> bool: ... | ||
def cmpfiles(a: AnyStr, b: AnyStr, common: Iterable[AnyStr], | ||
shallow: Union[int, bool] = ...) -> Tuple[List[AnyStr], List[AnyStr], List[AnyStr]]: ... | ||
if sys.version_info >= (3, 6): | ||
def cmp(f1: Union[bytes, Text, os.PathLike], f2: Union[bytes, Text, os.PathLike], | ||
shallow: Union[int, bool] = ...) -> bool: ... | ||
def cmpfiles(a: Union[AnyStr, os.PathLike], b: Union[AnyStr, os.PathLike], common: Iterable[Union[AnyStr, os.PathLike]], | ||
shallow: Union[int, bool] = ...) -> Tuple[List[AnyStr], List[AnyStr], List[AnyStr]]: ... | ||
else: | ||
def cmp(f1: Union[bytes, Text], f2: Union[bytes, Text], shallow: Union[int, bool] = ...) -> bool: ... | ||
def cmpfiles(a: AnyStr, b: AnyStr, common: Iterable[AnyStr], | ||
shallow: Union[int, bool] = ...) -> Tuple[List[AnyStr], List[AnyStr], List[AnyStr]]: ... | ||
|
||
|
||
class dircmp(Generic[AnyStr]): | ||
def __init__(self, a: AnyStr, b: AnyStr, | ||
ignore: Optional[Sequence[AnyStr]] = ..., | ||
hide: Optional[Sequence[AnyStr]] = ...) -> None: ... | ||
|
||
if sys.version_info >= (3, 6): | ||
def __init__(self, a: Union[AnyStr, os.PathLike], b: Union[AnyStr, os.PathLike], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we should use PathLike[AnyStr] but should verify it actually works. Also, do the |
||
ignore: Optional[Sequence[AnyStr]] = ..., | ||
hide: Optional[Sequence[AnyStr]] = ...) -> None: ... | ||
else: | ||
def __init__(self, a: AnyStr, b: AnyStr, | ||
ignore: Optional[Sequence[AnyStr]] = ..., | ||
hide: Optional[Sequence[AnyStr]] = ...) -> None: ... | ||
|
||
left = ... # type: AnyStr | ||
right = ... # type: AnyStr | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ | |
# based on http://docs.python.org/3.3/library/tempfile.html | ||
|
||
import sys | ||
import os | ||
from types import TracebackType | ||
from typing import Any, AnyStr, Generic, IO, Optional, Tuple, Type | ||
from typing import Any, AnyStr, Generic, IO, Optional, Tuple, Type, Union | ||
|
||
# global variables | ||
TMP_MAX: int | ||
|
@@ -14,6 +15,48 @@ template = ... # type: str | |
|
||
|
||
if sys.version_info >= (3, 5): | ||
def gettempdirb() -> bytes: ... | ||
def gettempprefixb() -> bytes: ... | ||
|
||
if sys.version_info >= (3, 6): | ||
def TemporaryFile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of these functions have the same issue with AnyStr. |
||
mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., | ||
newline: Optional[str] = ..., suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
dir: Optional[Union[AnyStr, os.PathLike]] = ... | ||
) -> IO[Any]: | ||
... | ||
def NamedTemporaryFile( | ||
mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., | ||
newline: Optional[str] = ..., suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
dir: Optional[Union[AnyStr, os.PathLike]] = ..., delete: bool =... | ||
) -> IO[Any]: | ||
... | ||
def SpooledTemporaryFile( | ||
max_size: int = ..., mode: str = ..., buffering: int = ..., | ||
encoding: str = ..., newline: str = ..., suffix: Optional[AnyStr] = ..., | ||
prefix: Optional[AnyStr] = ..., dir: Optional[Union[AnyStr, os.PathLike]] = ... | ||
) -> IO[Any]: | ||
... | ||
|
||
class TemporaryDirectory(Generic[AnyStr]): | ||
name = ... # type: str | ||
def __init__(self, suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
dir: Optional[Union[AnyStr, os.PathLike]] = ...) -> None: ... | ||
def cleanup(self) -> None: ... | ||
def __enter__(self) -> AnyStr: ... | ||
def __exit__(self, exc_type: Optional[Type[BaseException]], | ||
exc_val: Optional[BaseException], | ||
exc_tb: Optional[TracebackType]) -> bool: ... | ||
|
||
def mkstemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
dir: Optional[Union[AnyStr, os.PathLike]] = ..., | ||
text: bool = ...) -> Tuple[int, AnyStr]: ... | ||
def mkdtemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
dir: Optional[Union[AnyStr, os.PathLike]] = ...) -> AnyStr: ... | ||
def mktemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
dir: Optional[Union[AnyStr, os.PathLike]] = ...) -> AnyStr: ... | ||
|
||
elif sys.version_info >= (3, 5): | ||
def TemporaryFile( | ||
mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., | ||
newline: Optional[str] = ..., suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., | ||
|
@@ -49,8 +92,6 @@ if sys.version_info >= (3, 5): | |
dir: Optional[str] = ...) -> AnyStr: ... | ||
def mktemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., dir: Optional[AnyStr] = ...) -> AnyStr: ... | ||
|
||
def gettempdirb() -> bytes: ... | ||
def gettempprefixb() -> bytes: ... | ||
else: | ||
def TemporaryFile( | ||
mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this uses AnyStr, we should ideally also use a type variable in the PathLike case (so
PathLike[AnyStr]
). But this sort of things tends to run into mypy bugs sometimes. We should check a few cases like these:cmpfiles(bytes, bytes)
returnsTuple[List[bytes], ...]
cmpfiles(str, str)
returnsTuple[List[str], ...]
cmpfiles(PathLike[str], PathLike[str])
returnsTuple[List[str], ...]
cmpfiles(PathLike[str], bytes)
is an errorYou can check these in mypy by using
reveal_type
.