Skip to content
Discussion options

You must be logged in to vote

It's not entirely clear to me what exactly you want to achieve. I give you the broad rundown

The usage of var: TypeAlias = expression tells your type-checker to interpret the assignment as a type expression. One of the most important cases is to distinguish strings vs type expressions written as strings.

Foo = "int | str"  # interpreted as string
Bar: TypeAlias = "int | str"  # interpreted as a Union type expression

The most crucial runtime difference is that the type statement creates a TypeAliasType, while : TypeAlias is basically just like any other variable assignment.

print(Bar is "int | str")  # true
type Baz = "int | str"
print(Baz is "int | str")  # false; but equivalent 

That me…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hunterhogan
Comment options

Answer selected by JelleZijlstra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants