@@ -1072,46 +1072,50 @@ def _visit_func_def(self, defn: FuncDef) -> None:
10721072 if defn .original_def :
10731073 # Override previous definition.
10741074 new_type = self .function_type (defn )
1075- if isinstance (defn .original_def , FuncDef ):
1076- # Function definition overrides function definition.
1077- old_type = self .function_type (defn .original_def )
1078- if not is_same_type (new_type , old_type ):
1079- self .msg .incompatible_conditional_function_def (defn , old_type , new_type )
1080- else :
1081- # Function definition overrides a variable initialized via assignment or a
1082- # decorated function.
1083- orig_type = defn .original_def .type
1084- if orig_type is None :
1085- # If other branch is unreachable, we don't type check it and so we might
1086- # not have a type for the original definition
1087- return
1088- if isinstance (orig_type , PartialType ):
1089- if orig_type .type is None :
1090- # Ah this is a partial type. Give it the type of the function.
1091- orig_def = defn .original_def
1092- if isinstance (orig_def , Decorator ):
1093- var = orig_def .var
1094- else :
1095- var = orig_def
1096- partial_types = self .find_partial_types (var )
1097- if partial_types is not None :
1098- var .type = new_type
1099- del partial_types [var ]
1075+ self .check_func_def_override (defn , new_type )
1076+
1077+ def check_func_def_override (self , defn : FuncDef , new_type : FunctionLike ) -> None :
1078+ assert defn .original_def is not None
1079+ if isinstance (defn .original_def , FuncDef ):
1080+ # Function definition overrides function definition.
1081+ old_type = self .function_type (defn .original_def )
1082+ if not is_same_type (new_type , old_type ):
1083+ self .msg .incompatible_conditional_function_def (defn , old_type , new_type )
1084+ else :
1085+ # Function definition overrides a variable initialized via assignment or a
1086+ # decorated function.
1087+ orig_type = defn .original_def .type
1088+ if orig_type is None :
1089+ # If other branch is unreachable, we don't type check it and so we might
1090+ # not have a type for the original definition
1091+ return
1092+ if isinstance (orig_type , PartialType ):
1093+ if orig_type .type is None :
1094+ # Ah this is a partial type. Give it the type of the function.
1095+ orig_def = defn .original_def
1096+ if isinstance (orig_def , Decorator ):
1097+ var = orig_def .var
11001098 else :
1101- # Trying to redefine something like partial empty list as function.
1102- self .fail (message_registry .INCOMPATIBLE_REDEFINITION , defn )
1099+ var = orig_def
1100+ partial_types = self .find_partial_types (var )
1101+ if partial_types is not None :
1102+ var .type = new_type
1103+ del partial_types [var ]
11031104 else :
1104- name_expr = NameExpr (defn .name )
1105- name_expr .node = defn .original_def
1106- self .binder .assign_type (name_expr , new_type , orig_type )
1107- self .check_subtype (
1108- new_type ,
1109- orig_type ,
1110- defn ,
1111- message_registry .INCOMPATIBLE_REDEFINITION ,
1112- "redefinition with type" ,
1113- "original type" ,
1114- )
1105+ # Trying to redefine something like partial empty list as function.
1106+ self .fail (message_registry .INCOMPATIBLE_REDEFINITION , defn )
1107+ else :
1108+ name_expr = NameExpr (defn .name )
1109+ name_expr .node = defn .original_def
1110+ self .binder .assign_type (name_expr , new_type , orig_type )
1111+ self .check_subtype (
1112+ new_type ,
1113+ orig_type ,
1114+ defn ,
1115+ message_registry .INCOMPATIBLE_REDEFINITION ,
1116+ "redefinition with type" ,
1117+ "original type" ,
1118+ )
11151119
11161120 def check_func_item (
11171121 self ,
@@ -5120,6 +5124,10 @@ def visit_decorator_inner(self, e: Decorator, allow_empty: bool = False) -> None
51205124 if e .type and not isinstance (get_proper_type (e .type ), (FunctionLike , AnyType )):
51215125 self .fail (message_registry .BAD_CONSTRUCTOR_TYPE , e )
51225126
5127+ if e .func .original_def :
5128+ # Function definition overrides function definition.
5129+ self .check_func_def_override (e .func , sig )
5130+
51235131 def check_for_untyped_decorator (
51245132 self , func : FuncDef , dec_type : Type , dec_expr : Expression
51255133 ) -> None :
0 commit comments