-
Notifications
You must be signed in to change notification settings - Fork 264
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
optional static typing for toolz #496
Comments
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 |
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? |
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: |
@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 |
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 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 |
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 For example, the def remove(predicate, seq):
return itertools.filterfalse(predicate, seq) could be typed in two different ways; (1) The following type hints for the function specify ALL the ways T = TypeVar("T")
def remove(predicate: Union[None, Callable[[T], Any]], seq: Iterable[T]) -> Iterator[T]:
return itertools.filterfalse(predicate, seq) (2) Or the following (more specific) type hints for the function declare the "intended" way to call the function ( T = TypeVar("T")
def remove(predicate: Callable[[T], bool], seq: Iterable[T]) -> Iterator[T]:
return itertools.filterfalse(predicate, seq) I would choose option (2) if writing @eriknw Can I get your opinion on this? |
Are there any plans to add an empty |
@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:
A first step could be to bring some typing support to a few widely-used functions like [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. |
Any update on this? It's the one thing stopping me from using toolz rn, would be great if this was fixed |
Would y'all be open to a PR that starts with the easy stuff instead of the hard stuff?
|
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
andg
are typed functions defined as follows:Since
f
isCallable[[A], B]
andg
isCallable[[B], C]
, one would expect the compositetoolz.compose(g, f)
to beCallable[[A], C]
. (Here I am using theCallable
protocol from the typing module).Of course, implementing this could be tricky.
The text was updated successfully, but these errors were encountered: