This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dmontague
Recipients dmontague, levkivskyi
Date 2019-08-23.05:44:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1566539041.61.0.905400107157.issue37923@roundup.psfhosted.org>
In-reply-to
Content
I am trying to obtain the output of `inspect.signature`, except with string-valued annotations converted to resolved type hints, similarly to `typing.get_type_hints`.

Is there a good way to do this currently? If not, might this be a good fit for the standard library?

---------------

The straightforward way I see to attempt this would be to call both `typing.get_type_hints` and `inspect.signature` on the callable, and then "merge" the results. However, as far as I can tell, that only works if the provided callable is a function or method (i.e., not a type or a callable class instance).

As an example, if I have an instance of a class with a `__call__` method, and the class was defined in a module with `from __future__ import annotations`, then calling `inspect.signature` on the instances will only return an `inspect.Signature` with type-unaware strings as the annotations for each `inspect.Parameter`. On the other hand, calling `get_type_hints` on the instance will return type hints for the class, rather than for its `__call__` method.

I wouldn't mind manually handling an edge case or two, but the logic used by `inspect.signature` to determine which function will be called seems relatively involved, and so I would ideally like to leverage this same logic while obtaining the type-resolved version signature for the callable.
History
Date User Action Args
2019-08-23 05:44:01dmontaguesetrecipients: + dmontague, levkivskyi
2019-08-23 05:44:01dmontaguesetmessageid: <1566539041.61.0.905400107157.issue37923@roundup.psfhosted.org>
2019-08-23 05:44:01dmontaguelinkissue37923 messages
2019-08-23 05:44:01dmontaguecreate