@@ -192,7 +192,7 @@ def may_be_immortal(self) -> bool:
192192 def serialize (self ) -> str :
193193 return "void"
194194
195- def __eq__ (self , other : object ) -> bool :
195+ def __eq__ (self , other : object ) -> TypeGuard [ RVoid ] :
196196 return isinstance (other , RVoid )
197197
198198 def __hash__ (self ) -> int :
@@ -279,7 +279,7 @@ def serialize(self) -> str:
279279 def __repr__ (self ) -> str :
280280 return "<RPrimitive %s>" % self .name
281281
282- def __eq__ (self , other : object ) -> bool :
282+ def __eq__ (self , other : object ) -> TypeGuard [ RPrimitive ] :
283283 return isinstance (other , RPrimitive ) and other .name == self .name
284284
285285 def __hash__ (self ) -> int :
@@ -518,15 +518,15 @@ def __hash__(self) -> int:
518518)
519519
520520
521- def is_tagged (rtype : RType ) -> bool :
521+ def is_tagged (rtype : RType ) -> TypeGuard [ RPrimitive ] :
522522 return rtype is int_rprimitive or rtype is short_int_rprimitive
523523
524524
525- def is_int_rprimitive (rtype : RType ) -> bool :
525+ def is_int_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
526526 return rtype is int_rprimitive
527527
528528
529- def is_short_int_rprimitive (rtype : RType ) -> bool :
529+ def is_short_int_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
530530 return rtype is short_int_rprimitive
531531
532532
@@ -540,7 +540,7 @@ def is_int32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
540540 )
541541
542542
543- def is_int64_rprimitive (rtype : RType ) -> bool :
543+ def is_int64_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
544544 return rtype is int64_rprimitive or (
545545 rtype is c_pyssize_t_rprimitive and rtype ._ctype == "int64_t"
546546 )
@@ -559,81 +559,93 @@ def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
559559 return rtype is uint8_rprimitive
560560
561561
562- def is_uint32_rprimitive (rtype : RType ) -> bool :
562+ def is_uint32_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
563563 return rtype is uint32_rprimitive
564564
565565
566- def is_uint64_rprimitive (rtype : RType ) -> bool :
566+ def is_uint64_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
567567 return rtype is uint64_rprimitive
568568
569569
570- def is_c_py_ssize_t_rprimitive (rtype : RType ) -> bool :
570+ def is_c_py_ssize_t_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
571571 return rtype is c_pyssize_t_rprimitive
572572
573573
574- def is_pointer_rprimitive (rtype : RType ) -> bool :
574+ def is_pointer_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
575575 return rtype is pointer_rprimitive
576576
577577
578- def is_float_rprimitive (rtype : RType ) -> bool :
578+ def is_float_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
579579 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.float"
580580
581581
582- def is_bool_rprimitive (rtype : RType ) -> bool :
582+ def is_bool_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
583583 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.bool"
584584
585585
586- def is_bit_rprimitive (rtype : RType ) -> bool :
586+ def is_bit_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
587587 return isinstance (rtype , RPrimitive ) and rtype .name == "bit"
588588
589589
590- def is_bool_or_bit_rprimitive (rtype : RType ) -> bool :
590+ def is_bool_or_bit_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
591591 return is_bool_rprimitive (rtype ) or is_bit_rprimitive (rtype )
592592
593593
594- def is_object_rprimitive (rtype : RType ) -> bool :
594+ def is_object_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
595595 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.object"
596596
597597
598- def is_none_rprimitive (rtype : RType ) -> bool :
598+ def is_none_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
599599 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.None"
600600
601601
602- def is_list_rprimitive (rtype : RType ) -> bool :
602+ def is_list_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
603603 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.list"
604604
605605
606- def is_dict_rprimitive (rtype : RType ) -> bool :
606+ def is_dict_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
607607 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.dict"
608608
609609
610- def is_set_rprimitive (rtype : RType ) -> bool :
610+ def is_set_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
611611 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.set"
612612
613613
614- def is_frozenset_rprimitive (rtype : RType ) -> bool :
614+ def is_frozenset_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
615615 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.frozenset"
616616
617617
618- def is_str_rprimitive (rtype : RType ) -> bool :
618+ def is_str_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
619619 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.str"
620620
621621
622- def is_bytes_rprimitive (rtype : RType ) -> bool :
622+ def is_bytes_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
623623 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.bytes"
624624
625625
626- def is_tuple_rprimitive (rtype : RType ) -> bool :
626+ def is_tuple_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
627627 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.tuple"
628628
629629
630- def is_range_rprimitive (rtype : RType ) -> bool :
630+ def is_range_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
631631 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.range"
632632
633633
634- def is_sequence_rprimitive (rtype : RType ) -> bool :
634+ def is_sequence_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
635635 return isinstance (rtype , RPrimitive ) and (
636- is_list_rprimitive (rtype ) or is_tuple_rprimitive (rtype ) or is_str_rprimitive (rtype )
636+ is_list_rprimitive (rtype )
637+ or is_tuple_rprimitive (rtype )
638+ or is_str_rprimitive (rtype )
639+ or is_bytes_rprimitive (rtype )
640+ )
641+
642+
643+ def is_immutable_rprimitive (rtype : RType ) -> TypeGuard [RPrimitive ]:
644+ return (
645+ is_str_rprimitive (rtype )
646+ or is_bytes_rprimitive (rtype )
647+ or is_tuple_rprimitive (rtype )
648+ or is_frozenset_rprimitive (rtype )
637649 )
638650
639651
@@ -726,7 +738,7 @@ def __str__(self) -> str:
726738 def __repr__ (self ) -> str :
727739 return "<RTuple %s>" % ", " .join (repr (typ ) for typ in self .types )
728740
729- def __eq__ (self , other : object ) -> bool :
741+ def __eq__ (self , other : object ) -> TypeGuard [ RTuple ] :
730742 return isinstance (other , RTuple ) and self .types == other .types
731743
732744 def __hash__ (self ) -> int :
@@ -859,7 +871,7 @@ def __repr__(self) -> str:
859871 ", " .join (name + ":" + repr (typ ) for name , typ in zip (self .names , self .types )),
860872 )
861873
862- def __eq__ (self , other : object ) -> bool :
874+ def __eq__ (self , other : object ) -> TypeGuard [ RStruct ] :
863875 return (
864876 isinstance (other , RStruct )
865877 and self .name == other .name
@@ -929,7 +941,7 @@ def attr_type(self, name: str) -> RType:
929941 def __repr__ (self ) -> str :
930942 return "<RInstance %s>" % self .name
931943
932- def __eq__ (self , other : object ) -> bool :
944+ def __eq__ (self , other : object ) -> TypeGuard [ RInstance ] :
933945 return isinstance (other , RInstance ) and other .name == self .name
934946
935947 def __hash__ (self ) -> int :
@@ -983,7 +995,7 @@ def __str__(self) -> str:
983995 return "union[%s]" % ", " .join (str (item ) for item in self .items )
984996
985997 # We compare based on the set because order in a union doesn't matter
986- def __eq__ (self , other : object ) -> bool :
998+ def __eq__ (self , other : object ) -> TypeGuard [ RUnion ] :
987999 return isinstance (other , RUnion ) and self .items_set == other .items_set
9881000
9891001 def __hash__ (self ) -> int :
@@ -1025,7 +1037,7 @@ def optional_value_type(rtype: RType) -> RType | None:
10251037 return None
10261038
10271039
1028- def is_optional_type (rtype : RType ) -> bool :
1040+ def is_optional_type (rtype : RType ) -> TypeGuard [ RUnion ] :
10291041 """Is rtype an optional type with exactly two union items?"""
10301042 return optional_value_type (rtype ) is not None
10311043
@@ -1057,7 +1069,7 @@ def __str__(self) -> str:
10571069 def __repr__ (self ) -> str :
10581070 return f"<RArray { self .item_type !r} [{ self .length } ]>"
10591071
1060- def __eq__ (self , other : object ) -> bool :
1072+ def __eq__ (self , other : object ) -> TypeGuard [ RArray ] :
10611073 return (
10621074 isinstance (other , RArray )
10631075 and self .item_type == other .item_type
0 commit comments