@@ -5016,12 +5016,39 @@ def fast_container_type(
50165016 self .resolved_type [e ] = ct
50175017 return ct
50185018
5019+ def infer_item_type (self , item_types : List [Type ]) -> Type :
5020+ """Infer the item type for a list based on its elements."""
5021+ joined_type = self .chk .join_types (* item_types )
5022+ proper_joined = get_proper_type (joined_type )
5023+ if (
5024+ isinstance (proper_joined , Instance )
5025+ and proper_joined .type .fullname == "builtins.object"
5026+ and len (set (map (type , item_types ))) > 1
5027+ ):
5028+ # if we can't find a common supertype other than 'object',
5029+ # use a Union of the item types
5030+ return UnionType .make_simplified_union (item_types )
5031+ else :
5032+ # otherwise just use the common supertype
5033+ return joined_type
5034+
50195035 def check_lst_expr (self , e : ListExpr | SetExpr | TupleExpr , fullname : str , tag : str ) -> Type :
50205036 # fast path
50215037 t = self .fast_container_type (e , fullname )
50225038 if t :
50235039 return t
50245040
5041+ # if a ListExpr, just infer the item type directly
5042+ if isinstance (e , ListExpr ):
5043+ item_types = [self .accept (item ) for item in e .items ]
5044+ if not item_types :
5045+ # empty list, default to Any
5046+ item_type = AnyType (TypeOfAny .from_empty_collection )
5047+ else :
5048+ # attempt to find a common supertype
5049+ item_type = self .infer_item_type (item_types )
5050+ return self .chk .named_generic_type (fullname , [item_type ])
5051+
50255052 # Translate into type checking a generic function call.
50265053 # Used for list and set expressions, as well as for tuples
50275054 # containing star expressions that don't refer to a
0 commit comments