Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
wants to merge 6 commits 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
26 changes: 20 additions & 6 deletions stdlib/2and3/filecmp.pyi
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]],
Copy link
Member

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) returns Tuple[List[bytes], ...]
  • cmpfiles(str, str) returns Tuple[List[str], ...]
  • cmpfiles(PathLike[str], PathLike[str]) returns Tuple[List[str], ...]
  • cmpfiles(PathLike[str], bytes) is an error

You can check these in mypy by using reveal_type.

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],
Copy link
Member

Choose a reason for hiding this comment

The 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 and hide parameters also accept paths?

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
Expand Down
47 changes: 44 additions & 3 deletions stdlib/3/tempfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The 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] = ...,
Expand Down Expand Up @@ -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] = ...,
Expand Down