@@ -285,6 +285,25 @@ def check_frozenset_items_valid_literals(self, op: LoadLiteral, s: frozenset[obj
285285 else :
286286 self .fail (op , f"Invalid type for item of frozenset literal: { type (x )} )" )
287287
288+ def check_dict_items_valid_literals (self , op : LoadLiteral , d : dict [object , object ]) -> None :
289+ key_types = (str , bytes , bool , int , float , complex , tuple )
290+ value_types = (str , bytes , bool , int , float , complex , tuple , dict , frozenset )
291+ for k , v in d .items ():
292+ # Acceptable key types: str, bytes, bool, int, float, complex, tuple, None
293+ if k is not None and not isinstance (k , key_types ):
294+ self .fail (op , f"Invalid type for key of dict literal: { type (k )} )" )
295+ if isinstance (k , tuple ):
296+ self .check_tuple_items_valid_literals (op , k )
297+ # Acceptable value types: str, bytes, bool, int, float, complex, tuple, dict, frozenset, None
298+ if v is not None and not isinstance (v , value_types ):
299+ self .fail (op , f"Invalid type for value of dict literal: { type (v )} )" )
300+ if isinstance (v , tuple ):
301+ self .check_tuple_items_valid_literals (op , v )
302+ elif isinstance (v , dict ):
303+ self .check_dict_items_valid_literals (op , v )
304+ elif isinstance (v , frozenset ):
305+ self .check_frozenset_items_valid_literals (op , v )
306+
288307 def visit_load_literal (self , op : LoadLiteral ) -> None :
289308 expected_type = None
290309 if op .value is None :
@@ -309,6 +328,9 @@ def visit_load_literal(self, op: LoadLiteral) -> None:
309328 # it's a set (when it's really a frozenset).
310329 expected_type = "builtins.set"
311330 self .check_frozenset_items_valid_literals (op , op .value )
331+ elif isinstance (op .value , dict ):
332+ expected_type = "builtins.dict"
333+ self .check_dict_items_valid_literals (op , op .value )
312334
313335 assert expected_type is not None , "Missed a case for LoadLiteral check"
314336
0 commit comments