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

Improve I/O types in socket.pyi #7852

Merged
merged 4 commits into from
May 17, 2022
Merged

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented May 17, 2022

  • Use a protocol for socket.sendfile() "file" argument.
  • Use concrete classes for socket.makefile() return types.

* Use a protocol for socket.sendfile() "file" argument.
* Use concrete classes for socket.makefile() return types.
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

stdlib/socket.pyi Outdated Show resolved Hide resolved
def makefile(
self,
mode: Literal["r", "w", "rw", "wr", ""] = ...,
mode: Literal["rwb", "rbw", "wrb", "wbr", "brw", "bwr"],
buffering: int | None = ...,
Copy link
Member

Choose a reason for hiding this comment

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

This will pick the wrong overload when the user passes something that looks like an int to the typechecker but is 0 at runtime. For builtins.open, we handle that with a fallback overload that returns BinaryIO; we should do that here too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll use IOBase for the fallback for now as I'm interested what stubtest says. Another case for my pet peeve python/typing#566. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JelleZijlstra primer passes. I'd prefer to use the concrete IOBase here, instead of the BinaryIO pseudo-protocol. We can always change it later if someone complains. Maybe even returning a union here is alternative. Type checkers should treat it the same as IOBase, but it's closer to the implementation.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. We could try these approaches also with open().

@github-actions
Copy link
Contributor

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

@JelleZijlstra JelleZijlstra merged commit f21898a into python:master May 17, 2022
@srittau srittau deleted the socket-io branch May 17, 2022 13:12
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