Skip to content

Accessing the type of an argument at a specific position in a callback #1628

Answered by Daverball
guacs asked this question in Q&A
Discussion options

You must be logged in to vote

It's also worth noting that Callable is too broad of a Protocol for typing.get_type_hints, since it also accepts lambdas (which don't have annotations) and any object that implements __call__ including type, which is also a valid argument for get_type_hints, but at that point you would be inspecting the attributes of the class and not the parameters of the __init__/__new__ method.

So you would need to use inspect.signature anyways to properly handle all the different kinds of callables with reasonable certainty, that you didn't miss some kind of corner case. There's some internal functions in the typing module that can give you an idea how to deal with things like Annotated and forward re…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@guacs
Comment options

Answer selected by guacs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants