Skip to content

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

Closed

Description

@deveshks

(This was originally filed as python/typeshed#4201, but it was suggested
to file here instead)

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"

Activity

  1. srittau commented on Jun 8, 2020

    @srittau
    Contributor

    Something seems to have changed between mypy 0.770 and 0.780 regarding how overloads are handled. BinaryIO.write() has two overloads: bytearray and bytes in that order (since 2018). When removing the superclass IO[bytes] from BinaryIO, the error message changes to:

    error: Argument 1 to "copyfileobj" has incompatible type "BinaryIO"; expected "_Reader[bytearray]"
    

    Here is an example that doesn't rely on typeshed types (assuming that IO is special-cased):

    from typing import TypeVar, Protocol, Any, overload, IO
    
    _S_co = TypeVar("_S_co", covariant=True)
    _S_contra = TypeVar("_S_contra", contravariant=True)
    _T = TypeVar("_T")
    
    class BinaryIO(IO[bytes]):
        @overload
        def write(self, s: bytearray) -> int: ...
        @overload
        def write(self, s: bytes) -> int: ...
        def write(self, s) -> int:
            return 0
    
    class _Reader(Protocol[_S_co]):
        def read(self, n: int) -> _S_co: ...
    
    class _Writer(Protocol[_S_contra]):
        def write(self, s: _S_contra) -> Any: ...
    
    def foo(fsrc: _Reader[_T], fdst: _Writer[_T]) -> None:
        pass
    
    srcfp: BinaryIO
    destfp: BinaryIO
    
    foo(srcfp, destfp)
  2. added a commit that references this issue on Jul 11, 2020
  3. added a commit that references this issue on Jul 17, 2020
  4. McSinyx commented on Oct 12, 2020

    @McSinyx

    This seems to be fixed by 0.790.

  5. hauntsaninja commented on Oct 12, 2020

    @hauntsaninja
    Collaborator

    Thanks for reporting the fix! Used mypy_primer to bisect it to #9064 , so probably the result of python/typeshed#4203

    While I'm here, I also bisected the original regression to #8845 . Since this issue is "fixed" by the typeshed change, I'm going to close — but maybe @msullivan is curious enough to take a closer look.

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