@@ -162,8 +162,8 @@ def is_subseteq_Generic(self, other: "Generic") -> bool:
162162 def is_subseteq_TypeVar (self , other : "TypeVar" ) -> bool :
163163 return self .is_subseteq (other .bound )
164164
165- def is_subseteq_Const (self , other : "Const " ) -> bool :
166- return self .is_subseteq (other .typ )
165+ def is_subseteq_Const (self , other : "Hinted " ) -> bool :
166+ return self .is_subseteq (other .type )
167167
168168 def __hash__ (self ) -> int :
169169 return hash ((PyClass , self .typ ))
@@ -349,7 +349,7 @@ def __init__(
349349 self .body = body
350350 else :
351351 self .body = PyClass (body )
352- self .vars , self .vararg = split_type_args (vars )
352+ self .vars , self .vararg = _split_type_args (vars )
353353
354354 def is_subseteq_Literal (self , other : Literal ) -> bool :
355355 return False
@@ -408,7 +408,7 @@ def where(self, typ: TypeVarValue | tuple[TypeVarValue, ...]) -> "Generic":
408408 else :
409409 typs = (typ ,)
410410
411- args , vararg = split_type_args (typs )
411+ args , vararg = _split_type_args (typs )
412412 if self .vararg is None and vararg is None :
413413 assert len (args ) <= len (
414414 self .vars
@@ -446,45 +446,52 @@ def where(self, typ: TypeVarValue | tuple[TypeVarValue, ...]) -> "Generic":
446446 raise TypeError ("Type arguments do not match" )
447447
448448
449- ConstType = typing .TypeVar ("ConstType " )
449+ HintedData = typing .TypeVar ("HintedData " )
450450
451451
452452@typing .final
453453@dataclass
454- class Const (TypeAttribute , typing .Generic [ConstType ]):
455- name = "Const"
456- data : ConstType
457- typ : TypeAttribute
454+ class Hinted (TypeAttribute , typing .Generic [HintedData ]):
455+ """Type wrapped with a hint.
456+
457+ `Hinted` is used to represent a type with additional data that can be used as
458+ a hint for type inference. The additional data is only used for specific type
459+ inference purposes, or improve certain type inference precision, it does not affect the
460+ order of types in the lattice.
461+ """
458462
459- def __init__ (self , data : ConstType , typ : TypeAttribute | None = None ):
463+ name = "Hinted"
464+ type : TypeAttribute
465+ data : HintedData
466+
467+ def __init__ (self , type : TypeAttribute , data : HintedData ):
460468 self .data = data
461- if isinstance (typ , Const ):
462- typ = widen_const (typ )
463- elif typ is None :
464- typ = PyClass (type (data ))
465- self .typ = typ
469+ if isinstance (type , Hinted ):
470+ type = type .type
471+ self .type = type
466472
467473 def is_equal (self , other : TypeAttribute ) -> bool :
468474 return (
469- isinstance (other , Const )
475+ isinstance (other , Hinted )
470476 and self .data == other .data
471- and self .typ .is_equal (other .typ )
477+ and self .type .is_equal (other .type )
472478 )
473479
474480 def is_subseteq_fallback (self , other : TypeAttribute ) -> bool :
475- return self .typ .is_subseteq (other )
481+ return self .type .is_subseteq (other )
476482
477483 def __hash__ (self ) -> int :
478- return hash (self .typ )
484+ return hash (self .type )
479485
480486 def print_impl (self , printer : Printer ) -> None :
481487 printer .print_name (self , prefix = "!" )
482- printer .plain_print ("(" , self .data , ", " )
483- printer .print (self .typ )
488+ printer .plain_print ("(" )
489+ printer .print (self .type )
490+ printer .plain_print (", " , self .data )
484491 printer .plain_print (")" )
485492
486493
487- def split_type_args (
494+ def _split_type_args (
488495 args : tuple [TypeVarValue , ...]
489496) -> tuple [tuple [TypeAttribute , ...], Vararg | None ]:
490497 if args is None or len (args ) == 0 :
@@ -539,10 +546,10 @@ def hint2type(hint) -> TypeAttribute:
539546 return Generic (body , * params )
540547
541548
542- def widen_const ( typ : TypeAttribute ) -> TypeAttribute :
543- if isinstance (typ , Const ):
544- return typ . typ
545- return typ
549+ def unwrap_hinted ( hint : TypeAttribute ) -> TypeAttribute :
550+ if isinstance (hint , Hinted ):
551+ return hint . type
552+ return hint
546553
547554
548555Any = AnyType ()
0 commit comments