Skip to content

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

Closed

Description

@deveshks

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.

Activity

  1. hauntsaninja commented on Jun 7, 2020

    @hauntsaninja
    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.

  2. deveshks commented on Jun 7, 2020

    @deveshks
    Author

    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.

  3. deveshks commented on Jun 8, 2020

    @deveshks
    Author

    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.

  4. srittau commented on Jun 8, 2020

    @srittau
    Collaborator

    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.

  5. added a commit that references this issue on Jun 8, 2020
    3933b61
  6. deveshks commented on Jun 8, 2020

    @deveshks
    Author

    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?

  7. srittau commented on Jun 8, 2020

    @srittau
    Collaborator

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

  8. added a commit that references this issue on Jun 8, 2020
    206bff4
  9. added a commit that references this issue on Jun 26, 2020
    f529260
  10. pmhahn commented on May 11, 2023

    @pmhahn
    Contributor

    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):
    
  11. AlexWaygood commented on May 11, 2023

    @AlexWaygood
    Member

    @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!

  12. AlexWaygood commented on May 11, 2023

    @AlexWaygood
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions