Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to inspect return type of Callable

Let’s say I have something like the following:

import inspect
from collections.abc import Callable  # Using Python 3.10+
from typing import get_type_hints

def foo(arg: Callable[..., int]) -> None:
    pass

type_hints = get_type_hints(foo)["arg"]
annotation = inspect.signature(foo).parameters["arg"].annotation
# How can one get the return type `int` from either of these?

I can figure out how to get to the Callable type hint. From there, how can one inspect the return type of the Callable?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

annotation.__args__

or

type_hints.__args__

(Ellipsis, <class 'int'>)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading