-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
"Missing return statement" caused by context manager? #7577
Comments
If the |
I wonder if we should just roll this back. This was reported several times
since the 0.730 release and the answer is not easy to find in the docs.
There are a *lot* of context managers in the world, and I would guess that
few of them ever swallow exceptions.
Worse, there may be situations where a subclass could override `__exit__`
to swallow some exceptions for a base class that doesn't -- if the base
class' `__exit__` has type `Literal[False]` then wouldn't the subclass be
in error if it had `__exit__` returning type `bool`?
Or maybe the error message could have a note pointing to this solution, if
there's a context manager involved?
|
It is not as easy as that, since reverting will cause false positives (like falsely complaining about unreachable code).
Unfortunately this may be not easy due to how we track the control flow (binder). We can move the docs to a more prominent location. |
I agree that we should do something about this. I think that reverting is fine, at least temporarily, since I generally prefer preserving existing "questionable" behavior to introducing new, different "questionable" behavior (to avoid friction) -- unless we can expect that the new behavior occurs much less frequently. It would be better to fix this or to improve the generated error message, however. Note that a Here are some ideas about how we could make this better without reverting:
Only 2 and 4 help with stubs. My current preference would be to implement 1, 4 and 2 (in this order). 3 seems too magical and ad hoc. |
…7655) 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.
Mypy will complain about
func()
, saying it doesn't have a return statement.Is this intended?
Note: I also tried
class TestManager(ContextManager[None]):
just to see if that would help mypy but it didn't change anything.The text was updated successfully, but these errors were encountered: