Skip to content
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

Cannot infer type argument 1 of "shutil.copyfileobj" with mypy 0.780 #4201

Closed
deveshks opened this issue Jun 7, 2020 · 9 comments · Fixed by #4203
Closed

Cannot infer type argument 1 of "shutil.copyfileobj" with mypy 0.780 #4201

deveshks opened this issue Jun 7, 2020 · 9 comments · Fixed by #4203

Comments

@deveshks
Copy link

deveshks commented Jun 7, 2020

The following code used to pass mypy type check with mypy 0.770 but doesn't work for mypy 0.780

import shutil

filename1 = '/foo/bar'
filename2 = '/foo/baz'

srcfp =  open(filename1, 'rb')
destfp = open(filename2, 'wb')

shutil.copyfileobj(srcfp, destfp)

> mypy 0.770

$ mypy --version
mypy 0.770
$  mypy scratch.py
Success: no issues found in 1 source file

> mypy 0.780

$ mypy --version
mypy 0.780

$ scratch.py  
scratch.py:9: error: Cannot infer type argument 1 of "copyfileobj"

Do let me know if this not a typeshed issue, but instead is a mypy issue.

@hauntsaninja
Copy link
Collaborator

The relevant shutil types haven't changed since 2018. If this is a typeshed issue, it might be related to changes in IO types (although I'm not quite sure what has made it to which mypy version).

I think this is a mypy issue, because throwing in reveal_type(srcfp/destfp/shutil.copyfileobj) get me the same thing across both mypy versions.

Note that your code does crash when I run it, presumably because you need to open srcfp with "rb", although you do get the same error when you fix the mode.

@deveshks
Copy link
Author

deveshks commented Jun 7, 2020

The relevant shutil types haven't changed since 2018. If this is a typeshed issue, it might be related to changes in IO types (although I'm not quite sure what has made it to which mypy version).

I think this is a mypy issue, because throwing in reveal_type(srcfp/destfp/shutil.copyfileobj) get me the same thing across both mypy versions.

Okay, let me open it with mypy.

Note that your code does crash when I run it, presumably because you need to open srcfp with "rb", although you do get the same error when you fix the mode.

The code was just meant to be an example for type checking 😊 I didn't check it for correctness.

@deveshks
Copy link
Author

deveshks commented Jun 8, 2020

The relevant shutil types haven't changed since 2018. If this is a typeshed issue, it might be related to changes in IO types (although I'm not quite sure what has made it to which mypy version).

Also how would I make sure that this is not the case between mypy 0.770 released on 10th March and mypy 0.780 released on 2nd June 2020.

@deveshks deveshks reopened this Jun 8, 2020
@srittau
Copy link
Collaborator

srittau commented Jun 8, 2020

I believe the problem is with the order of the overloads of BinaryIO.write(). The first overload is bytearray, the second is bytes. mypy tries to infer bytearray as the generic type, which causes problems. I don't see why the overloads are necessary anyway, so I will open a PR to remove them.

srittau added a commit to srittau/typeshed that referenced this issue Jun 8, 2020
write() is inherited from IO[bytes], where it's defined as
`def write(self, s: AnyStr) -> int: ...`. If AnyStr is bytes,
this should accept bytes, bytearray, and memoryview, so the
overload is unnecessary.

Closes: python#4201
@deveshks
Copy link
Author

deveshks commented Jun 8, 2020

I believe the problem is with the order of the overloads of BinaryIO.write(). The first overload is bytearray, the second is bytes. mypy tries to infer bytearray as the generic type, which causes problems. I don't see why the overloads are necessary anyway, so I will open a PR to remove them.

Thanks for the quick PR. Should I go ahead and close the related issue I opened with mypy about this?

@srittau
Copy link
Collaborator

srittau commented Jun 8, 2020

I think there is something weird going on with mypy, so let them analyze it.

JelleZijlstra pushed a commit that referenced this issue Jun 8, 2020
write() is inherited from IO[bytes], where it's defined as
`def write(self, s: AnyStr) -> int: ...`. If AnyStr is bytes,
this should accept bytes, bytearray, and memoryview, so the
overload is unnecessary.

Closes: #4201
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this issue Jun 26, 2020
write() is inherited from IO[bytes], where it's defined as
`def write(self, s: AnyStr) -> int: ...`. If AnyStr is bytes,
this should accept bytes, bytearray, and memoryview, so the
overload is unnecessary.

Closes: python#4201
@pmhahn
Copy link
Contributor

pmhahn commented May 11, 2023

Sorry to post to this old issue, but I still get this error:

import shutil
from typing import IO


def _copy_data(src_file: IO[bytes]) -> None:
    reveal_type(src_file)
    shutil.copyfileobj(src_file, src_file)

mypy-1.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl reports the following error:

# mypy mypy-bug.py 
mypy-bug.py:6: note: Revealed type is "typing.IO[builtins.bytes]"
mypy-bug.py:7: error: Cannot infer type argument 1 of "copyfileobj"  [misc]
Found 1 error in 1 file (checked 1 source file)

What am I missing? For me the type hints should allow this:

# find /usr/local/lib/python3.11 -name shutil.py\* -exec grep --color -n 'def copyfileobj(' {} +
/usr/local/lib/python3.11/site-packages/mypy/typeshed/stdlib/shutil.pyi:51:    def copyfileobj(fsrc: SupportsRead[AnyStr], fdst: SupportsWrite[AnyStr], length: int = 0) -> None: ...
/usr/local/lib/python3.11/site-packages/mypy/typeshed/stdlib/shutil.pyi:54:    def copyfileobj(fsrc: SupportsRead[AnyStr], fdst: SupportsWrite[AnyStr], length: int = 16384) -> None: ...
/usr/local/lib/python3.11/shutil.py:189:def copyfileobj(fsrc, fdst, length=0):

@AlexWaygood
Copy link
Member

AlexWaygood commented May 11, 2023

@pmhahn, that's a mypy bug, not a typeshed bug (although it was exposed by a recent change in typeshed). It's already tracked in the mypy repo: please see python/mypy#15031 and python/mypy#14943.

In general, please don't post on extremely old issues that haven't seen activity in three years :) In such cases, it's highly likely that the problem you're encountering will be a new bug, rather than the same issue that was reported and closed 3 years ago. That's indeed the case here. Please just open a new issue in the future!

@AlexWaygood
Copy link
Member

(python/mypy#14975 appears to fix the issue, but it's still under review by the mypy maintainers.)

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 a pull request may close this issue.

5 participants