-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Mypy infers the type List[object] for x in this example:
x = [1, 'a']This is often not what the user expects and can be confusing, as there tends to be an error when the program tries to do something with the list. Often the user would prefer the type to be List[Any] or List[Union[int, str]], but generally the desired item type could be something else, such as an ABC or protocol (once protocols are supported).
It's perhaps impractical to unambiguously infer the desired type, but it would be easy enough to detect cases like these and require an explicit annotation. For the above example, mypy would require an annotation for x (and potentially suggest List[Any] or List[Union[int, str]] as the potential type), similar to how mypy deals with x = [].
For user-defined generic types object might be a reasonable value for a type parameter, so we should probably restrict this to things where the object type is likely a problem. For example, we could generate the message for the types List[object] and Dict[<anything>, object] only. It's less clear if we should do this for dictionaries withobject keys or for sets.