The tuple type Tuple[T1, ..., Tn] (at least one type argument). Instance variables: items: Tuple item types partial_fallback: The (imprecise) underlying instance type that is used for non-tuple methods. This is generally builtins.tuple[Any, ...] for regul
| 2748 | |
| 2749 | |
| 2750 | class TupleType(ProperType): |
| 2751 | """The tuple type Tuple[T1, ..., Tn] (at least one type argument). |
| 2752 | |
| 2753 | Instance variables: |
| 2754 | items: Tuple item types |
| 2755 | partial_fallback: The (imprecise) underlying instance type that is used |
| 2756 | for non-tuple methods. This is generally builtins.tuple[Any, ...] for |
| 2757 | regular tuples, but it's different for named tuples and classes with |
| 2758 | a tuple base class. Use mypy.typeops.tuple_fallback to calculate the |
| 2759 | precise fallback type derived from item types. |
| 2760 | implicit: If True, derived from a tuple expression (t,....) instead of Tuple[t, ...] |
| 2761 | """ |
| 2762 | |
| 2763 | __slots__ = ("items", "partial_fallback", "implicit") |
| 2764 | |
| 2765 | items: list[Type] |
| 2766 | partial_fallback: Instance |
| 2767 | implicit: bool |
| 2768 | |
| 2769 | def __init__( |
| 2770 | self, |
| 2771 | items: list[Type], |
| 2772 | fallback: Instance, |
| 2773 | line: int = -1, |
| 2774 | column: int = -1, |
| 2775 | implicit: bool = False, |
| 2776 | ) -> None: |
| 2777 | super().__init__(line, column) |
| 2778 | self.partial_fallback = fallback |
| 2779 | self.items = items |
| 2780 | self.implicit = implicit |
| 2781 | |
| 2782 | def can_be_true_default(self) -> bool: |
| 2783 | if self.can_be_any_bool(): |
| 2784 | # Corner case: it is a `NamedTuple` with `__bool__` method defined. |
| 2785 | # It can be anything: both `True` and `False`. |
| 2786 | return True |
| 2787 | return self.length() > 0 |
| 2788 | |
| 2789 | def can_be_false_default(self) -> bool: |
| 2790 | if self.can_be_any_bool(): |
| 2791 | # Corner case: it is a `NamedTuple` with `__bool__` method defined. |
| 2792 | # It can be anything: both `True` and `False`. |
| 2793 | return True |
| 2794 | if self.length() == 0: |
| 2795 | return True |
| 2796 | if self.length() > 1: |
| 2797 | return False |
| 2798 | # Special case tuple[*Ts] may or may not be false. |
| 2799 | item = self.items[0] |
| 2800 | if not isinstance(item, UnpackType): |
| 2801 | return False |
| 2802 | if not isinstance(item.type, TypeVarTupleType): |
| 2803 | # Non-normalized tuple[int, ...] can be false. |
| 2804 | return True |
| 2805 | return item.type.min_len == 0 |
| 2806 | |
| 2807 | def can_be_any_bool(self) -> bool: |
no outgoing calls
searching dependent graphs…