@@ -192,7 +192,7 @@ def may_be_immortal(self) -> bool:
192
192
def serialize (self ) -> str :
193
193
return "void"
194
194
195
- def __eq__ (self , other : object ) -> bool :
195
+ def __eq__ (self , other : object ) -> TypeGuard [ RVoid ] :
196
196
return isinstance (other , RVoid )
197
197
198
198
def __hash__ (self ) -> int :
@@ -279,7 +279,7 @@ def serialize(self) -> str:
279
279
def __repr__ (self ) -> str :
280
280
return "<RPrimitive %s>" % self .name
281
281
282
- def __eq__ (self , other : object ) -> bool :
282
+ def __eq__ (self , other : object ) -> TypeGuard [ RPrimitive ] :
283
283
return isinstance (other , RPrimitive ) and other .name == self .name
284
284
285
285
def __hash__ (self ) -> int :
@@ -513,15 +513,15 @@ def __hash__(self) -> int:
513
513
range_rprimitive : Final = RPrimitive ("builtins.range" , is_unboxed = False , is_refcounted = True )
514
514
515
515
516
- def is_tagged (rtype : RType ) -> bool :
516
+ def is_tagged (rtype : RType ) -> TypeGuard [ RPrimitive ] :
517
517
return rtype is int_rprimitive or rtype is short_int_rprimitive
518
518
519
519
520
- def is_int_rprimitive (rtype : RType ) -> bool :
520
+ def is_int_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
521
521
return rtype is int_rprimitive
522
522
523
523
524
- def is_short_int_rprimitive (rtype : RType ) -> bool :
524
+ def is_short_int_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
525
525
return rtype is short_int_rprimitive
526
526
527
527
@@ -535,7 +535,7 @@ def is_int32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
535
535
)
536
536
537
537
538
- def is_int64_rprimitive (rtype : RType ) -> bool :
538
+ def is_int64_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
539
539
return rtype is int64_rprimitive or (
540
540
rtype is c_pyssize_t_rprimitive and rtype ._ctype == "int64_t"
541
541
)
@@ -554,81 +554,93 @@ def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
554
554
return rtype is uint8_rprimitive
555
555
556
556
557
- def is_uint32_rprimitive (rtype : RType ) -> bool :
557
+ def is_uint32_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
558
558
return rtype is uint32_rprimitive
559
559
560
560
561
- def is_uint64_rprimitive (rtype : RType ) -> bool :
561
+ def is_uint64_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
562
562
return rtype is uint64_rprimitive
563
563
564
564
565
- def is_c_py_ssize_t_rprimitive (rtype : RType ) -> bool :
565
+ def is_c_py_ssize_t_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
566
566
return rtype is c_pyssize_t_rprimitive
567
567
568
568
569
- def is_pointer_rprimitive (rtype : RType ) -> bool :
569
+ def is_pointer_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
570
570
return rtype is pointer_rprimitive
571
571
572
572
573
- def is_float_rprimitive (rtype : RType ) -> bool :
573
+ def is_float_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
574
574
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.float"
575
575
576
576
577
- def is_bool_rprimitive (rtype : RType ) -> bool :
577
+ def is_bool_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
578
578
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.bool"
579
579
580
580
581
- def is_bit_rprimitive (rtype : RType ) -> bool :
581
+ def is_bit_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
582
582
return isinstance (rtype , RPrimitive ) and rtype .name == "bit"
583
583
584
584
585
- def is_bool_or_bit_rprimitive (rtype : RType ) -> bool :
585
+ def is_bool_or_bit_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
586
586
return is_bool_rprimitive (rtype ) or is_bit_rprimitive (rtype )
587
587
588
588
589
- def is_object_rprimitive (rtype : RType ) -> bool :
589
+ def is_object_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
590
590
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.object"
591
591
592
592
593
- def is_none_rprimitive (rtype : RType ) -> bool :
593
+ def is_none_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
594
594
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.None"
595
595
596
596
597
- def is_list_rprimitive (rtype : RType ) -> bool :
597
+ def is_list_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
598
598
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.list"
599
599
600
600
601
- def is_dict_rprimitive (rtype : RType ) -> bool :
601
+ def is_dict_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
602
602
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.dict"
603
603
604
604
605
- def is_set_rprimitive (rtype : RType ) -> bool :
605
+ def is_set_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
606
606
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.set"
607
607
608
608
609
- def is_frozenset_rprimitive (rtype : RType ) -> bool :
609
+ def is_frozenset_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
610
610
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.frozenset"
611
611
612
612
613
- def is_str_rprimitive (rtype : RType ) -> bool :
613
+ def is_str_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
614
614
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.str"
615
615
616
616
617
- def is_bytes_rprimitive (rtype : RType ) -> bool :
617
+ def is_bytes_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
618
618
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.bytes"
619
619
620
620
621
- def is_tuple_rprimitive (rtype : RType ) -> bool :
621
+ def is_tuple_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
622
622
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.tuple"
623
623
624
624
625
- def is_range_rprimitive (rtype : RType ) -> bool :
625
+ def is_range_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
626
626
return isinstance (rtype , RPrimitive ) and rtype .name == "builtins.range"
627
627
628
628
629
- def is_sequence_rprimitive (rtype : RType ) -> bool :
629
+ def is_sequence_rprimitive (rtype : RType ) -> TypeGuard [ RPrimitive ] :
630
630
return isinstance (rtype , RPrimitive ) and (
631
- is_list_rprimitive (rtype ) or is_tuple_rprimitive (rtype ) or is_str_rprimitive (rtype )
631
+ is_list_rprimitive (rtype )
632
+ or is_tuple_rprimitive (rtype )
633
+ or is_str_rprimitive (rtype )
634
+ or is_bytes_rprimitive (rtype )
635
+ )
636
+
637
+
638
+ def is_immutable_rprimitive (rtype : RType ) -> TypeGuard [RPrimitive ]:
639
+ return (
640
+ is_str_rprimitive (rtype )
641
+ or is_bytes_rprimitive (rtype )
642
+ or is_tuple_rprimitive (rtype )
643
+ or is_frozenset_rprimitive (rtype )
632
644
)
633
645
634
646
@@ -717,7 +729,7 @@ def __str__(self) -> str:
717
729
def __repr__ (self ) -> str :
718
730
return "<RTuple %s>" % ", " .join (repr (typ ) for typ in self .types )
719
731
720
- def __eq__ (self , other : object ) -> bool :
732
+ def __eq__ (self , other : object ) -> TypeGuard [ RTuple ] :
721
733
return isinstance (other , RTuple ) and self .types == other .types
722
734
723
735
def __hash__ (self ) -> int :
@@ -850,7 +862,7 @@ def __repr__(self) -> str:
850
862
", " .join (name + ":" + repr (typ ) for name , typ in zip (self .names , self .types )),
851
863
)
852
864
853
- def __eq__ (self , other : object ) -> bool :
865
+ def __eq__ (self , other : object ) -> TypeGuard [ RStruct ] :
854
866
return (
855
867
isinstance (other , RStruct )
856
868
and self .name == other .name
@@ -920,7 +932,7 @@ def attr_type(self, name: str) -> RType:
920
932
def __repr__ (self ) -> str :
921
933
return "<RInstance %s>" % self .name
922
934
923
- def __eq__ (self , other : object ) -> bool :
935
+ def __eq__ (self , other : object ) -> TypeGuard [ RInstance ] :
924
936
return isinstance (other , RInstance ) and other .name == self .name
925
937
926
938
def __hash__ (self ) -> int :
@@ -974,7 +986,7 @@ def __str__(self) -> str:
974
986
return "union[%s]" % ", " .join (str (item ) for item in self .items )
975
987
976
988
# We compare based on the set because order in a union doesn't matter
977
- def __eq__ (self , other : object ) -> bool :
989
+ def __eq__ (self , other : object ) -> TypeGuard [ RUnion ] :
978
990
return isinstance (other , RUnion ) and self .items_set == other .items_set
979
991
980
992
def __hash__ (self ) -> int :
@@ -1016,7 +1028,7 @@ def optional_value_type(rtype: RType) -> RType | None:
1016
1028
return None
1017
1029
1018
1030
1019
- def is_optional_type (rtype : RType ) -> bool :
1031
+ def is_optional_type (rtype : RType ) -> TypeGuard [ RUnion ] :
1020
1032
"""Is rtype an optional type with exactly two union items?"""
1021
1033
return optional_value_type (rtype ) is not None
1022
1034
@@ -1048,7 +1060,7 @@ def __str__(self) -> str:
1048
1060
def __repr__ (self ) -> str :
1049
1061
return f"<RArray { self .item_type !r} [{ self .length } ]>"
1050
1062
1051
- def __eq__ (self , other : object ) -> bool :
1063
+ def __eq__ (self , other : object ) -> TypeGuard [ RArray ] :
1052
1064
return (
1053
1065
isinstance (other , RArray )
1054
1066
and self .item_type == other .item_type
0 commit comments