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

Class UnionType

mypy/types.py:3306–3413  ·  view source on GitHub ↗

The union type Union[T1, ..., Tn] (at least one type argument).

Source from the content-addressed store, hash-verified

3304
3305
3306class 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

Callers 15

infer_issubclass_mapsMethod · 0.90
visit_BinOpMethod · 0.90
visit_union_typeMethod · 0.90
make_optional_typeFunction · 0.90
read_typeFunction · 0.90
visit_union_typeMethod · 0.90
assign_typeMethod · 0.90

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…