-
-
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 option to selectively disable --disallow-untyped-calls #15845
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -490,7 +490,37 @@ section of the command line docs. | |
:default: False | ||
|
||
Disallows calling functions without type annotations from functions with type | ||
annotations. | ||
annotations. Note that when used in per-module options, it enables/disables | ||
this check **inside** the module(s) specified, not for functions that come | ||
from that module(s), for example config like this: | ||
|
||
.. code-block:: ini | ||
|
||
[mypy] | ||
disallow_untyped_calls = True | ||
|
||
[mypy-some.library.*] | ||
disallow_untyped_calls = False | ||
|
||
will disable this check inside ``some.library``, not for your code that | ||
imports ``some.library``. If you want to selectively disable this check for | ||
all your code that imports ``some.library`` you should instead use | ||
:confval:`untyped_call_exception`, for example: | ||
|
||
.. code-block:: ini | ||
|
||
[mypy] | ||
disallow_untyped_calls = True | ||
untyped_call_exception = some.library | ||
|
||
.. confval:: untyped_call_exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems useful, but I'm not sure about the name of the option. Maybe we can come up with a better name? I don't any suggestions right now, but I'll think about this later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also struggled some time with this. I would propose to not spend much time on this. People who want this will probably search for "mypy untyped call" or similar, so including these words in the option name should be good enough. So if you have a better idea, then I will be happy to update, otherwise let's just move on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fwiw I'd go with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I considered at first, but then how wold we call corresponding command line option? |
||
|
||
:type: comma-separated list of strings | ||
|
||
Selectively excludes functions and methods defined in specific packages, | ||
modules, and classes from action of :confval:`disallow_untyped_calls`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention that this also applies to submodules of packages (i.e. everything inside that prefix). |
||
Note, this option does not support per-file configuration, the exception | ||
list is defined globally for all your code. | ||
|
||
.. confval:: disallow_untyped_defs | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -529,13 +529,6 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> | |
callee_type = get_proper_type( | ||
self.accept(e.callee, type_context, always_allow_any=True, is_callee=True) | ||
) | ||
if ( | ||
self.chk.options.disallow_untyped_calls | ||
and self.chk.in_checked_function() | ||
and isinstance(callee_type, CallableType) | ||
and callee_type.implicit | ||
): | ||
self.msg.untyped_function_call(callee_type, e) | ||
|
||
# Figure out the full name of the callee for plugin lookup. | ||
object_type = None | ||
|
@@ -561,6 +554,21 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> | |
): | ||
member = e.callee.name | ||
object_type = self.chk.lookup_type(e.callee.expr) | ||
|
||
if ( | ||
self.chk.options.disallow_untyped_calls | ||
and self.chk.in_checked_function() | ||
and isinstance(callee_type, CallableType) | ||
and callee_type.implicit | ||
): | ||
if fullname is None and member is not None: | ||
assert object_type is not None | ||
fullname = self.method_fullname(object_type, member) | ||
if not fullname or not any( | ||
fullname.startswith(p) for p in self.chk.options.untyped_call_exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure the exception There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
): | ||
self.msg.untyped_function_call(callee_type, e) | ||
|
||
ret_type = self.check_call_expr_with_callee_type( | ||
callee_type, e, fullname, object_type, member | ||
) | ||
|
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.
Maybe also use a real third-party package in the example, such as numpy?