Skip to content

Commit

Permalink
Allow calling a function with name '_' (#11810)
Browse files Browse the repository at this point in the history
Fixes #11774. The issue has relevant background information in the
discussion.

It may be worth it to eventually add back some restrictions, but we'd
only focus the restrictions on singledispatch functions. This is a
quick fix to work around a regression.
  • Loading branch information
JukkaL committed Dec 22, 2021
1 parent b31b06b commit 768ce5b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
7 changes: 0 additions & 7 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Expression type checker. This file is conceptually part of TypeChecker."""

from mypy.util import unnamed_function
from mypy.backports import OrderedDict, nullcontext
from contextlib import contextmanager
import itertools
Expand Down Expand Up @@ -347,12 +346,6 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
and callee_type.implicit):
self.msg.untyped_function_call(callee_type, e)

if (isinstance(callee_type, CallableType)
and not callee_type.is_type_obj()
and unnamed_function(callee_type.name)):
self.msg.underscore_function_call(e)
return AnyType(TypeOfAny.from_error)

# Figure out the full name of the callee for plugin lookup.
object_type = None
member = None
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,12 @@ reveal_type(h) # N: Revealed type is "builtins.function"
h(7) # E: Cannot call function of unknown type
[builtins fixtures/bool.pyi]

[case testFunctionWithNameUnderscore]
def _(x: int) -> None: pass

_(1)
_('x') # E: Argument 1 to "_" has incompatible type "str"; expected "int"

-- Positional-only arguments
-- -------------------------

Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-redefine.test
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ def _(arg: str):
def _(arg: int) -> int:
return 'a' # E: Incompatible return value type (got "str", expected "int")

[case testCallingUnderscoreFunctionIsNotAllowed]
[case testCallingUnderscoreFunctionIsNotAllowed-skip]
# Skipped because of https://github.com/python/mypy/issues/11774
def _(arg: str) -> None:
pass

Expand Down

0 comments on commit 768ce5b

Please sign in to comment.