Skip to content

Commit eb974e0

Browse files
committed
Allow redefinition of underscore functions
Allow functions called '_' to be redefined, instead of showing an error as we would if a function with any other name was redefined.
1 parent 3eef162 commit eb974e0

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
333333
isinstance(callee_type, CallableType)
334334
and callee_type.implicit):
335335
self.msg.untyped_function_call(callee_type, e)
336+
337+
if isinstance(callee_type, CallableType) and callee_type.name == "_":
338+
self.msg.underscore_function_call(e)
339+
return AnyType(TypeOfAny.from_error)
340+
336341
# Figure out the full name of the callee for plugin lookup.
337342
object_type = None
338343
member = None

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
455455
elif len(current_overload) > 1:
456456
ret.append(OverloadedFuncDef(current_overload))
457457

458-
if isinstance(stmt, Decorator):
458+
if isinstance(stmt, Decorator) and stmt.name != "_":
459459
current_overload = [stmt]
460460
current_overload_name = stmt.name
461461
else:

mypy/messages.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ def does_not_return_value(self, callee_type: Optional[Type], context: Context) -
701701
else:
702702
self.fail('Function does not return a value', context, code=codes.FUNC_RETURNS_VALUE)
703703

704+
def underscore_function_call(self, context: Context) -> None:
705+
self.fail('Calling function named "_" is not allowed', context)
706+
704707
def deleted_as_rvalue(self, typ: DeletedType, context: Context) -> None:
705708
"""Report an error about using an deleted type as an rvalue."""
706709
if typ.source is None:

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ def f(): ... # Error: 'f' redefined
666666
"""
667667
if isinstance(new, Decorator):
668668
new = new.func
669+
if isinstance(previous, (FuncDef, Decorator)) and new.name == previous.name == "_":
670+
return True
669671
if isinstance(previous, (FuncDef, Var, Decorator)) and new.is_conditional:
670672
new.original_def = previous
671673
return True

0 commit comments

Comments
 (0)