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
The type hints described in PEP-484 provide a convenient way to reason about code, and make it possible for static type checkers like mypy to catch type-mismatch errors.
Here is an example of how static type checking might be able to fit in with the toolz library:
Suppose f and g are typed functions defined as follows:
def f(x: A) -> B:
...
def g(y: B) -> C:
...
Since f is Callable[[A], B] and g is Callable[[B], C], one would expect the composite toolz.compose(g, f) to be Callable[[A], C]. (Here I am using the Callable protocol from the typing module).
Of course, implementing this could be tricky.
Reacted by Charles-Axel Dein, Florian Magin, Skurikhin Alexandr, Dan Oneață, Changsheng Wu, Edgar Ramírez Mondragón, Byeonghoon Yoo, Robert Shecter, Christophe Benz, fakuivan and 16 more
Yeah, this trend is becoming a standard. I'm okay with adding static typing as long as it doesn't hinder readability too much. Hopefully it can be done in a way to improve readability.
Alternatively, this could be put in the source files directly, with minimal readability impact (IMO, it improves readability). As this uses python 3.5+, and types are supported in python 3.5+, this would not even break compatibility.
(mypy could be used to ensure all types are consistent within this lib as well, as a side effect).
I'd prefer to put annotations in the source files directly. Easier to maintain. It's clear that annotating functions has gained a lot of traction, so it's probably time to jump on the bandwagon.
I note that toolz supports Python 3.5+. Would a dependency on typing_extensions be acceptable given that there are no other dependencies at the moment? This would expand how expressive we can make the typing.
Also, there is the question of the right type checker to use. It seems that mypy actually doesn't support a lot of the more advanced PEPs that we might want for functional programming: Concatenate, Unpack etc
@multimeric Pyright seems popular enough, and tends to support new features sooner than Mypy. If you wanted to enable type checking in CI, Pyright would probably be fine. It's unfortunate that Mypy doesn't support the hot new features yet, but targeting one type checker is better than targeting none.
I think a dependency on typing-extensions is totally reasonable given that it's an "official" package.
Reacted by Michael, Vladimir and Guillaume Pelletier
I have opened a PR #552 adding a pyright run to the CI.
It is important to note that even though we might use pyright to type-check toolz,
any type hints added to toolz will also be used by whatever type-checker dependent projects use.
So type hints should be written according to the rules of the standard library, not to whatever makes pyright pass.
It's tricky though, because I started writing some type hints for toolz, and found that I needed to rely pretty heavily on new typing features like Unpack which mypy doesn't support (python/mypy#12280). So while these type hints would be correct according to the PEP specifications, I'm not sure mypy projects would see them as valid. What should be done in this case?
I guess the simplest option would be to incrementally add type hints, and only use typing features once we are confident the major type checkers all support them.
Or if we know that a typing feature is not supported, but a type checker ignores it, rather than failing, we might go ahead and use it.
I quickly ran into another question, do we add type hints that either (1) support ALL possible ways to call a function from toolz, or (2) just the 'best practice' or 'intended' ways?
(2) Or the following (more specific) type hints for the function declare the "intended" way to call the function (predicate can't be None and must return a bool). I would say that this way is better because it encourages callers to call functions as intended.
I would choose option (2) if writing toolz from scratch.
But adding type hints like that may not be backwards-compatible with existing type-checked code that uses this function.
@eriknw is there an update on thoughts on + possible plans for bringing typing into toolz?
If I'm allowed to make a short pitch for that:
There is a clear trend both within the Python community and software development in general to move towards typing (this is also mentioned in the conversation above)
Python still lacks a high-quality, reasonably well-used, external library for functional programming that includes typing support [1]
A first step could be to bring some typing support to a few widely-used functions like curry, compose and pipe. Perhaps not necessarily covering all use cases for these, but a good part of them.
[1]: dry-python/returns would probably be the closest in that regard, but with its container-based approach it has a slightly different direction than toolz, also, its toolkit is far less comprehensive than toolz's and much less stable/reliable.
Reacted by Greg Werbin, OlegAlexander, Ryan McGinty, buster-blue, Daniel De Lucca Fonseca, Ilya Kuleshov, Tomasz Sokolowski, Dario Hett, beskep and Guillaume Pelletier
Would y'all be open to a PR that starts with the easy stuff instead of the hard stuff?
itertoolz, for the most part, should be extremely easy to add type annotations to - nearly all functions would be some form of (seq: Sequence[T]) -> T or (seq: Sequence[T]) -> Sequence[T]. That, plus a py.typedfile in the root, would give our team what we need to start using at least theitertoolz` subset of functionality without writing a lot of untyped code.
returns has been mentioned, and it covers our typed compose/curry/pipe needs quite well, but I'm not aware of a library that does what itertoolz does and has static types. And typing compose and friends is not quite as simple - you need a lot of overload-type stuff usually as far as I understand it.
Reacted by Greg Werbin, carlwr, Ilan, kiaradlf, Michael Uloth and VDawg
If anyone is interested I just made a repo for the stubs I made for a personal project.
They are for cytoolz but I exclusively used the toolz official doc to make them, so they should be good for both.
Hey all, I just published toolz-stubs(on pypi) to provide type hinting. It works for both toolz and tlz but could definitely use some more work - particularly around curried operators and some of the more bespoke functions like diff, join, memoize, and a few others.
Hopefully this helps folks that are looking for type hinting in this amazing module!
Reacted by Edgar Ramírez Mondragón and Greg Werbin
Activity
eriknw commentedon Sep 21, 2020
Yeah, this trend is becoming a standard. I'm okay with adding static typing as long as it doesn't hinder readability too much. Hopefully it can be done in a way to improve readability.
Thanks for the suggestion @Jasha10! Are you willing to get this started? I found somebody created some typing information for
cytoolz, which may be a good starting point: https://github.com/blindspot-ai/cytoolz-stubsJiehong commentedon Oct 7, 2020
Very good idea!
Using stub files like cytoolz-stubs would not hinder readability much, but it would duplicate all definitions though (https://mypy.readthedocs.io/en/stable/stubs.html#stub-files).
Alternatively, this could be put in the source files directly, with minimal readability impact (IMO, it improves readability). As this uses python 3.5+, and types are supported in python 3.5+, this would not even break compatibility.
(mypy could be used to ensure all types are consistent within this lib as well, as a side effect).
Is there any preferred approach?
eriknw commentedon Oct 7, 2020
I'd prefer to put annotations in the source files directly. Easier to maintain. It's clear that annotating functions has gained a lot of traction, so it's probably time to jump on the bandwagon.
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
feature: add type hints for recipes (pytoolz#496)
multimeric commentedon Jul 3, 2022
I note that toolz supports Python 3.5+. Would a dependency on typing_extensions be acceptable given that there are no other dependencies at the moment? This would expand how expressive we can make the typing.
multimeric commentedon Jul 7, 2022
Also, there is the question of the right type checker to use. It seems that mypy actually doesn't support a lot of the more advanced PEPs that we might want for functional programming:
Concatenate,Unpacketcgwerbin commentedon Aug 29, 2022
@multimeric Pyright seems popular enough, and tends to support new features sooner than Mypy. If you wanted to enable type checking in CI, Pyright would probably be fine. It's unfortunate that Mypy doesn't support the hot new features yet, but targeting one type checker is better than targeting none.
I think a dependency on
typing-extensionsis totally reasonable given that it's an "official" package.LincolnPuzey commentedon Sep 23, 2022
I have opened a PR #552 adding a pyright run to the CI.
It is important to note that even though we might use pyright to type-check
toolz,any type hints added to
toolzwill also be used by whatever type-checker dependent projects use.So type hints should be written according to the rules of the standard library, not to whatever makes pyright pass.
multimeric commentedon Sep 23, 2022
It's tricky though, because I started writing some type hints for
toolz, and found that I needed to rely pretty heavily on new typing features likeUnpackwhich mypy doesn't support (python/mypy#12280). So while these type hints would be correct according to the PEP specifications, I'm not sure mypy projects would see them as valid. What should be done in this case?LincolnPuzey commentedon Sep 23, 2022
I guess the simplest option would be to incrementally add type hints, and only use typing features once we are confident the major type checkers all support them.
Or if we know that a typing feature is not supported, but a type checker ignores it, rather than failing, we might go ahead and use it.
LincolnPuzey commentedon Sep 23, 2022
I quickly ran into another question, do we add type hints that either (1) support ALL possible ways to call a function from
toolz, or (2) just the 'best practice' or 'intended' ways?For example, the
removefunction:could be typed in two different ways;
(1) The following type hints for the function specify ALL the ways
removecan currently be called without error(2) Or the following (more specific) type hints for the function declare the "intended" way to call the function (
predicatecan't beNoneand must return abool). I would say that this way is better because it encourages callers to call functions as intended.I would choose option (2) if writing
toolzfrom scratch.But adding type hints like that may not be backwards-compatible with existing type-checked code that uses this function.
@eriknw Can I get your opinion on this?
sid-kap commentedon Oct 7, 2022
Are there any plans to add an empty
py.typedfile in toolz, so that projects using toolz can use the type hints? (See https://mypy.readthedocs.io/en/stable/installed_packages.html)carlwr commentedon Nov 9, 2022
@eriknw is there an update on thoughts on + possible plans for bringing typing into toolz?
If I'm allowed to make a short pitch for that:
There is a clear trend both within the Python community and software development in general to move towards typing (this is also mentioned in the conversation above)
Python still lacks a high-quality, reasonably well-used, external library for functional programming that includes typing support [1]
A first step could be to bring some typing support to a few widely-used functions like
curry,composeandpipe. Perhaps not necessarily covering all use cases for these, but a good part of them.[1]: dry-python/returns would probably be the closest in that regard, but with its container-based approach it has a slightly different direction than toolz, also, its toolkit is far less comprehensive than toolz's and much less stable/reliable.
Ilykuleshov commentedon Jul 29, 2023
Any update on this? It's the one thing stopping me from using toolz rn, would be great if this was fixed
petergaultney commentedon Mar 7, 2024
Would y'all be open to a PR that starts with the easy stuff instead of the hard stuff?
itertoolz, for the most part, should be extremely easy to add type annotations to - nearly all functions would be some form of(seq: Sequence[T]) -> Tor (seq: Sequence[T]) -> Sequence[T]. That, plus apy.typedfile in the root, would give our team what we need to start using at least theitertoolz` subset of functionality without writing a lot of untyped code.returnshas been mentioned, and it covers our typedcompose/curry/pipeneeds quite well, but I'm not aware of a library that does whatitertoolzdoes and has static types. And typingcomposeand friends is not quite as simple - you need a lot ofoverload-type stuff usually as far as I understand it.OutSquareCapital commentedon Aug 7, 2025
If anyone is interested I just made a repo for the stubs I made for a personal project.
They are for cytoolz but I exclusively used the toolz official doc to make them, so they should be good for both.
If anyone want to take a look it's here:
https://github.com/OutSquareCapital/cytoolz-stubs
This is not compatible for older versions but It's a solid base to work from.
curry class is hard to deal with because of the dynamic call return type but otherwise I cover almost everything.
mgrinshpon commentedon Nov 26, 2025
Hey all, I just published toolz-stubs (on pypi) to provide type hinting. It works for both toolz and tlz but could definitely use some more work - particularly around curried operators and some of the more bespoke functions like diff, join, memoize, and a few others.
Hopefully this helps folks that are looking for type hinting in this amazing module!
Expose combined `__annotations__` on composed functions (#496) (#634)