Restricted version of t with only False-ish values
(t: Type)
| 785 | |
| 786 | |
| 787 | def false_only(t: Type) -> ProperType: |
| 788 | """ |
| 789 | Restricted version of t with only False-ish values |
| 790 | """ |
| 791 | t = get_proper_type(t) |
| 792 | |
| 793 | if not t.can_be_false: |
| 794 | if state.strict_optional: |
| 795 | # All values of t are True-ish, so there are no false values in it |
| 796 | return UninhabitedType(line=t.line) |
| 797 | else: |
| 798 | # When strict optional checking is disabled, everything can be |
| 799 | # False-ish since anything can be None |
| 800 | return NoneType(line=t.line) |
| 801 | elif not t.can_be_true: |
| 802 | # All values of t are already False-ish, so false_only is idempotent in this case |
| 803 | return t |
| 804 | elif isinstance(t, UnionType): |
| 805 | # The false version of a union type is the union of the false versions of its components |
| 806 | new_items = [false_only(item) for item in t.items] |
| 807 | can_be_false_items = [item for item in new_items if item.can_be_false] |
| 808 | return make_simplified_union(can_be_false_items, line=t.line, column=t.column) |
| 809 | elif isinstance(t, Instance) and t.type.fullname in ("builtins.str", "builtins.bytes"): |
| 810 | return LiteralType("", fallback=t) |
| 811 | elif isinstance(t, Instance) and t.type.fullname == "builtins.int": |
| 812 | return LiteralType(0, fallback=t) |
| 813 | else: |
| 814 | ret_type = _get_type_method_ret_type(t, name="__bool__") or _get_type_method_ret_type( |
| 815 | t, name="__len__" |
| 816 | ) |
| 817 | |
| 818 | if ret_type: |
| 819 | if not ret_type.can_be_false: |
| 820 | return UninhabitedType(line=t.line) |
| 821 | elif isinstance(t, Instance): |
| 822 | if (t.type.is_final or t.type.is_enum) and state.strict_optional: |
| 823 | return UninhabitedType(line=t.line) |
| 824 | elif isinstance(t, LiteralType) and t.is_enum_literal() and state.strict_optional: |
| 825 | return UninhabitedType(line=t.line) |
| 826 | |
| 827 | new_t = copy_type(t) |
| 828 | new_t.can_be_true = False |
| 829 | return new_t |
| 830 | |
| 831 | |
| 832 | def true_or_false(t: Type) -> ProperType: |
searching dependent graphs…