-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Honor NoReturn
as __setitem__
return type to mark unreachable code.
#12572
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
Thanks, this looks great!
One small adjustment, could you apply this diff:
diff --git a/mypy/checker.py b/mypy/checker.py
index 3340cb4b4..bf9905e7d 100644
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -4055,7 +4055,8 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
[nodes.ARG_POS, nodes.ARG_POS],
context,
)
- if isinstance(get_proper_type(res_type), UninhabitedType):
+ res_type = get_proper_type(res_type)
+ if isinstance(res_type, UninhabitedType) and not res_type.ambiguous:
self.binder.unreachable()
def try_infer_partial_type_from_indexed_assignment(
And add a test case like:
from typing import NoReturn, TypeVar
T = TypeVar("T")
class Foo:
def __setitem__(self, key: str, value: str) -> T:
raise Exception
def f() -> None:
Foo()['a'] = 'a'
x = 0
I'm happy to apply the diff (PR updated), but the proposed test case looks weird (unbound TypeVar), and I expect only one error ("a function returning TypeVar...", if I recall properly) unrelated to NoReturn. Could you clarify your intent here? |
It was meant to be a test for the additional diff I proposed. I think on the original PR you'd get an incorrect unreachable warning on that test. (I can double check when I'm at a computer) |
This comment has been minimized.
This comment has been minimized.
Sorry, haven't noticed that. Updated the PR with this test. |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
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.
Thank you!
Description
In general code that follows any calls to methods annotated with
NoReturn
is considered unreachable. However calls likevariable['foo'] = 'foo'
are not treated this way. Moreover,variable.__setitem__('foo', 'foo')
is interpreted properly, so this behavior is inconsistent. After this change both variants result in marking remaining part of the branch as unreachable.Test Plan
Added test case where code after indexed assignment should be reported is unreachable: