Skip to content
Advertisement

TypeScript vs. Python typing default arguments and autocompletion

In TypeScript I used to have the following and it will work as type inference:

JavaScript

The above example should give you options and autocompletion when typing, and error when you type options that are not in the list of options.

When I was exploring Python I found the typing module. But what I tried didn’t work, and I couldn’t find what I’ve hopped to find.

My expected code:

JavaScript

My expected result should be similar to the one in TypeScript.

Advertisement

Answer

JavaScript

All valid, except the last line makes mypy rightfully complain:

error: Argument 1 to "func" has incompatible type "Literal['#4']"; expected "Literal['#1', '#2', '#3']"  [arg-type]

Note that in Python, the interpreter does not care about the type annotations and will execute that last function call without a problem.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement