-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loss of typing information on composed functions #6
Comments
It's partially a library limitation (duplicate of #5 as far as that goes), and partially a language+analyzer limitation - Python has been slow to add type hints for combining/modifying callables (some relevant discussion in pytoolz/toolz#523 ), and then static analyzers are sometimes slow to support those few hints that we do get (as you might've seen in python/mypy#12280 ). In particular, last I checked there was still no good way provided by Python to tell static type checkers that the return type of one function is supposed to be the same as the argument type of the next function in a variadic way. So I do apologize if the "can be type-checked" in my project description caused your assumption. When I wrote that, I was just talking about type-checking at runtime (i.e. |
Thank you for your explanation, now it's more clear! I suppose I'll have to wait some more time before the python ecosystem matures enough for this type of use case. Just for information, the use case I had was a simple streaming parser for a text based file format. I wanted to return strongly typed values, I wanted to read the input file lazily, and I also wanted to separate the I/O from the pure parsing, so that the function was easily testable. The composition would have been useful because I would write something like:
instead of:
As long as I stay in python-land, I guess I'll stick to write Thank you again! |
Do you mind installing https://pypi.org/project/compose-stubs along with Just note the I've tested with MyPy, Pyre, and Pyright, and it works for (MyPy struggles when the return type of one composed function and the argument type of the next composed function, MyPy's error is the rather unhelpful "Cannot infer type of argument ..." instead of the more helpful "... incompatible type ...; expected ..."; Pyre and Pyright don't have this problem, and in particular I want to compliment Pyright for actually saying what the concrete mismatch of types is.) |
I'll try it tomorrow, thanks! |
Stubs package is now about as good as it's going to get, I think (until Python adds better type hint features). I've bumped it up from alpha to beta, and If we don't notice any serious issues in the near future I'll promote it to stable v1.0.0 soon. |
Thanks @mentalisttraceur, after installing
I only tested my simple use case of composition of two functions at a time, and works perfectly for me. At this point, in compose's README, I'd suggest to mention the usefulness of also installing compose-stubs if one wants to have non-surprising static typing. Thanks again! |
Yep! That was my plan, I was just delaying to see if we noticed any deal-breaking issues. Since we haven't, |
Passing any function through
compose.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.
This issue is a cross post of something I also found in another library (pytoolz/toolz#569).
Is this a limitation of the libraries? Of the language itself? Or the static analyzers?
Thank you.
The text was updated successfully, but these errors were encountered: