-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
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.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
stdlib/socket.pyi
Outdated
def makefile( | ||
self, | ||
mode: Literal["r", "w", "rw", "wr", ""] = ..., | ||
mode: Literal["rwb", "rbw", "wrb", "wbr", "brw", "bwr"], | ||
buffering: int | None = ..., |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |