-
-
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
Note that assert_type
always matches against any in untyped function
#13626
Conversation
mypy/checkexpr.py
Outdated
@@ -3724,6 +3724,10 @@ def visit_assert_type_expr(self, expr: AssertTypeExpr) -> Type: | |||
) | |||
target_type = expr.type | |||
if not is_same_type(source_type, target_type): | |||
if not self.chk.in_checked_function(): | |||
self.msg.note( | |||
"'assert_type' always outputs 'Any' in unchecked functions", expr.expr |
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 is quite right; assert_type() doesn't output anything.
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.
Ah, you mean in terms of wording? Would this be better? "assert_type(x, y)
always evaluates x
as Any
in unchecked functions"
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.
Oh, I see now.. if doesn't actually output a message in the test.
When I actually run it from my branch I saw
$ mypy func_scope.py
func_scope.py:5: error: Expression is of type "Any", not "Literal[42]"
func_scope.py:5: note: 'assert_type' always outputs 'Any' in unchecked functions
Found 1 error in 1 file (checked 1 source file)
so it had looked like my fix worked
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.
Yes, your code looks correct in the sense that the note gets output, but I don't think the wording is good. I like @hauntsaninja's suggestion below.
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.
Gotchya!
I am not sure why my test is failing. It looks like it has the same form as the other test cases, but no message is emitted: https://github.com/python/mypy/runs/8241823231?check_suite_focus=true#step:8:295
This comment has been minimized.
This comment has been minimized.
You need to make sure |
Got it! I updated the note and fixed the test according to your feedback |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@sobolevn you are right: I am beyond my depth with working with mypy's internals here, and am afraid that I am wasting mypy dev time by stumbling my way through this. I am happy to close this if it is easier for someone else to take care of this. |
@rsokl no worries, I am happy to help :) My understanding is that |
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.
LGMT, any other comments? :)
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!
Thank you, all of you, for your help! I really appreciate it |
Description
reveal_type
provides a useful note to users that it will always revealAny
within an unchecked function. Similarly,assert_type
always checks againstAny
, but does not notify the user about its behavior.This PR adds said note, which is displayed when
assert_type
raises an error in an unchecked functionTest Plan
I added a test to
test-data/unit/check-expressions.test
that is comparable to the corresponding test case forreveal_type
in an unchecked scope.