Skip to content

shutil.copyfileobj stopped working in 1.2.0 #15031

Closed

Description

@henryiii

Bug Report

MyPy 1.2.0 doesn't like shutil.copyfileobj anymore, with "can't infer type argument 1".

Hit in pypa/cibuildwheel#1468.

To Reproduce

import shutil
import typing

stdin = typing.cast(typing.IO[bytes], None)
with open("random_file.txt", "rb") as from_file:
    shutil.copyfileobj(from_file, stdin)

Expected Behavior

$ pipx run mypy==1.1.1 --strict tmp.py
Success: no issues found in 1 source file

https://mypy-play.net/?mypy=1.1.1&python=3.11&gist=7f9fbe8e5df5f52e5c639e30671eb208

Actual Behavior

$ pipx run mypy==1.2.0 --strict tmp.py
tmp.py:6: error: Cannot infer type argument 1 of "copyfileobj"  [misc]
Found 1 error in 1 file (checked 1 source file)

https://mypy-play.net/?mypy=latest&python=3.11&gist=dbc914004646a0c0ad90926c501d1daa

Your Environment

  • Mypy version used: 1.1.1 & 1.2.0
  • Mypy command-line flags: (Seen in playground with nothing)
  • Mypy configuration options from mypy.ini (and other config files): (Seen in playground with nothing)
  • Python version used: 3.11 and others.

Activity

  1. hauntsaninja commented on Apr 11, 2023

    @hauntsaninja
    Collaborator
  2. henryiii commented on Apr 11, 2023

    @henryiii
    ContributorAuthor

    Should I open an issue over there, then?

  3. JelleZijlstra commented on Apr 11, 2023

    @JelleZijlstra
    Member

    I think this is actually a mypy bug, though one that was exposed through the typeshed change. It would help to try to create a reproducer that doesn't rely on the stdlib (basically, copy-paste in the definitions of IO, open, and copyfileobj, and simplify them as much as possible).

  4. AlexWaygood commented on Apr 11, 2023

    @AlexWaygood
    Member

    It looks like #14975 fixes this false positive, which strongly implies to me that this is the same bug as #14943

  5. AlexWaygood commented on Apr 11, 2023

    @AlexWaygood
    Member

    Here's a minimal-ish repro that doesn't rely on typeshed's stdlib stubs:

    from typing import AnyStr, Generic, Protocol, TypeVar, overload
    
    T_co = TypeVar("T_co", covariant=True)
    T_contra = TypeVar("T_contra", contravariant=True)
    
    class SupportsRead(Generic[T_co]):
        def read(self) -> T_co: ...
    
    class SupportsWrite(Protocol[T_contra]):
        def write(self, s: T_contra) -> object: ...
    
    def copyfileobj(fsrc: SupportsRead[AnyStr], fdst: SupportsWrite[AnyStr]) -> None: ...
    
    class WriteToMe(Generic[AnyStr]):
        @overload
        def write(self: WriteToMe[str], s: str) -> int: ...
        @overload
        def write(self: WriteToMe[bytes], s: bytes) -> int: ...
    
    class WriteToMeOrReadFromMe(WriteToMe[AnyStr], SupportsRead[AnyStr]): ...
    
    copyfileobj(WriteToMeOrReadFromMe[bytes](), WriteToMe[bytes]())

    https://mypy-play.net/?mypy=latest&python=3.11&gist=7443887f86538e58f87bc9b6f1957ce5

    It does indeed end up looking very similar to #14943.

  6. added a commit that references this issue on Jun 28, 2023
  7. added a commit that references this issue on Oct 3, 2024
    3c09b32
  8. henryiii commented on Oct 21, 2024

    @henryiii
    ContributorAuthor

    Thanks! Type ignore removed in pypa/cibuildwheel#2054.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions