The union type Union[T1, ..., Tn] (at least one type argument).
| 3304 | |
| 3305 | |
| 3306 | class UnionType(ProperType): |
| 3307 | """The union type Union[T1, ..., Tn] (at least one type argument).""" |
| 3308 | |
| 3309 | __slots__ = ( |
| 3310 | "items", |
| 3311 | "is_evaluated", |
| 3312 | "uses_pep604_syntax", |
| 3313 | "original_str_expr", |
| 3314 | "original_str_fallback", |
| 3315 | ) |
| 3316 | |
| 3317 | def __init__( |
| 3318 | self, |
| 3319 | items: Sequence[Type], |
| 3320 | line: int = -1, |
| 3321 | column: int = -1, |
| 3322 | *, |
| 3323 | is_evaluated: bool = True, |
| 3324 | uses_pep604_syntax: bool = False, |
| 3325 | ) -> None: |
| 3326 | super().__init__(line, column) |
| 3327 | # We must keep this false to avoid crashes during semantic analysis. |
| 3328 | # TODO: maybe switch this to True during type-checking pass? |
| 3329 | self.items = flatten_nested_unions(items, handle_type_alias_type=False) |
| 3330 | # is_evaluated should be set to false for type comments and string literals |
| 3331 | self.is_evaluated = is_evaluated |
| 3332 | # uses_pep604_syntax is True if Union uses OR syntax (X | Y) |
| 3333 | self.uses_pep604_syntax = uses_pep604_syntax |
| 3334 | # The meaning of these two is the same as for UnboundType. A UnionType can be |
| 3335 | # return by type parser from a string "A|B", and we need to be able to fall back |
| 3336 | # to plain string, when such a string appears inside a Literal[...]. |
| 3337 | self.original_str_expr: str | None = None |
| 3338 | self.original_str_fallback: str | None = None |
| 3339 | |
| 3340 | def can_be_true_default(self) -> bool: |
| 3341 | return any(item.can_be_true for item in self.items) |
| 3342 | |
| 3343 | def can_be_false_default(self) -> bool: |
| 3344 | return any(item.can_be_false for item in self.items) |
| 3345 | |
| 3346 | def __hash__(self) -> int: |
| 3347 | return hash(frozenset(self.items)) |
| 3348 | |
| 3349 | def __eq__(self, other: object) -> bool: |
| 3350 | if not isinstance(other, UnionType): |
| 3351 | return NotImplemented |
| 3352 | if self is other: |
| 3353 | return True |
| 3354 | return frozenset(self.items) == frozenset(other.items) |
| 3355 | |
| 3356 | @overload |
| 3357 | @staticmethod |
| 3358 | def make_union( |
| 3359 | items: Sequence[ProperType], line: int = -1, column: int = -1 |
| 3360 | ) -> ProperType: ... |
| 3361 | |
| 3362 | @overload |
| 3363 | @staticmethod |
no outgoing calls
searching dependent graphs…