-
-
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
Add more precise error message for Callable annotation #12518
Add more precise error message for Callable annotation #12518
Conversation
What if we just remove the "or Callable" part? We should nudge people towards more precise types, and error messages don't need to include every detail. |
We could probably remove the "or Callable" part if a user has the |
One of my least favorite parts of typing is typing a That said, I can see an argument being made that, if you choose for the Dependent on the outcome of this discussion I will fix the flake8 error or change the error message. |
This comment has been minimized.
This comment has been minimized.
@frerikandriessen what flake8 error are you referring to? And note that I'm not proposing to remove the ability to write " |
The flake8 CI check is failing because one of the lines is slightly too long |
mypy/typeanal.py
Outdated
@@ -813,7 +813,10 @@ def analyze_callable_type(self, t: UnboundType) -> Type: | |||
return AnyType(TypeOfAny.from_error) | |||
ret = maybe_ret | |||
else: | |||
self.fail('Please use "Callable[[<parameters>], <return type>]" or "Callable"', t) | |||
if self.options.disallow_any_generics: | |||
self.fail('Please use "Callable[[<parameters>], <return type>]" or "Callable[..., Any]"', t) |
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.
self.fail('Please use "Callable[[<parameters>], <return type>]" or "Callable[..., Any]"', t) | |
self.fail('Please use "Callable[[<parameters>], <return type>]"', t) |
Let's remove the suboptimal suggestion here
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.
My 2 cents: I actually want to keep the error message with both suggestions, because the 'suboptimal' suggestion is exactly what I wanted to know. So for me it was very helpful (albeit unfortunately misleading, which is why I've corrected it). Just to emphasize that some users will get value from the not-concise message.
That said, if you still want me to remove that part I'll gladly do so.
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.
@JelleZijlstra just to double-check: I lined out above why I wanted to keep that message as is. Do you still want me to change it?
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'd still lean towards removing it. Error messages should be short and provide the most useful option, and Callable[..., Any]
generally isn't what we should steer people towards.
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.
Okay, I've incorporated your suggestion
Adds a more precise error message for a Callable annotation, if the option --disallow-any-generics has been used. The suggestion in the old message was incorrect in this scenario.
77e98b2
to
1b00107
Compare
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Description
Adds a more precise error message for a
Callable
annotation, if theoption
--disallow-any-generics
(part of--strict
mode) has been used. The suggestion in the oldmessage was incorrect in this scenario.
Fixes #11757
Test Plan
Contents of test.py:
Results: