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

Change IO/IOBase.__exit__ return type to None #7471

Merged
merged 4 commits into from
Mar 10, 2022
Merged

Conversation

nbdaaron
Copy link
Contributor

similar to #7470, this was caught by quora/pyanalyze#496.

I don't believe __exit__ in IOBase returns anything: https://github.com/python/cpython/blob/main/Lib/_pyio.py#L513 and I think typing.IO just represents I/O streams returned by open() which I don't think return anything from __exit__

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

zulip (https://github.com/zulip/zulip)
+ zerver/lib/test_console_output.py:123: error: Return type "Optional[bool]" of "__exit__" incompatible with return type "None" in supertype "IO"  [override]

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

The C version of IOBase.__exit__ just returns self.close(), and close() is expected to return None.

@srittau
Copy link
Collaborator

srittau commented Mar 10, 2022

While I think it's okay to change the return type for IOBase if the subclasses all return None, I'm not sure we should do the same for IO as it is a semi-protocol and sub-classes could decide to return True.

@JelleZijlstra
Copy link
Member

I feel like the contract for IO.__exit__ is that it closes the file. A subclass that also suppresses exceptions would be unexpected enough that it's good for a type checker to warn about it.

@srittau srittau merged commit a3e62d5 into python:master Mar 10, 2022
@AlexWaygood AlexWaygood changed the title change Change IO/IOBase.__exit__ return type to None Change IO/IOBase.__exit__ return type to None Jun 11, 2022
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.

3 participants