`meet_types` gives unexpected results when meeting literal and Instance
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
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']?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the examples around mypy.meet.meet_types and mypy.solve.solve_constraints. Inspect how LiteralType and Instance.last_known_value are handled in both entry points, then verify that meeting and constraint solving preserve the expected literal result without later losing literal information.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100