@@ -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 :
@@ -520,15 +520,15 @@ def __hash__(self) -> int:
520520range_rprimitive : Final = RPrimitive ("builtins.range" , is_unboxed = False , is_refcounted = True )
521521
522522
523- def is_tagged (rtype : RType ) -> bool :
523+ def is_tagged (rtype : RType ) -> TypeGuard [ RPrimitive ] :
524524 return rtype is int_rprimitive or rtype is short_int_rprimitive
525525
526526
527- def is_int_rprimitive (rtype : RType ) -> bool :
527+ def is_int_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
528528 return rtype is int_rprimitive
529529
530530
531- def is_short_int_rprimitive (rtype : RType ) -> bool :
531+ def is_short_int_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
532532 return rtype is short_int_rprimitive
533533
534534
@@ -542,7 +542,7 @@ def is_int32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
542542 )
543543
544544
545- def is_int64_rprimitive (rtype : RType ) -> bool :
545+ def is_int64_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
546546 return rtype is int64_rprimitive or (
547547 rtype is c_pyssize_t_rprimitive and rtype ._ctype == "int64_t"
548548 )
@@ -561,88 +561,100 @@ def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
561561 return rtype is uint8_rprimitive
562562
563563
564- def is_uint32_rprimitive (rtype : RType ) -> bool :
564+ def is_uint32_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
565565 return rtype is uint32_rprimitive
566566
567567
568- def is_uint64_rprimitive (rtype : RType ) -> bool :
568+ def is_uint64_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
569569 return rtype is uint64_rprimitive
570570
571571
572- def is_c_py_ssize_t_rprimitive (rtype : RType ) -> bool :
572+ def is_c_py_ssize_t_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
573573 return rtype is c_pyssize_t_rprimitive
574574
575575
576- def is_pointer_rprimitive (rtype : RType ) -> bool :
576+ def is_pointer_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
577577 return rtype is pointer_rprimitive
578578
579579
580- def is_float_rprimitive (rtype : RType ) -> bool :
580+ def is_float_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
581581 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.float"
582582
583583
584- def is_bool_rprimitive (rtype : RType ) -> bool :
584+ def is_bool_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
585585 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.bool"
586586
587587
588- def is_bit_rprimitive (rtype : RType ) -> bool :
588+ def is_bit_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
589589 return isinstance (rtype , RPrimitive ) and rtype .name == "bit"
590590
591591
592- def is_bool_or_bit_rprimitive (rtype : RType ) -> bool :
592+ def is_bool_or_bit_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
593593 return is_bool_rprimitive (rtype ) or is_bit_rprimitive (rtype )
594594
595595
596- def is_object_rprimitive (rtype : RType ) -> bool :
596+ def is_object_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
597597 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.object"
598598
599599
600- def is_none_rprimitive (rtype : RType ) -> bool :
600+ def is_none_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
601601 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.None"
602602
603603
604- def is_list_rprimitive (rtype : RType ) -> bool :
604+ def is_list_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
605605 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.list"
606606
607607
608- def is_dict_rprimitive (rtype : RType ) -> bool :
608+ def is_dict_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
609609 return isinstance (rtype , RPrimitive ) and rtype .name in (
610610 "builtins.dict" ,
611611 "builtins.dict[exact]" ,
612612 )
613613
614614
615- def is_exact_dict_rprimitive (rtype : RType ) -> bool :
615+ def is_exact_dict_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
616616 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.dict[exact]"
617617
618618
619- def is_set_rprimitive (rtype : RType ) -> bool :
619+ def is_set_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
620620 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.set"
621621
622622
623- def is_frozenset_rprimitive (rtype : RType ) -> bool :
623+ def is_frozenset_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
624624 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.frozenset"
625625
626626
627- def is_str_rprimitive (rtype : RType ) -> bool :
627+ def is_str_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
628628 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.str"
629629
630630
631- def is_bytes_rprimitive (rtype : RType ) -> bool :
631+ def is_bytes_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
632632 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.bytes"
633633
634634
635- def is_tuple_rprimitive (rtype : RType ) -> bool :
635+ def is_tuple_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
636636 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.tuple"
637637
638638
639- def is_range_rprimitive (rtype : RType ) -> bool :
639+ def is_range_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
640640 return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.range"
641641
642642
643- def is_sequence_rprimitive (rtype : RType ) -> bool :
643+ def is_sequence_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
644644 return isinstance (rtype , RPrimitive ) and (
645- is_list_rprimitive (rtype ) or is_tuple_rprimitive (rtype ) or is_str_rprimitive (rtype )
645+ is_list_rprimitive (rtype )
646+ or is_tuple_rprimitive (rtype )
647+ or is_str_rprimitive (rtype )
648+ or is_bytes_rprimitive (rtype )
649+ )
650+
651+
652+ def is_immutable_rprimitive (rtype : RType ) -> TypeGuard [RPrimitive ]:
653+ return (
654+ is_str_rprimitive (rtype )
655+ or is_bytes_rprimitive (rtype )
656+ or is_tuple_rprimitive (rtype )
657+ or is_frozenset_rprimitive (rtype )
646658 )
647659
648660
@@ -731,7 +743,7 @@ def __str__(self) -> str:
731743 def __repr__ (self ) -> str :
732744 return "<RTuple %s>" % ", " .join (repr (typ ) for typ in self .types )
733745
734- def __eq__ (self , other : object ) -> bool :
746+ def __eq__ (self , other : object ) -> TypeGuard [ RTuple ] :
735747 return isinstance (other , RTuple ) and self .types == other .types
736748
737749 def __hash__ (self ) -> int :
@@ -864,7 +876,7 @@ def __repr__(self) -> str:
864876 ", " .join (name + ":" + repr (typ ) for name , typ in zip (self .names , self .types )),
865877 )
866878
867- def __eq__ (self , other : object ) -> bool :
879+ def __eq__ (self , other : object ) -> TypeGuard [ RStruct ] :
868880 return (
869881 isinstance (other , RStruct )
870882 and self .name == other .name
@@ -934,7 +946,7 @@ def attr_type(self, name: str) -> RType:
934946 def __repr__ (self ) -> str :
935947 return "<RInstance %s>" % self .name
936948
937- def __eq__ (self , other : object ) -> bool :
949+ def __eq__ (self , other : object ) -> TypeGuard [ RInstance ] :
938950 return isinstance (other , RInstance ) and other .name == self .name
939951
940952 def __hash__ (self ) -> int :
@@ -988,7 +1000,7 @@ def __str__(self) -> str:
9881000 return "union[%s]" % ", " .join (str (item ) for item in self .items )
9891001
9901002 # We compare based on the set because order in a union doesn't matter
991- def __eq__ (self , other : object ) -> bool :
1003+ def __eq__ (self , other : object ) -> TypeGuard [ RUnion ] :
9921004 return isinstance (other , RUnion ) and self .items_set == other .items_set
9931005
9941006 def __hash__ (self ) -> int :
@@ -1030,7 +1042,7 @@ def optional_value_type(rtype: RType) -> RType | None:
10301042 return None
10311043
10321044
1033- def is_optional_type (rtype : RType ) -> bool :
1045+ def is_optional_type (rtype : RType ) -> TypeGuard [ RUnion ] :
10341046 """Is rtype an optional type with exactly two union items?"""
10351047 return optional_value_type (rtype ) is not None
10361048
@@ -1062,7 +1074,7 @@ def __str__(self) -> str:
10621074 def __repr__ (self ) -> str :
10631075 return f"<RArray { self .item_type !r} [{ self .length } ]>"
10641076
1065- def __eq__ (self , other : object ) -> bool :
1077+ def __eq__ (self , other : object ) -> TypeGuard [ RArray ] :
10661078 return (
10671079 isinstance (other , RArray )
10681080 and self .item_type == other .item_type
0 commit comments