Type hint for a function that returns only a specific set of values

Copperfield :

I have a function that can only return a, b or c, all of them are of type T. And I want to include this fact in the signature because of the special meaning they carry in the context of the function. How do I do that?

Currently, I use this

def fun(...) -> "a or b or c":
    #briefly explain the meaning of a, b and c in its docstring

Is that the correct one?

I know that I can do this

def fun(...) -> T:
    #briefly explain the meaning of a, b and c in its docstring

but as I said I want to express in the signature that the function only returns those specific values.

Blckknght :

You can't specify that your function returns only a subset of a type's values using type hinting alone. As the name implies, type hinting is all about types not values.

However, you can create a new enum.Enum subtype that only has the values you're going to return and use it in the function. Then you can type hint that you're returning the enum type.

import enum

class cmp_results(enum.IntEnum):
    less = -1
    equal = 0
    greater = 1

def my_cmp_function(x, y) -> cmp_results:
    if x < y: return cmp_results.less
    elif x == y: return cmp_results.equal
    else: return cmp_results.greater

This may be overkill. Just hinting int as the return type (and documenting the specific values) is probably good enough.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type hint that a function never returns

How to type hint that a function returns another function?

How to type hint a function that returns a function?

What is the type hint of a function that returns an image?

Python type hint for specific values of dictionary

How to type hint specific objects in function argument

Python - how to type hint calling a function that returns a tuple?

How do I type hint a function that returns an instance of the current class?

How to provide type hint for a function that returns an Protocol subclass in Python?

Array values set in function only work properly for one data type

execute SET only for specific values?

What is the type hint for a function

How to manipulate column entries using only one specific output of a function that returns several values?

How to document a function that returns specific values?

Function hint - possible values for parameters

Function returns json instead of only values

Function returns incorrect False (only at higher values)

Is it possible to type hint a lambda function?

Type hint function accepting a Union

How to reference the type hint of a function

Oracle: Create a function that returns a set of values?

How do I type-hint that a Python function returns instance of any class derived from a superclass?

type hint returns NameError: name 'datetime' not defined

Is there a way to make a type hint for multiple returns

TypeScript type with specific set of keys and values

Set 3 values to default values if only one of them returns 'undefined'

using pymongo,I want to retrieve only values in hint's subject from hints array if the hint type is free&active and then list the values into a list

type hint for an instance of a non specific dataclass

How to type hint specific constant string in TypeScript?