Open muxator opened 1 year ago
Additional note:
calling import signature ; print(inspect.signature(g))
prints the correct answer:
(x: int) -> str
But I suspect this is done at runtime, so there is no benefit for my use case. I am curious now: what would a language such as OCaml do?
n.b.: I also tried a different library, more limited in scope (https://pypi.org/project/compose/), which behaves exactly as toolz
(mypy
does not infer the types of the composed functions, while inspect.signature()
does).
Update re: last comment: my https://pypi.org/project/compose/ now has optional type hints (available in https://pypi.org/project/compose-stubs/ )
Notes for the toolz community based on my experience type-hinting compose
:
compose
are possible, they just have to be brute-forced with an overload for each arity.
Passing any function through
compose()
seems to strip away all the information about the types of those functions.Minimal example:
Testing on python 3.11 yields:
I expected that the static type checker would have printed
builtins.str
even forg(1)
.This is a stripped down example of a larger problem I had early on on a project of mine, where I wrongly assumed that composed functions would have brought over the type information about their signature. I ended up introducing a bug in my project because of this assumption.
Is this use case something that
toolz
could ever support? I have no idea if this is due to a deep limitation in the Python language or is simply something that eventually needs to be worked on.I also read #496, but I do not know if my problem could be solved by working on that issue.
Thank you for the library.