From 15212876b375d7709c59d02390d565ddf33cf69a Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 21 Dec 2021 12:40:38 +0000 Subject: [PATCH] Allow calling a function with name '_' (#11810) 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. --- mypy/checkexpr.py | 7 ------- test-data/unit/check-functions.test | 6 ++++++ test-data/unit/check-redefine.test | 3 ++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 619d9f22e250..b3129018faf3 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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 @@ -337,12 +336,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 diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index b00aad55b204..5287d6197623 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -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 -- ------------------------- diff --git a/test-data/unit/check-redefine.test b/test-data/unit/check-redefine.test index d2229dba470f..85208df0932a 100644 --- a/test-data/unit/check-redefine.test +++ b/test-data/unit/check-redefine.test @@ -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