Skip to content
Advertisement

how to type a function that returns None if input is string otherwise return the input as is

I wonder what’s the right way to type annotate a python function as below?

JavaScript

This is a toy example that is analogous to a python I encountered that needs type annotated. Using Union like

JavaScript

doesn’t feel right because it doesn’t enforce the rules

  • an int input must result in an int output.
  • a float input must result in a float output.
  • a str input must result in None.

Let’s assume the input can only be one of int/float/str.

Advertisement

Answer

You can use typing.overload to narrow the types of return values based on the types of the arguments:

JavaScript

Note that the actual implementation uses a union of all the possible argument and return types.

If a call to the function conforms to one of the overload stubs, the appropriate return type is inferred:

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