-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
bugmypy got something wrongmypy got something wrong
Description
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:
from mypy.meet import meet_types
from mypy.nodes import Block, ClassDef, SymbolTable, TypeInfo
from mypy.types import Instance, LiteralType, TypeOfAny
ST = SymbolTable()
def make_typeinfo(name: str, module_name: str = "__main__") -> TypeInfo:
class_def = ClassDef(name, Block([])) # Create a dummy ClassDef
info = TypeInfo(ST, class_def, module_name)
class_def.info = info # circular reference
return info
# Create demo types
str_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) # str
print("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:
from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint
from mypy.solve import solve_constraints
from mypy.types import AnyType, TypeVarId, TypeVarType
T = 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']?
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong