How to reference the type hint of a function

ttyridal

Is there a way in python's type hinting to say "function with the same signature as this one"?

The following works, but writing out the signature an extra time is required:

from typing import Callable

fn_sig = Callable[[int], bool]  # can I get rid of this?
def callme(a: int) -> bool:
    return a > 1

def do_something(cb: fn_sig):
    cb(1)

ie I want to write something like:

def do_something(cb: Callable[callme]):

or

def do_something(cb: callme):

But neither seems to be valid. (python 3.6.3, mypy 0.570)

Display Name

First, you can retrieve structured data about the signature of the function from __annotations__:

def callme(a: int) -> bool:
    return a > 1

print(callme.__annotations__)

# prints {'a': <class 'int'>, 'return': <class 'bool'>}

From here, you could work on a function that will convert it to the type as you want.

Update: a crude and probably not universal way to do just that:

import typing
from typing import Callable


def callme(a: int) -> bool:
    return a > 1


def get_function_type(fn):
    annotations = typing.get_type_hints(fn)
    return_type = annotations.get('return', None)
    arg_types = []
    for k, v in annotations.items():
        if k != 'return':
            arg_types.append(v)
    return Callable[arg_types, return_type]


def example(f: get_function_type(callme)):
    pass


print(get_function_type(callme))
# prints 'typing.Callable[[int], bool]'
print(get_function_type(example))
# prints 'typing.Callable[[typing.Callable[[int], bool]], NoneType]'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type hint for a reference to a generic function in TypeScript

How to type hint that a function returns another function?

How to type hint a function that returns a function?

How do I type hint a filename in a function?

How to hint the type of a function I do not control?

How to type hint a function that transforms an RDD?

How to type hint specific objects in function argument

How to type-hint "SupportsOrdering" in a function parameter?

What is the type hint for a function

What is the type hint for a class reference?

How to type hint function accepting both union and any type of that union?

How to type-hint non-basic parameters of a function?

How to type hint a function's optional return parameter?

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?

In Python 3.5, how can I specify a function as a type hint?

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

How to type hint function with a callable argument and default value

Type hint that a function never returns

Is it possible to type hint a lambda function?

Type hint function accepting a Union

How to change the type of a function reference?

How can I type-hint a function where the return type depends on the input type of an argument?

How to type hint with an optional import?

How to type-hint in Hy

Explicit type hint in method reference lambda results in raw type

The right way to type hint a Coroutine function?

What is the return type hint of a generator function?

type hint clone function in python 3.5+