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

shutil.copyfileobj stopped working in 1.2.0 #15031

Closed
henryiii opened this issue Apr 11, 2023 · 6 comments · Fixed by #17873
Closed

shutil.copyfileobj stopped working in 1.2.0 #15031

henryiii opened this issue Apr 11, 2023 · 6 comments · Fixed by #17873
Labels
bug mypy got something wrong topic-type-variables

Comments

@henryiii
Copy link
Contributor

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.
@henryiii henryiii added the bug mypy got something wrong label Apr 11, 2023
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Apr 11, 2023

@henryiii
Copy link
Contributor Author

Should I open an issue over there, then?

@JelleZijlstra
Copy link
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).

@AlexWaygood
Copy link
Member

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

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 11, 2023

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.

NiklasRosenstein added a commit to NiklasRosenstein/novella that referenced this issue May 28, 2023
asottile added a commit to asottile/rubyvenv that referenced this issue Jun 28, 2023
NicolasT pushed a commit to NicolasT/gptsum that referenced this issue Aug 27, 2023
This requires an explicit `cast` of `shutil.copyfileobj` to work-around
a typeshed/Mypy issue.

Bumps [mypy](https://github.com/python/mypy) from 1.1.1 to 1.4.1.
- [Commits](python/mypy@v1.1.1...v1.4.1)

---
updated-dependencies:
- dependency-name: mypy
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

See: python/mypy#15031
See: https://github.com/python/mypy/pull/14902/files#diff-363460977156fcfda748f21565484fe1d5862edf2823e9784cbf34d1e52ff2f2
See: python/mypy#14975

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Nicolas Trangez <ikke@nicolast.be>
NicolasT pushed a commit to NicolasT/gptsum that referenced this issue Aug 27, 2023
This requires an explicit `cast` of `shutil.copyfileobj` to work-around
a typeshed/Mypy issue.

Bumps [mypy](https://github.com/python/mypy) from 1.1.1 to 1.4.1.
- [Commits](python/mypy@v1.1.1...v1.4.1)

---
updated-dependencies:
- dependency-name: mypy
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

See: python/mypy#15031
See: https://github.com/python/mypy/pull/14902/files#diff-363460977156fcfda748f21565484fe1d5862edf2823e9784cbf34d1e52ff2f2
See: python/mypy#14975

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Nicolas Trangez <ikke@nicolast.be>
NicolasT pushed a commit to NicolasT/gptsum that referenced this issue Aug 27, 2023
This requires an explicit `cast` of `shutil.copyfileobj` to work-around
a typeshed/Mypy issue.

Bumps [mypy](https://github.com/python/mypy) from 1.1.1 to 1.4.1.
- [Commits](python/mypy@v1.1.1...v1.4.1)

---
updated-dependencies:
- dependency-name: mypy
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

See: python/mypy#15031
See: https://github.com/python/mypy/pull/14902/files#diff-363460977156fcfda748f21565484fe1d5862edf2823e9784cbf34d1e52ff2f2
See: python/mypy#14975

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Nicolas Trangez <ikke@nicolast.be>
@JukkaL JukkaL closed this as completed in 3c09b32 Oct 3, 2024
@henryiii
Copy link
Contributor Author

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
Labels
bug mypy got something wrong topic-type-variables
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants