Skip to content

Give error if __exit__ returns False but is declared to return bool#7655

Merged
JukkaL merged 8 commits intomasterfrom
exit-return
Oct 9, 2019
Merged

Give error if __exit__ returns False but is declared to return bool#7655
JukkaL merged 8 commits intomasterfrom
exit-return

Conversation

@JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Oct 8, 2019

Mypy can give false positives about missing return statements
if __exit__ that always returns False is annotated to return
bool instead of Literal[False].

Add new error code and documentation for the error code since
this error condition is not very obvious.

Fixes #7577.

There are two major limitations:

  1. This doesn't support async context managers.
  2. This won't help if a stub has an invalid __exit__ return type.

I'll create a follow-up issues about the above.

@JukkaL JukkaL requested a review from msullivan October 8, 2019 18:26
@Michael0x2a
Copy link
Collaborator

Maybe we could also suggest returning Optional[bool]? I believe that'll also make mypy assume the function will not swallow exceptions.

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

Looks good. This will avoid a pretty tricky failure mode.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 9, 2019

Maybe we could also suggest returning Optional[bool]? I believe that'll also make mypy assume the function will not swallow exceptions.

That doesn't sound correct, though. If the return type is Optional[bool], the function could still return True and swallow exceptions.

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.

"Missing return statement" caused by context manager?

3 participants