-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Note that assert_type always matches against any in untyped function
#13626
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
Conversation
mypy/checkexpr.py
Outdated
| 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. 🤖🎉 |
sobolevn
left a comment
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? :)
hauntsaninja
left a comment
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_typeprovides a useful note to users that it will always revealAnywithin an unchecked function. Similarly,assert_typealways checks againstAny, but does not notify the user about its behavior.This PR adds said note, which is displayed when
assert_typeraises an error in an unchecked functionTest Plan
I added a test to
test-data/unit/check-expressions.testthat is comparable to the corresponding test case forreveal_typein an unchecked scope.