Restricted version of t with only True-ish values
(t: Type)
| 755 | |
| 756 | |
| 757 | def true_only(t: Type) -> ProperType: |
| 758 | """ |
| 759 | Restricted version of t with only True-ish values |
| 760 | """ |
| 761 | t = get_proper_type(t) |
| 762 | |
| 763 | if not t.can_be_true: |
| 764 | # All values of t are False-ish, so there are no true values in it |
| 765 | return UninhabitedType(line=t.line, column=t.column) |
| 766 | elif not t.can_be_false: |
| 767 | # All values of t are already True-ish, so true_only is idempotent in this case |
| 768 | return t |
| 769 | elif isinstance(t, UnionType): |
| 770 | # The true version of a union type is the union of the true versions of its components |
| 771 | new_items = [true_only(item) for item in t.items] |
| 772 | can_be_true_items = [item for item in new_items if item.can_be_true] |
| 773 | return make_simplified_union(can_be_true_items, line=t.line, column=t.column) |
| 774 | else: |
| 775 | ret_type = _get_type_method_ret_type(t, name="__bool__") or _get_type_method_ret_type( |
| 776 | t, name="__len__" |
| 777 | ) |
| 778 | |
| 779 | if ret_type and not ret_type.can_be_true: |
| 780 | return UninhabitedType(line=t.line, column=t.column) |
| 781 | |
| 782 | new_t = copy_type(t) |
| 783 | new_t.can_be_false = False |
| 784 | return new_t |
| 785 | |
| 786 | |
| 787 | def false_only(t: Type) -> ProperType: |
searching dependent graphs…