You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a working typescript version of this (with left-to-right composition). Something analogous would be possible in typed Python, but with the added bonus of decorators and operator overloading to clean up the syntax.
I tried, but I couldn't get the type inference to work at all.
Even limiting my functions to always receive a single parameter...
Either I'm too stupid or the type hinting system is just not there yet.
Epic. You fixed it. Ok, now type inference works through the whole chain.
But do you know of a way to make it so we don't lose the ability to use N-parameter functions, i.e., variadic generic types?
I'm imagining we'd need python to support something like Callable[[*X], Y] so we could then unpack X on the type hint of the return of __or__.
Another thing we lost was the documentation (__doc__, __annotations__, etc.) of all decorated functions as well as the composed functions, but I assume a workaround for that to be readily available in the stdlib with something like functools.wraps for callable classes.
Note that ParamSpec will be available from the typing module in Python 3.10. Also, one limitation here is that only the left-most function can have more than one required parameter. The second function (int_to_str in this case) must be callable with a single argument - the return value from the first one (float_and_str_to_int).
As to the function documentation, I'm sure there are many solutions out there that will allow composable to capture the decorated function info when used as a decorator, though I have never done this myself. However, what are your ideas regarding the docstring for the composed functions? For instance:
@composabledeff(int_: int) ->str:
'''Converts ints to strs'''returnstr(int_)
@composabledefg(str_: str) ->None:
'''Prints strings'''print(g)
# the composable object copies the decorated function docstring:f.__doc__# "Converts ints to strs"g.__doc__# "Prints strings"# however, what should we do with the composition?
(f|g).__doc__# ??? what should we do here?# __doc__ should *not* be "Converts ints to strs", because this is not what# it does; nor should it be "Prints strings"
Perhaps a combination of both, like what is already done here in toolz? I would suggest using toolz.functoolz.compose here, because that is where the core functionality is:
one limitation here is that only the left-most function can have more than one required parameter
The second function must be callable with a single argument
the return value from the first one.
I've been thinking about this and, though it is a bit niche, if the left function returned a tuple we could have a separate operator that also unpacked its returns so they'd work on a right function that had more than a single required parameter.
I don't feel like abusing the operator overloading more than we already do, so we could follow the stdlib and call it starcompose after their itertools.starmap (that's my excuse for not being able to figure out an intuitive operator for it).
Though, we'd have to figure out types for that, I'm sure it wouldn't be a problem for you.
And in similar fashion, we could have a doublestarcompose for kwargs that would do the same for functions that return a Mapping.
@eriknw is there any interest in having this feature (I mean the @composable decorator) supported by toolz? If so, I can write a PR for that in the next few days.
Hey, thanks for the ping and offer @ruancomelli! Sorry for my delay in seeing this (new job and email workflow; my filters have been fixed).
I think composability is theoretically interesting. And fun. I'm curious, though: how useful will it be in practice? When would you have used it?
What API do you suggest?
A new class that can be used as a decorator such as @composable?
Add __or__ to compose class?
Might there be confusion around left-to-right or right-to-left processing of functions?
What about compose_left? Should we upgrade that to a class and add __or__?
I think probably f | g should behave like a pipe and be e.g. g(f(*args, **kwargs)) regardless of whether composable, compose , or compose_left is used.
Is this syntax special enough and nice enough to add to our other classes in functoolz: curry, juxt, and excepts?
Which implementation choices are best for typing for e.g. mypy?
Yeah, I think there's enough interest in this to get it into toolz, so I say go for it!
Regarding typing: I think we'll want to add typing to toolz eventually, so it's worth considering typing-friendliness. The trend, however, is that the longer we wait to add typing to toolz (e.g., the minimum Python version we choose to support), the better the typing situation will be. We'll get there someday, I promise!
Reacted by Nei Cardoso de Oliveira Neto
Reacted by Nei Cardoso de Oliveira Neto and Ruan Comelli
Hi, @eriknw! I'm so sorry for the long delay. I guess I didn't have time to reply when I first saw it, and then I ended up completely forgetting about it 😬
I think composability is theoretically interesting. And fun. I'm curious, though: how useful will it be in practice? When would you have used it?
I don't think I have ever needed this myself, it's easy enough to write compose(f, g). Maybe it's just a nice shortcut?
The big benefit I see for using the pipe operator is that it is possible to get full type-correctness given the current Python typing environment (we still need ParamSpec though), something I don't believe we can have with the variadic compose. To elaborate a bit, we can imagine (and implement) composable.__or__ as compose2, where
This function has all the type information we need. This also means that chaining multiple calls to compose2 (or the pipe operator) will also have very nice typing:
In contrast, there is currently no way for us to annotate compose(*fs) in order for it to be this correct.
This leaves us with three ways for composing functions:
k=compose(f, g, h) # not type-correct in the general casek=compose2(f, compose2(g, h)) # type-correct, but not ergonomic - imagine 3 or 5 functions?k=f|g|h# type-correct and clean
What API do you suggest?
I propose adding a composable class to toolz with the pipe operator implemented as shown in previous comments, and making Compose inherit from it. This way, composable can be used as a decorator, and functions composed via compose benefit from it.
Might there be confusion around left-to-right or right-to-left processing of functions?
I don't think so - (f | g)(x) should be equivalent to g(f(x)). This is how piping works in general I guess.
What about compose_left? Should we upgrade that to a class and add __or__?
If we implement this the way I suggested, no further changes are required here since compose_left calls compose, which in turn returns an instance of Compose.
Is this syntax special enough and nice enough to add to our other classes in functoolz: curry, juxt, and excepts?
I have no preference here - I would personally default to not doing it. But the change would be as simple as making all of those classes derive from composable instead of from object.
Which implementation choices are best for typing for e.g. mypy?
I would guess that any alternative implementations would have the same type-safety/limitations as this one. The implementation from my previous comment (#523 (comment)) is enough to make sure we keep all type information that we need. In particular, composable[P, R] is just like a Callable[P, R] with support for the pipe operator.
Yeah, I think there's enough interest in this to get it into toolz, so I say go for it!
Awesome! I can write a PR for it this weekend. Let me know if you disagree with any of the points above.
Regarding typing: I think we'll want to add typing to toolz eventually, so it's worth considering typing-friendliness. The trend, however, is that the longer we wait to add typing to toolz (e.g., the minimum Python version we choose to support), the better the typing situation will be. We'll get there someday, I promise!
You promised!! 😆
Actually, inline type-annotations (like x: int) can be added as soon as toolz drops support for Python 3.5 🙈 Actually, PEP 484 got into Python 3.5, my bad! So we have all the syntax we need for this. But typing.ParamSpec is only available in Python 3.10. The alternative would be to add typing_extensions as a dependency to toolz. How do you feel about this?
If we don't have typing.ParamSpec or typing_extensions.ParamSpec, we can still implement composable, but we will lose type-information along the way. Probably we would have to rewrite
A thought: using | for composition has a funny interplay with the type union | added in Python 3.10.
This is probably a non-issue, but I wanted to mention it so others can give it a think-over too....
For example, let's say I use the composable decorator in #531 on a class I'm defining:
@composableclassFoo:
...
Now Foo construction can be easily composed, but it lost the type union shorthand.
Also, the overload of | on the composable wrapper class instance takes precedence over type.__or__, so even one such class will "contaminate" any type union: str | int | Foo | Bar | Qux becomes compose(Qux, Bar, Foo, typing.Union[str, int]), which doesn't work either as a callable composition or as a type union.
Normally we can just say "then don't do that", but
someone else may have defined Foo, and we might be just using their module without expecting to worry about "did the module I imported Foo from wrap it in a decorator that breaks | type union syntax?"
that "someone else" could think "oh it could be nice to make all my classes composable" and not realize it creates a land mine for type hint breakage for users.
So I see approximately three reasonable options:
just stay on course, trust the community of users to figure it out, and maybe add a warning in the docs about using @composable on classes,
use a different operator than |, one that seems safer from future native-to-Python overload on any callables (I had a user suggest g+f for compose(g, f)), or
have composable(a_callable) raise a TypeError if isinstance(a_callable, type) is true, and maybe hint in the docs that if you want a composable class constructor you can always wrap the class in a function (like how functoolz.compose wraps functoolz.Compose).
(By the way, until thinking about this exact dilemma, I dismissed the Compose/compose split as just a way to satisfy rigidly literal style rules, but now I'm thinking this | overload type union dilemma might be evidence of a deeper "true" reason to maintain a clean split between types (that happen to incidentally also be callables which construct things of that type) and function-like callables (which are instances of some type but not types in themselves).)
I had a composable decorator+wrapper whose | overload
takes precedence over type union |, so for example SomeClass | composable(str) would compose as str(SomeClass(...)) as expected;
has a general if not callable(other): return NotImplemented, and since type unions aren't callable, that forced cases like str | int | composable(float) to at least fail clearly and explicitly: TypeError: unsupported operand type(s) for |: 'types.UnionType' and 'composable'.
I had a separate @composable_constructor decorator+wrapper, which would defer to type union |. This was for the people who want to decorate a class definition so that the class is always composable.
I was going to say something in my docs like "if you want to decorate a class so that calling the class as a constructor is composable like a function, use @composable_constructor instead of @composable - that way, normal class functionality such as | for type unions still works as developers would expect".
I also made it so that if some code gave you a class wrongly/unsuitably decorated with @composable, you could just wrap it with composable_constructor, and the outermost of those two wrappers would win, even other wrappers were layered on as well - so if you ever had a class you wanted to be absolutely sure properly participated in a type union, you could hit it with composable_constructor and not even have to worry about whether or not it was wrapped with composable. [I eventually ended removing this feature, because it was getting too complicated to handle this correctly in the edge-cases. But most cases are covered by accessing .__wrapped__ or using inspect.unwrap.]
(I also had a composable_instances decorator+wrapper, for the probably-more-common case where each instance of a class was wanted to be composable, rather than the constructor call - it basically just took the result of calling the class and wrapped it with composable.)
But... in my library this kind of split made more sense because I was trying to thoroughly foresee and handle the decorator and wrapper use-cases as well as possible. That's probably beyond the scope that toolz wants to cover.
For example, my library used wrapt to provide much more thorough transparency - you could slap @composable on a function and all introspection and attribute accesses would still work through the composable wrapper, and if it was a callable object with other features being wrapped then any other operator overloads that object had would still work through it as well, and so on. I doubt toolz wants to pull wrapt in as a dependency (then again, toolz has several wrapper classes which could probably become better if they were built on top of wrapt).
(click to expand details) Vaguely relevant aside, I also sketched out how I would do curry as a "proper" (debatable) wrapt-style wrapper combined with my `composable`.
(Note that this variant doesn't support as many Python versions as toolz does, since it just assumes inspect.signature support, and positional-only argument syntax, though that's trivial to replace):
Anyway, personally, my journey when it comes to compose as an operator went like this:
"nah, I don't think it's really that worth doing, and I don't want to be the one doing it",
"wow, there are actually a lot of tricky pitfalls to this design space which I'd want to carefully get right, like composing async functions, error-handling, preserving introspectability, composing with other wrappers, Python suddenly recently using | for type unions... I'd hate to have myself or others dealing with problems caused by implementations that don't get those right, and users keep asking for this feature, so I guess I should go ahead and figure out a solution that takes care of all of that",
"having done it, and sat on it for a little while, it turns out I just don't have the passion to provide it - I'm not the guy to carry this torch for others".
So that's all I have to contribute for now on the matter.
If I was going to do it, that's how I'd do it, and there's some very long-winded design ramblings in mentalisttraceur/python-compose-operator#1 and mentalisttraceur/python-compose#1 covering almost the full thought process leading up to why I did it that way, including several false-starts and dead-ends along the way.
But how I did it might not be the best fit for toolz, and I don't know what the best solution within toolz would be, if any.
Reacted by Ruan Comelli and Nei Cardoso de Oliveira Neto
compose-operator is back! So you can get compose as | from there, and I'm hoping this enables experimentation and community experience gain which helps toolz design/implement its own version if it still wants to.
One neat thing that caused me to revive it is that just one tiny change made it magically elegantly combine with stuff like toolz.curry:
It doesn't have type hints yet - it would probably be trivial to add type hints for the non-async case, but I'm hoping we can figure out the full sync-or-async type hints. See mentalisttraceur/python-compose-operator#3 if you want to help or follow that.
I'll promote compose-operator to stable v1.0.0 within a day or so unless someone notices a serious issue (but there shouldn't be any - I gave it a lot of careful design thought last year, and the tests are fairly thorough).
Reacted by Ruan Comelli, Nei Cardoso de Oliveira Neto and Sam Frances
Activity
samfrances commentedon Sep 2, 2021
I have a working typescript version of this (with left-to-right composition). Something analogous would be possible in typed Python, but with the added bonus of decorators and operator overloading to clean up the syntax.
Here's the typescript version:
Link
cardoso-neto commentedon Sep 18, 2021
I tried, but I couldn't get the type inference to work at all.
Even limiting my functions to always receive a single parameter...
Either I'm too stupid or the type hinting system is just not there yet.
lol
ruancomelli commentedon Oct 7, 2021
@cardoso-neto you can make this work by turning
composableinto aGenerictype:cardoso-neto commentedon Oct 7, 2021
Epic. You fixed it. Ok, now type inference works through the whole chain.
But do you know of a way to make it so we don't lose the ability to use N-parameter functions, i.e., variadic generic types?
I'm imagining we'd need python to support something like
Callable[[*X], Y]so we could then unpackXon the type hint of the return of__or__.Another thing we lost was the documentation (
__doc__,__annotations__, etc.) of all decorated functions as well as the composed functions, but I assume a workaround for that to be readily available in the stdlib with something likefunctools.wrapsfor callable classes.ruancomelli commentedon Oct 8, 2021
@cardoso-neto I think that the only way to preserve the entire function type structure is to use
typing.ParamSpecs, like this:Note that
ParamSpecwill be available from thetypingmodule in Python 3.10. Also, one limitation here is that only the left-most function can have more than one required parameter. The second function (int_to_strin this case) must be callable with a single argument - the return value from the first one (float_and_str_to_int).As to the function documentation, I'm sure there are many solutions out there that will allow
composableto capture the decorated function info when used as a decorator, though I have never done this myself. However, what are your ideas regarding the docstring for the composed functions? For instance:Perhaps a combination of both, like what is already done here in toolz? I would suggest using
toolz.functoolz.composehere, because that is where the core functionality is:cardoso-neto commentedon Oct 11, 2021
I've been thinking about this and, though it is a bit niche, if the left function returned a tuple we could have a separate operator that also unpacked its returns so they'd work on a right function that had more than a single required parameter.
I don't feel like abusing the operator overloading more than we already do, so we could follow the stdlib and call it
starcomposeafter theiritertools.starmap(that's my excuse for not being able to figure out an intuitive operator for it).Rough sketch:
Or, more inline with toolz/funcy: # (snippet stolen from Suor/funcy#62)
And then
Though, we'd have to figure out types for that, I'm sure it wouldn't be a problem for you.
And in similar fashion, we could have a
doublestarcomposefor kwargs that would do the same for functions that return aMapping.ruancomelli commentedon Feb 10, 2022
@eriknw is there any interest in having this feature (I mean the
@composabledecorator) supported by toolz? If so, I can write a PR for that in the next few days.eriknw commentedon Feb 12, 2022
Hey, thanks for the ping and offer @ruancomelli! Sorry for my delay in seeing this (new job and email workflow; my filters have been fixed).
I think composability is theoretically interesting. And fun. I'm curious, though: how useful will it be in practice? When would you have used it?
What API do you suggest?
@composable?__or__tocomposeclass?compose_left? Should we upgrade that to a class and add__or__?f | gshould behave like a pipe and be e.g.g(f(*args, **kwargs))regardless of whethercomposable,compose, orcompose_leftis used.functoolz:curry,juxt, andexcepts?Yeah, I think there's enough interest in this to get it into
toolz, so I say go for it!Regarding typing: I think we'll want to add typing to
toolzeventually, so it's worth considering typing-friendliness. The trend, however, is that the longer we wait to add typing totoolz(e.g., the minimum Python version we choose to support), the better the typing situation will be. We'll get there someday, I promise!ruancomelli commentedon Apr 8, 2022
Hi, @eriknw! I'm so sorry for the long delay. I guess I didn't have time to reply when I first saw it, and then I ended up completely forgetting about it 😬
I don't think I have ever needed this myself, it's easy enough to write
compose(f, g). Maybe it's just a nice shortcut?The big benefit I see for using the pipe operator is that it is possible to get full type-correctness given the current Python typing environment (we still need
ParamSpecthough), something I don't believe we can have with the variadiccompose. To elaborate a bit, we can imagine (and implement)composable.__or__ascompose2, whereThis function has all the type information we need. This also means that chaining multiple calls to
compose2(or the pipe operator) will also have very nice typing:In contrast, there is currently no way for us to annotate
compose(*fs)in order for it to be this correct.This leaves us with three ways for composing functions:
I propose adding a
composableclass totoolzwith the pipe operator implemented as shown in previous comments, and makingComposeinherit from it. This way,composablecan be used as a decorator, and functions composed viacomposebenefit from it.I don't think so -
(f | g)(x)should be equivalent tog(f(x)). This is how piping works in general I guess.If we implement this the way I suggested, no further changes are required here since
compose_leftcallscompose, which in turn returns an instance ofCompose.I have no preference here - I would personally default to not doing it. But the change would be as simple as making all of those classes derive from
composableinstead of fromobject.I would guess that any alternative implementations would have the same type-safety/limitations as this one. The implementation from my previous comment (#523 (comment)) is enough to make sure we keep all type information that we need. In particular,
composable[P, R]is just like aCallable[P, R]with support for the pipe operator.Awesome! I can write a PR for it this weekend. Let me know if you disagree with any of the points above.
You promised!! 😆
Actually, inline type-annotations (likex: int) can be added as soon astoolzdrops support for Python 3.5 🙈Actually, PEP 484 got into Python 3.5, my bad! So we have all the syntax we need for this. But
typing.ParamSpecis only available in Python 3.10. The alternative would be to addtyping_extensionsas a dependency totoolz. How do you feel about this?If we don't have
typing.ParamSpecortyping_extensions.ParamSpec, we can still implementcomposable, but we will lose type-information along the way. Probably we would have to rewriteas
The huge downside of doing this is that we no longer know what parameters
composable.__call__accepts.ruancomelli commentedon Apr 9, 2022
I just wrote a PR for adding this feature to toolz 😁
mentalisttraceur commentedon Jun 23, 2022
A thought: using
|for composition has a funny interplay with the type union|added in Python 3.10.This is probably a non-issue, but I wanted to mention it so others can give it a think-over too....
For example, let's say I use the
composabledecorator in #531 on a class I'm defining:Now
Fooconstruction can be easily composed, but it lost the type union shorthand.Also, the overload of
|on thecomposablewrapper class instance takes precedence overtype.__or__, so even one such class will "contaminate" any type union:str | int | Foo | Bar | Quxbecomescompose(Qux, Bar, Foo, typing.Union[str, int]), which doesn't work either as a callable composition or as a type union.Normally we can just say "then don't do that", but
someone else may have defined
Foo, and we might be just using their module without expecting to worry about "did the module I importedFoofrom wrap it in a decorator that breaks|type union syntax?"that "someone else" could think "oh it could be nice to make all my classes composable" and not realize it creates a land mine for type hint breakage for users.
So I see approximately three reasonable options:
just stay on course, trust the community of users to figure it out, and maybe add a warning in the docs about using
@composableon classes,use a different operator than
|, one that seems safer from future native-to-Python overload on any callables (I had a user suggestg+fforcompose(g, f)), orhave
composable(a_callable)raise aTypeErrorifisinstance(a_callable, type)is true, and maybe hint in the docs that if you want a composable class constructor you can always wrap the class in a function (like howfunctoolz.composewrapsfunctoolz.Compose).(By the way, until thinking about this exact dilemma, I dismissed the
Compose/composesplit as just a way to satisfy rigidly literal style rules, but now I'm thinking this|overload type union dilemma might be evidence of a deeper "true" reason to maintain a clean split between types (that happen to incidentally also be callables which construct things of that type) and function-like callables (which are instances of some type but not types in themselves).)mentalisttraceur commentedon Jul 19, 2022
Continuing the above and replying to #531 (comment) :
For what it's worth, in my library where I sketched out a solution for this, here's how I solved it:
I had a
composabledecorator+wrapper whose|overload|, so for exampleSomeClass | composable(str)would compose asstr(SomeClass(...))as expected;if not callable(other): return NotImplemented, and since type unions aren't callable, that forced cases likestr | int | composable(float)to at least fail clearly and explicitly:TypeError: unsupported operand type(s) for |: 'types.UnionType' and 'composable'.I had a separate
@composable_constructordecorator+wrapper, which would defer to type union|. This was for the people who want to decorate a class definition so that the class is always composable.I was going to say something in my docs like "if you want to decorate a class so that calling the class as a constructor is composable like a function, use
@composable_constructorinstead of@composable- that way, normal class functionality such as|for type unions still works as developers would expect".I also made it so that if some code gave you a class wrongly/unsuitably decorated with[I eventually ended removing this feature, because it was getting too complicated to handle this correctly in the edge-cases. But most cases are covered by accessing@composable, you could just wrap it withcomposable_constructor, and the outermost of those two wrappers would win, even other wrappers were layered on as well - so if you ever had a class you wanted to be absolutely sure properly participated in a type union, you could hit it withcomposable_constructorand not even have to worry about whether or not it was wrapped withcomposable..__wrapped__or usinginspect.unwrap.](I also had a
composable_instancesdecorator+wrapper, for the probably-more-common case where each instance of a class was wanted to be composable, rather than the constructor call - it basically just took the result of calling the class and wrapped it withcomposable.)But... in my library this kind of split made more sense because I was trying to thoroughly foresee and handle the decorator and wrapper use-cases as well as possible. That's probably beyond the scope that
toolzwants to cover.For example, my library used
wraptto provide much more thorough transparency - you could slap@composableon a function and all introspection and attribute accesses would still work through thecomposablewrapper, and if it was a callable object with other features being wrapped then any other operator overloads that object had would still work through it as well, and so on. I doubttoolzwants to pullwraptin as a dependency (then again,toolzhas several wrapper classes which could probably become better if they were built on top ofwrapt).(click to expand details) Vaguely relevant aside, I also sketched out how I would do
curryas a "proper" (debatable)wrapt-style wrapper combined with my `composable`.(Note that this variant doesn't support as many Python versions as
toolzdoes, since it just assumesinspect.signaturesupport, and positional-only argument syntax, though that's trivial to replace):Anyway, personally, my journey when it comes to compose as an operator went like this:
asyncfunctions, error-handling, preserving introspectability, composing with other wrappers, Python suddenly recently using|for type unions... I'd hate to have myself or others dealing with problems caused by implementations that don't get those right, and users keep asking for this feature, so I guess I should go ahead and figure out a solution that takes care of all of that",So that's all I have to contribute for now on the matter.
If I was going to do it, that's how I'd do it, and there's some very long-winded design ramblings in mentalisttraceur/python-compose-operator#1 and mentalisttraceur/python-compose#1 covering almost the full thought process leading up to why I did it that way, including several false-starts and dead-ends along the way.
But how I did it might not be the best fit for
toolz, and I don't know what the best solution withintoolzwould be, if any.mentalisttraceur commentedon Oct 7, 2023
compose-operatoris back! So you can get compose as|from there, and I'm hoping this enables experimentation and community experience gain which helpstoolzdesign/implement its own version if it still wants to.One neat thing that caused me to revive it is that just one tiny change made it magically elegantly combine with stuff like
toolz.curry:It doesn't have type hints yet - it would probably be trivial to add type hints for the non-
asynccase, but I'm hoping we can figure out the full sync-or-asynctype hints. See mentalisttraceur/python-compose-operator#3 if you want to help or follow that.(If type-checking matters more than the operator syntactic sugar for you, my
composeimplementation has type hints.)I'll promote
compose-operatorto stablev1.0.0within a day or so unless someone notices a serious issue (but there shouldn't be any - I gave it a lot of careful design thought last year, and the tests are fairly thorough).simlei commentedon Jan 5, 2025
Just passing by here. Is this already in releases? I could contribute type hints if necessary