`meet_types` gives unexpected results when meeting literal and Instance
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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']?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo gli esempi relativi a mypy.meet.meet_types e mypy.solve.solve_constraints. Esamina come vengono gestiti LiteralType e Instance.last_known_value in entrambi i punti di ingresso, quindi verifica che il meeting e la risoluzione dei vincoli preservino il risultato literal previsto senza perdere in seguito le informazioni literal.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100