Type annotations for adafruit_itertools
#24
Merged
+420
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds another set of type annotations for #12
I've added a few
#type: ignore
s in the library where the code uses sentinels or just to simplify things.Additional notes for annotations that could be worth a second look:
accumulate
: can theoretically accumulate a different type than the input list, but can be somewhat surprising as it only starts from the second element. e.g.list(it.accumulate([1, 2], lambda x, y: str(x) + str(y)))
returns[1, "12"]
. I've chosen to restrict it so the accumulator also produces the same type as the iterator, as it seemed more likely to catch unintended errors, but there are arguably other reasonable choices.I made the opposite decision in a few places by using
Any
to keep things simple, and/or there isn't enough support in micropython (class generics,@overload
etc).groupby
: is implemented as a class, but unfortunately it proved difficult to annotate the class itself (or the__init__
method) usefully. So I kept it simple, but relaxed; but callers may need to annotate the result to get useful type checks.product
,zip_longest
: relaxed the type checking on the incoming iterators as it seemed more likely that callers wouldintentionally use different types in the iterators, and it was simpler to relax the constraints.
(I'll repeat the process for the
adafruit_itertools_extra
once this batch of annotations gets in.)