@@ -1040,6 +1040,7 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
10401040 return
10411041
10421042 max_overload_count = 50
1043+ max_expanded_count = 200
10431044 if len (defn .items ) >= max_overload_count :
10441045 # Since the following is quadratic, we limit the size of the inner loop
10451046 self .fail (message_registry .TOO_MANY_OVERLOADS , defn )
@@ -1076,63 +1077,62 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
10761077 expanded_count += len (sig_expanded )
10771078 del item
10781079
1079- if expanded_count >= 200 :
1080+ if expanded_count >= max_expanded_count :
10801081 self .fail (message_registry .TOO_MANY_OVERLOADS , defn )
10811082
10821083 is_descriptor_get = defn .info and defn .name == "__get__"
10831084 for i , sig1 in enumerate (signatures ):
10841085 if sig1 is None :
10851086 continue
10861087
1087- if expanded_count < 200 :
1088- for j , sig2 in enumerate (signatures [i + 1 :]):
1089- if j >= max_overload_count :
1090- # Avoid quadratic blowup for large numbers of overloads.
1091- break
1088+ for j , sig2 in enumerate (signatures [i + 1 :]):
1089+ if j >= max_overload_count :
1090+ # Avoid quadratic blowup for large numbers of overloads.
1091+ break
10921092
1093- if sig2 is None :
1094- continue
1093+ if sig2 is None :
1094+ continue
10951095
1096- if not are_argument_counts_overlapping (sig1 , sig2 ):
1097- continue
1096+ if not are_argument_counts_overlapping (sig1 , sig2 ):
1097+ continue
10981098
1099- if overload_can_never_match (sig1 , sig2 ):
1100- ctx_item = defn .items [i + j + 1 ]
1101- assert isinstance (ctx_item , Decorator )
1102- self .msg .overloaded_signature_will_never_match (
1103- i + 1 , i + j + 2 , ctx_item .func
1104- )
1105- elif not is_descriptor_get :
1106- # Note: we force mypy to check overload signatures in strict-optional mode
1107- # so we don't incorrectly report errors when a user tries typing an overload
1108- # that happens to have a 'if the argument is None' fallback.
1109- #
1110- # For example, the following is fine in strict-optional mode but would throw
1111- # the unsafe overlap error when strict-optional is disabled:
1112- #
1113- # @overload
1114- # def foo(x: None) -> int: ...
1115- # @overload
1116- # def foo(x: str) -> str: ...
1117- #
1118- # See Python 2's map function for a concrete example of this kind of overload.
1119- with state .strict_optional_set (True ):
1120- esig1 = expanded_signatures [i ]
1121- esig2 = expanded_signatures [i + j + 1 ]
1122- assert esig1 is not None and esig2 is not None
1123- if is_unsafe_overlapping_overload_signatures (esig1 , esig2 , type_vars ):
1124- flip_note = (
1125- j == 0
1126- and not is_unsafe_overlapping_overload_signatures (
1127- esig2 , esig1 , type_vars
1128- )
1129- and not overload_can_never_match (sig2 , sig1 )
1130- )
1131- ctx_item = defn .items [i ]
1132- assert isinstance (ctx_item , Decorator )
1133- self .msg .overloaded_signatures_overlap (
1134- i + 1 , i + j + 2 , flip_note , ctx_item .func
1099+ if overload_can_never_match (sig1 , sig2 ):
1100+ ctx_item = defn .items [i + j + 1 ]
1101+ assert isinstance (ctx_item , Decorator )
1102+ self .msg .overloaded_signature_will_never_match (
1103+ i + 1 , i + j + 2 , ctx_item .func
1104+ )
1105+ elif not is_descriptor_get and expanded_count < max_expanded_count :
1106+ # Note: we force mypy to check overload signatures in strict-optional mode
1107+ # so we don't incorrectly report errors when a user tries typing an overload
1108+ # that happens to have a 'if the argument is None' fallback.
1109+ #
1110+ # For example, the following is fine in strict-optional mode but would throw
1111+ # the unsafe overlap error when strict-optional is disabled:
1112+ #
1113+ # @overload
1114+ # def foo(x: None) -> int: ...
1115+ # @overload
1116+ # def foo(x: str) -> str: ...
1117+ #
1118+ # See Python 2's map function for a concrete example of this kind of overload.
1119+ with state .strict_optional_set (True ):
1120+ esig1 = expanded_signatures [i ]
1121+ esig2 = expanded_signatures [i + j + 1 ]
1122+ assert esig1 is not None and esig2 is not None
1123+ if is_unsafe_overlapping_overload_signatures (esig1 , esig2 , type_vars ):
1124+ flip_note = (
1125+ j == 0
1126+ and not is_unsafe_overlapping_overload_signatures (
1127+ esig2 , esig1 , type_vars
11351128 )
1129+ and not overload_can_never_match (sig2 , sig1 )
1130+ )
1131+ ctx_item = defn .items [i ]
1132+ assert isinstance (ctx_item , Decorator )
1133+ self .msg .overloaded_signatures_overlap (
1134+ i + 1 , i + j + 2 , flip_note , ctx_item .func
1135+ )
11361136
11371137 if impl_type is not None :
11381138 assert defn .impl is not None
0 commit comments