Skip to content

Settle on Best Practice for Union / Optional Type Annotations #619

@tzaffi

Description

@tzaffi

Problem

PEP 604 allows the pipe symbol (|) to annotate unions of types. In particular:

X | Y $\equiv$ Union[X,Y]

and

X | None $\equiv$ Optional[X]

However, type forwarding does not work with this new capability. I.e. "Xtype" | Y doesn't work (though "Xtype | Y" does actually work).

Currently our repo is inconsistent when it comes to union types.

Solution

We should settle on a best practice for such annotations, change all usages to adhere to the best practice, and add it to our style guide.

I propose that we disallow | because of its incompatibility with forwarded types. But I'm open to discussion and don't have a strong opinion, except that we should have some convention.

Dependencies

None

Urgency

Very low

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions