-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Replace an instance of IO with a protocol #5471
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
Conversation
Can we assume that type checkers realize how |
It seems that mypy still can't. Changing the last overload to |
stdlib/zipfile.pyi
Outdated
mode: str, | ||
zipinfo: ZipInfo, | ||
pwd: Optional[bytes] = ..., | ||
close_fileobj: bool = ..., |
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 don't think this works as intended. If fileobj
is a _ZipStream
but not a _ClosableZipStream
, and you pass close_fileobj=True
, it will match this overload instead of erroring.
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.
See above: I think this is a reasonable tradeoff, considering our "prefer false negatives over false positives" policy.
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.
Then we might as well get rid of _ClosableZipStream
altogether, because distinguishing between closable and non-closable zip streams doesn't affect whether the code type-checks.
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.
But having it separate has the advantage that the following fails:
ZipExtFile(my_non_closable_stream, close_fileobj=True)
The only thing that can't be checked properly is the following:
close: bool = True
ZipExtFile(my_non_closable_stream, close_fileobj=close)
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 don't think that fails. It doesn't match the first two overloads, but it does match the third overload which just requires any stream, and bool
for close_fileobj
. (Note that True
converts to bool
.)
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.
Good point. At this point I think that maybe the first solution was the best, and until python/mypy#6113 is fixed, people just use # type: ignore
?
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.
Since we prefer false negatives, it might be better to just never require a .close()
method.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
So now there is an unnecessary |
I opened #5504 to improve this situation. |
No description provided.