11from collections .abc import Collection , Sequence
22from dataclasses import dataclass
3+ import sys
34
45from mypy .argmap import map_formals_to_actuals
56from mypy .checker import TypeChecker
@@ -16,16 +17,33 @@ class IterableSequenceChecker:
1617 checker : TypeChecker
1718
1819 def check_iterable_sequence_call (self , call : CallExpr ) -> None :
20+ if not self .is_builtin_function (call .callee ):
21+ self .check_iterable_sequence_arguments (call )
22+
23+ def is_builtin_function (self , expression : Expression ) -> bool :
24+ expression_type = self .checker .lookup_type (expression )
25+ if (
26+ isinstance (expression_type , CallableType )
27+ and (def_ := expression_type .definition ) is not None
28+ ):
29+ [module , * _ ] = def_ .fullname .split ("." , maxsplit = 1 ) if def_ else ["" ]
30+ return module in sys .stdlib_module_names
31+ return False
32+
33+ def check_iterable_sequence_arguments (self , call : CallExpr ) -> None :
1934 for argument , expected_type in self .actuals_formals_mapping_bijective_subset (call ):
20- argument_type = self .checker .lookup_type (argument )
21- if self .is_sequence (argument_type ) and self .is_iterable (expected_type ):
22- self ._display_error_message (expected_type , argument_type , argument )
35+ self .check_iterable_sequence_argument (argument , expected_type )
36+
37+ def check_iterable_sequence_argument (self , argument : Expression , expected_type : Type ) -> None :
38+ argument_type = self .checker .lookup_type (argument )
39+ if self .is_sequence (argument_type ) and self .is_iterable (expected_type ):
40+ self ._display_error_message (expected_type , argument_type , argument )
2341
2442 def _display_error_message (
2543 self , expected_type : Type , argument_type : Type , context : Context
2644 ) -> None :
2745 self .checker .fail (
28- f"Expected { format_type (expected_type , self .checker .options )} , got { format_type (argument_type , self .checker .options )} ." ,
46+ f"Argument has type { format_type (argument_type , self .checker .options )} ; expected { format_type (expected_type , self .checker .options )} ." ,
2947 context ,
3048 code = ITERABLE_SEQUENCE ,
3149 )
0 commit comments