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

Add missing tarfile.open overload for writable objects #12117

Merged
merged 2 commits into from
Jun 21, 2024

Conversation

max-muoto
Copy link
Contributor

@max-muoto max-muoto commented Jun 8, 2024

Fix issues detailed in #5491 by adding an appropiate overload when the tar file is being opened in a write mode.

@max-muoto max-muoto changed the title Update tarfile.pyi with open() function overloads Add missing tarfile.open overload for writable objects Jun 8, 2024

This comment has been minimized.

@max-muoto
Copy link
Contributor Author

Originally I had overloads for each literal combination, but had issues with MyPy due to the pipe operators in them, as python/mypy#17148 hasn't been released yet it seems.

@max-muoto max-muoto marked this pull request as ready for review June 8, 2024 19:03
@max-muoto max-muoto marked this pull request as draft June 8, 2024 19:04
@max-muoto max-muoto marked this pull request as ready for review June 8, 2024 19:05
Copy link
Contributor

github-actions bot commented Jun 8, 2024

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There still a bunch of possible modes missing, but this is a good first step, thanks!

@srittau srittau merged commit 72a85f4 into python:main Jun 21, 2024
63 checks passed
tsibley added a commit to nextstrain/cli that referenced this pull request Sep 5, 2024
This started failing recently with newer typeshed stubs for
tarfile.open() (and/or newer stubs for requests.Response?).

mypy fails with:

    nextstrain/cli/runner/conda.py:220: error: Argument "fileobj" to "open" has incompatible type "Union[HTTPResponse, Any]"; expected "Optional[IO[bytes]]"

and pyright with:

    nextstrain/cli/runner/conda.py:220:14 - error: No overloads for "open" match the provided arguments (reportCallIssue)
    nextstrain/cli/runner/conda.py:220:58 - error: Argument of type "Literal['r|*']" cannot be assigned to parameter "mode" of type "_FileCreationModes" in function "open"
      Type "Literal['r|*']" is not assignable to type "_FileCreationModes"
        "Literal['r|*']" is not assignable to type "Literal['a']"
        "Literal['r|*']" is not assignable to type "Literal['w']"
        "Literal['r|*']" is not assignable to type "Literal['x']" (reportArgumentType)

I'm guessing the difference is because they match the different
overloads of tarfile.open().

I dug into it a little¹, but the stubs here seem a bit scattershot at
the moment so I stopped and went with an ignore.  An alternative fix is
casting response.raw to BinaryIO, but that seemed uglier and worse than
an ignore.

¹ See <python/typeshed#12117> (implicated)
  and <python/typeshed#12181> (open, may fix?)
  and <python/typeshed#12182> (open, may fix?)
tsibley added a commit to nextstrain/cli that referenced this pull request Sep 5, 2024
This started failing recently with newer typeshed stubs for
tarfile.open() (and/or newer stubs for requests.Response?).

mypy fails with:

    nextstrain/cli/runner/conda.py:220: error: Argument "fileobj" to "open" has incompatible type "Union[HTTPResponse, Any]"; expected "Optional[IO[bytes]]"

and pyright with:

    nextstrain/cli/runner/conda.py:220:14 - error: No overloads for "open" match the provided arguments (reportCallIssue)
    nextstrain/cli/runner/conda.py:220:58 - error: Argument of type "Literal['r|*']" cannot be assigned to parameter "mode" of type "_FileCreationModes" in function "open"
      Type "Literal['r|*']" is not assignable to type "_FileCreationModes"
        "Literal['r|*']" is not assignable to type "Literal['a']"
        "Literal['r|*']" is not assignable to type "Literal['w']"
        "Literal['r|*']" is not assignable to type "Literal['x']" (reportArgumentType)

I'm guessing the difference is because they match the different
overloads of tarfile.open().

I dug into it a little¹, but the stubs here seem a bit scattershot at
the moment so I stopped and went with an ignore.  An alternative fix is
casting response.raw to BinaryIO, but that seemed uglier and worse than
an ignore.

¹ See <python/typeshed#12117> (implicated)
  and <python/typeshed#12181> (open, may fix?)
  and <python/typeshed#12182> (open, may fix?)
max-muoto added a commit to max-muoto/typeshed that referenced this pull request Sep 8, 2024
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 this pull request may close these issues.

2 participants