Result = Union
Type System & Annotations

Meaning

A type alias that uses a Union to indicate a value may be either a boolean success flag or an Exception object, signaling that a function can return a result or an error without raising it.

Primary Function

API design

Communicative Purpose

Express that a function may return a normal value or an error object as part of its contract

Pattern

Result = Union[first_type, second_type]

Core Structure

Result = Union[..., ...]

Função primária

API design

Propósito comunicativo

Express that a function may return a normal value or an error object as part of its contract

Situações de gatilho

When a function prefers to return error information instead of raising, when documenting APIs that return success flags or exception objects, when defining reusable result types for multiple functions

Contextos

Python codebases that use static typing (typing module), libraries that expose functional‑style APIs, services that serialize results

Padrão

Result = Union[first_type, second_type]

Estrutura central

Result = Union[..., ...]

Slots de substituição

alias_name: identifier, first_type: type, second_type: type

Colocados típicos

  • from typing import Union
  • def function(...) -> Result
  • isinstance(value
  • Exception)

Substituições comuns

  • Result = bool | Exception (PEP 604 pipe syntax)
  • Result = Union[bool
  • BaseException]

Erros comuns

Forgetting to import Union (misconception: assuming Union is built-in) -> NameError when the type checker runs; using Union with unrelated types (misconception: Union can combine any types) -> type checker may miss incompatible return values; treating the returned Exception as a regular value without handling it (misconception: the Union returns a usable value) -> runtime errors when calling methods on the Exception assuming it is a bool; conflating runtime behavior with static type hints (misconception: Union changes runtime behavior) -> assuming the function will automatically raise or unwrap the error

Similar / contraste

Using typing.Any to ignore type specifics, using custom error classes instead of generic Exception, returning None and checking for falsy values

Interferências

Coming from languages without static union types, developers may think the Union creates a runtime union type rather than a static hint; avoid assuming the return will be automatically unwrapped

Família do chunk

  • type alias
  • Union
  • type hint

Nuance

Union only affects static analysis; at runtime it does nothing. Prefer raising exceptions for error handling unless a functional style is required. In Python 3.10+ prefer the pipe syntax for brevity

Efeito pragmático

Makes function contracts explicit, helping static type checkers and readers understand possible error returns, reducing accidental misuse

Dica de memória

Result can be success or failure – a boolean or an exception

Nota

Python 3.10 introduced PEP 604 allowing "bool | Exception" as a more concise alternative to Union

Upgrade path

Result = bool | Exception # Python 3.10+ pipe syntax

Frequência: MediumFormulaicidade: Semi-fixedTipo de construção: Type alias using Union to define a result type that can hold either a boolean success flag or an Exception object.Prioridade de aquisição: Recognition firstPrioridade de output: OutputTag de espaçamento: Short-term

Log in to save chunks.