The internal mypy.meet.meet_types function gives unexpected types, when one argument is a LiteralType, and the other is a Instance whose last_known_value is the other argument:
frommypy.meetimportmeet_typesfrommypy.nodesimportBlock, ClassDef, SymbolTable, TypeInfofrommypy.typesimportInstance, LiteralType, TypeOfAnyST=SymbolTable()
defmake_typeinfo(name: str, module_name: str="__main__") ->TypeInfo:
class_def=ClassDef(name, Block([])) # Create a dummy ClassDefinfo=TypeInfo(ST, class_def, module_name)
class_def.info=info# circular referencereturninfo# Create demo typesstr_info=make_typeinfo("str", module_name="builtins")
str_type=Instance(str_info, [], last_known_value=None)
max_literal=LiteralType("max", fallback=str_type)
max_instance=Instance(str_info, [], last_known_value=max_literal)
print("str_type:", str_type) # strprint("max_literal:", max_literal) # Literal["max"]print("max_instance:", max_instance) # Literal["max"]?print(f"Meet type {meet_types(max_literal, max_instance)=}") # Literal["max"]?print(f"Meet type {meet_types(max_instance, max_literal)=}") # Literal["max"]?The predicted result is an Instance type with last_known_value=Literal["max"], but I think the result should be the LiteralType, since the former can be converted to plain str type later on, which would then be incorrect.
Similar issues are also present in the mypy.solve.solve_constraint function:
frommypy.constraintsimportSUBTYPE_OF, SUPERTYPE_OF, Constraintfrommypy.solveimportsolve_constraintsfrommypy.typesimportAnyType, TypeVarId, TypeVarTypeT=TypeVarType(
"T",
"T",
id=TypeVarId(1),
values=[],
upper_bound=AnyType(TypeOfAny.unannotated),
default=AnyType(TypeOfAny.unannotated),
)
uppers= [Constraint(T, SUBTYPE_OF, max_literal)] # T <: Literal['max']lowers= [Constraint(T, SUPERTYPE_OF, max_instance)] # T >: Literal['max']?print(f"solution={solve_constraints([T], lowers+uppers)}") # Literal['max']?
The internal
mypy.meet.meet_typesfunction gives unexpected types, when one argument is aLiteralType, and the other is aInstancewhoselast_known_valueis the other argument:The predicted result is an
Instancetype withlast_known_value=Literal["max"], but I think the result should be theLiteralType, since the former can be converted to plainstrtype later on, which would then be incorrect.Similar issues are also present in the
mypy.solve.solve_constraintfunction: