Skip to content

Commit 27b9ba0

Browse files
chore: homogenize TypeGuard usage in rtypes.py (#19655)
1 parent c95d8ab commit 27b9ba0

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

mypyc/ir/rtypes.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
@@ -513,15 +513,15 @@ def __hash__(self) -> int:
513513
range_rprimitive: Final = RPrimitive("builtins.range", is_unboxed=False, is_refcounted=True)
514514

515515

516-
def is_tagged(rtype: RType) -> bool:
516+
def is_tagged(rtype: RType) -> TypeGuard[RPrimitive]:
517517
return rtype is int_rprimitive or rtype is short_int_rprimitive
518518

519519

520-
def is_int_rprimitive(rtype: RType) -> bool:
520+
def is_int_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
521521
return rtype is int_rprimitive
522522

523523

524-
def is_short_int_rprimitive(rtype: RType) -> bool:
524+
def is_short_int_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
525525
return rtype is short_int_rprimitive
526526

527527

@@ -535,7 +535,7 @@ def is_int32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
535535
)
536536

537537

538-
def is_int64_rprimitive(rtype: RType) -> bool:
538+
def is_int64_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
539539
return rtype is int64_rprimitive or (
540540
rtype is c_pyssize_t_rprimitive and rtype._ctype == "int64_t"
541541
)
@@ -554,79 +554,79 @@ def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
554554
return rtype is uint8_rprimitive
555555

556556

557-
def is_uint32_rprimitive(rtype: RType) -> bool:
557+
def is_uint32_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
558558
return rtype is uint32_rprimitive
559559

560560

561-
def is_uint64_rprimitive(rtype: RType) -> bool:
561+
def is_uint64_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
562562
return rtype is uint64_rprimitive
563563

564564

565-
def is_c_py_ssize_t_rprimitive(rtype: RType) -> bool:
565+
def is_c_py_ssize_t_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
566566
return rtype is c_pyssize_t_rprimitive
567567

568568

569-
def is_pointer_rprimitive(rtype: RType) -> bool:
569+
def is_pointer_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
570570
return rtype is pointer_rprimitive
571571

572572

573-
def is_float_rprimitive(rtype: RType) -> bool:
573+
def is_float_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
574574
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.float"
575575

576576

577-
def is_bool_rprimitive(rtype: RType) -> bool:
577+
def is_bool_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
578578
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.bool"
579579

580580

581-
def is_bit_rprimitive(rtype: RType) -> bool:
581+
def is_bit_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
582582
return isinstance(rtype, RPrimitive) and rtype.name == "bit"
583583

584584

585-
def is_bool_or_bit_rprimitive(rtype: RType) -> bool:
585+
def is_bool_or_bit_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
586586
return is_bool_rprimitive(rtype) or is_bit_rprimitive(rtype)
587587

588588

589-
def is_object_rprimitive(rtype: RType) -> bool:
589+
def is_object_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
590590
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.object"
591591

592592

593-
def is_none_rprimitive(rtype: RType) -> bool:
593+
def is_none_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
594594
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.None"
595595

596596

597-
def is_list_rprimitive(rtype: RType) -> bool:
597+
def is_list_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
598598
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.list"
599599

600600

601-
def is_dict_rprimitive(rtype: RType) -> bool:
601+
def is_dict_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
602602
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.dict"
603603

604604

605-
def is_set_rprimitive(rtype: RType) -> bool:
605+
def is_set_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
606606
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.set"
607607

608608

609-
def is_frozenset_rprimitive(rtype: RType) -> bool:
609+
def is_frozenset_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
610610
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.frozenset"
611611

612612

613-
def is_str_rprimitive(rtype: RType) -> bool:
613+
def is_str_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
614614
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.str"
615615

616616

617-
def is_bytes_rprimitive(rtype: RType) -> bool:
617+
def is_bytes_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
618618
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.bytes"
619619

620620

621-
def is_tuple_rprimitive(rtype: RType) -> bool:
621+
def is_tuple_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
622622
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.tuple"
623623

624624

625-
def is_range_rprimitive(rtype: RType) -> bool:
625+
def is_range_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
626626
return isinstance(rtype, RPrimitive) and rtype.name == "builtins.range"
627627

628628

629-
def is_sequence_rprimitive(rtype: RType) -> bool:
629+
def is_sequence_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]:
630630
return isinstance(rtype, RPrimitive) and (
631631
is_list_rprimitive(rtype)
632632
or is_tuple_rprimitive(rtype)
@@ -729,7 +729,7 @@ def __str__(self) -> str:
729729
def __repr__(self) -> str:
730730
return "<RTuple %s>" % ", ".join(repr(typ) for typ in self.types)
731731

732-
def __eq__(self, other: object) -> bool:
732+
def __eq__(self, other: object) -> TypeGuard[RTuple]:
733733
return isinstance(other, RTuple) and self.types == other.types
734734

735735
def __hash__(self) -> int:
@@ -862,7 +862,7 @@ def __repr__(self) -> str:
862862
", ".join(name + ":" + repr(typ) for name, typ in zip(self.names, self.types)),
863863
)
864864

865-
def __eq__(self, other: object) -> bool:
865+
def __eq__(self, other: object) -> TypeGuard[RStruct]:
866866
return (
867867
isinstance(other, RStruct)
868868
and self.name == other.name
@@ -932,7 +932,7 @@ def attr_type(self, name: str) -> RType:
932932
def __repr__(self) -> str:
933933
return "<RInstance %s>" % self.name
934934

935-
def __eq__(self, other: object) -> bool:
935+
def __eq__(self, other: object) -> TypeGuard[RInstance]:
936936
return isinstance(other, RInstance) and other.name == self.name
937937

938938
def __hash__(self) -> int:
@@ -986,7 +986,7 @@ def __str__(self) -> str:
986986
return "union[%s]" % ", ".join(str(item) for item in self.items)
987987

988988
# We compare based on the set because order in a union doesn't matter
989-
def __eq__(self, other: object) -> bool:
989+
def __eq__(self, other: object) -> TypeGuard[RUnion]:
990990
return isinstance(other, RUnion) and self.items_set == other.items_set
991991

992992
def __hash__(self) -> int:
@@ -1028,7 +1028,7 @@ def optional_value_type(rtype: RType) -> RType | None:
10281028
return None
10291029

10301030

1031-
def is_optional_type(rtype: RType) -> bool:
1031+
def is_optional_type(rtype: RType) -> TypeGuard[RUnion]:
10321032
"""Is rtype an optional type with exactly two union items?"""
10331033
return optional_value_type(rtype) is not None
10341034

@@ -1060,7 +1060,7 @@ def __str__(self) -> str:
10601060
def __repr__(self) -> str:
10611061
return f"<RArray {self.item_type!r}[{self.length}]>"
10621062

1063-
def __eq__(self, other: object) -> bool:
1063+
def __eq__(self, other: object) -> TypeGuard[RArray]:
10641064
return (
10651065
isinstance(other, RArray)
10661066
and self.item_type == other.item_type

0 commit comments

Comments
 (0)