Skip to content

How to annotate a function that returns a callable that should follow a generic Protocol? #1753

Answered by carljm
willofferfit asked this question in Q&A
Discussion options

You must be logged in to vote

Generics aren't capable of following all the internal logic of get_fn, they just match types in the signature. So in order to do this with generics, you would need T_contra itself to appear directly as the type (or part of the type) of an argument to get_fn. You won't be able to use generics to map from an FnOption value to the corresponding T_contra type argument.

This more general mapping from argument types to return type is exactly what overloads are for, so you could use overloads here if there were an argument of type FnOption to get_fn (and if all call-sites called get_fn with literal values of FnOption)

Unfortunately you introduce a second level of indirection by nesting the FnOption

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

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

Answer selected by willofferfit
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