MCPcopy Index your code
hub / github.com/python/mypy / true_only

Function true_only

mypy/typeops.py:757–784  ·  view source on GitHub ↗

Restricted version of t with only True-ish values

(t: Type)

Source from the content-addressed store, hash-verified

755
756
757def 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
787def false_only(t: Type) -> ProperType:

Calls 6

get_proper_typeFunction · 0.90
UninhabitedTypeClass · 0.90
copy_typeFunction · 0.90
isinstanceFunction · 0.85
make_simplified_unionFunction · 0.85

Used in the wild real call sites across dependent graphs

searching dependent graphs…