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

Conversation

scivision
Copy link
Contributor

@scivision scivision commented Jun 20, 2018

filecmp can handle pathlib.Path, including:

  • filecmp.cmp
  • filecmp.cmpfiles
  • filecmp.dircmp

Before this patch, doing something like

from pathlib import Path
import filecmp

filecmp.dircmp(Path('/tmp'), '/tmp')

would work fine when executed, but would give mypy errors like

error: Value of type variable "AnyStr" of "dircmp" cannot be "Path"

After this change, type checking works as expected

@scivision
Copy link
Contributor Author

scivision commented Jun 20, 2018

Hmm, I think this may need to be split into stdlib/3.6/filecmp.pyi.

Do the maintainers concur?

The other option per https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#stub-versioning would be to use if statements inside the existing filecmp.pyi.

@gvanrossum
Copy link
Member

I would use if sys.version_info >= (3, 6).

@scivision scivision changed the title filecmp can handle pathlib.Path filecmp and tempfile can handle pathlib.Path Jun 21, 2018
@scivision scivision changed the title filecmp and tempfile can handle pathlib.Path filecmp and tempfile can handle pathlib.Path Jun 21, 2018
@scivision
Copy link
Contributor Author

OK I believe the fixes are ready to go.

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.

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?

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.

@srittau
Copy link
Collaborator

srittau commented Oct 21, 2018

@scivision Are you still interested in this pull request?

@scivision
Copy link
Contributor Author

Hi @srittau it seems like the fix grew bigger. I don't have much time lately so I have just been using type: ignore

@srittau
Copy link
Collaborator

srittau commented Nov 19, 2018

Thank you for your work on this. I am closing this PR for now. Please reopen it or open a new PR if you wish to finish it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants