Skip to content

Do not simplify Union at runtime? #536

@ilevkivskyi

Description

@ilevkivskyi

Currently unions are simplified at runtime, for example:

class B: ...
class C(B): ...
assert Union[C, B] is B

Also they are flattened, for example Union[A, Union[B, C]] evaluates to Union[A, B, C]. There are however some downsides:

  • Simplification of unions is a concept related to static types, applying it at runtime might blur the distinction between static types and runtime classes.
  • Implementation of this feature had bugs, all of them related to the fact that issublcass is only applicable to proper classes, and it is not always easy to detect those beforehand.
  • This has some (although minor) runtime costs.

On the positive side:

  • This still teaches people how unions work with subclasses
  • It may be too late to change this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions