@@ -1072,46 +1072,7 @@ def _visit_func_def(self, defn: FuncDef) -> None:
1072
1072
if defn .original_def :
1073
1073
# Override previous definition.
1074
1074
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 ]
1100
- else :
1101
- # Trying to redefine something like partial empty list as function.
1102
- self .fail (message_registry .INCOMPATIBLE_REDEFINITION , defn )
1103
- 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
- )
1075
+ self .check_func_def_override (defn , new_type )
1115
1076
1116
1077
def check_func_item (
1117
1078
self ,
@@ -1147,6 +1108,49 @@ def check_func_item(
1147
1108
if dataclasses_plugin .is_processed_dataclass (defn .info ):
1148
1109
dataclasses_plugin .check_post_init (self , defn , defn .info )
1149
1110
1111
+ def check_func_def_override (self , defn : FuncDef , new_type : FunctionLike ) -> None :
1112
+ assert defn .original_def is not None
1113
+ if isinstance (defn .original_def , FuncDef ):
1114
+ # Function definition overrides function definition.
1115
+ old_type = self .function_type (defn .original_def )
1116
+ if not is_same_type (new_type , old_type ):
1117
+ self .msg .incompatible_conditional_function_def (defn , old_type , new_type )
1118
+ else :
1119
+ # Function definition overrides a variable initialized via assignment or a
1120
+ # decorated function.
1121
+ orig_type = defn .original_def .type
1122
+ if orig_type is None :
1123
+ # If other branch is unreachable, we don't type check it and so we might
1124
+ # not have a type for the original definition
1125
+ return
1126
+ if isinstance (orig_type , PartialType ):
1127
+ if orig_type .type is None :
1128
+ # Ah this is a partial type. Give it the type of the function.
1129
+ orig_def = defn .original_def
1130
+ if isinstance (orig_def , Decorator ):
1131
+ var = orig_def .var
1132
+ else :
1133
+ var = orig_def
1134
+ partial_types = self .find_partial_types (var )
1135
+ if partial_types is not None :
1136
+ var .type = new_type
1137
+ del partial_types [var ]
1138
+ else :
1139
+ # Trying to redefine something like partial empty list as function.
1140
+ self .fail (message_registry .INCOMPATIBLE_REDEFINITION , defn )
1141
+ else :
1142
+ name_expr = NameExpr (defn .name )
1143
+ name_expr .node = defn .original_def
1144
+ self .binder .assign_type (name_expr , new_type , orig_type )
1145
+ self .check_subtype (
1146
+ new_type ,
1147
+ orig_type ,
1148
+ defn ,
1149
+ message_registry .INCOMPATIBLE_REDEFINITION ,
1150
+ "redefinition with type" ,
1151
+ "original type" ,
1152
+ )
1153
+
1150
1154
@contextmanager
1151
1155
def enter_attribute_inference_context (self ) -> Iterator [None ]:
1152
1156
old_types = self .inferred_attribute_types
@@ -5120,6 +5124,10 @@ def visit_decorator_inner(self, e: Decorator, allow_empty: bool = False) -> None
5120
5124
if e .type and not isinstance (get_proper_type (e .type ), (FunctionLike , AnyType )):
5121
5125
self .fail (message_registry .BAD_CONSTRUCTOR_TYPE , e )
5122
5126
5127
+ if e .func .original_def and isinstance (sig , FunctionLike ):
5128
+ # Function definition overrides function definition.
5129
+ self .check_func_def_override (e .func , sig )
5130
+
5123
5131
def check_for_untyped_decorator (
5124
5132
self , func : FuncDef , dec_type : Type , dec_expr : Expression
5125
5133
) -> None :
0 commit comments