Skip to content

Type annotations for adafruit_itertools #24

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

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

kbsriram
Copy link
Contributor

This adds another set of type annotations for #12

  • Mypy passes the adafruit_itertools module without errors.
  • Added tests for functionality and to exercise type annotations, and mypy also passes these tests without errors.

I've added a few #type: ignores 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 would
intentionally 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.)

This adds another set of type annotations for
adafruit#12

- Mypy passes the adafruit_itertools module without errors.
- Added tests for functionality and to exercise type annotations,
  and mypy also passes these tests without errors.
@dhalbert dhalbert requested a review from a team March 27, 2024 15:15
@tekktrik tekktrik requested review from tekktrik and FoamyGuy March 27, 2024 17:16
Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixups of the type annotations and the new tests!

@dhalbert dhalbert merged commit 750de7a into adafruit:main Apr 25, 2024
@kbsriram kbsriram deleted the add-types-2 branch April 26, 2024 05:58
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Apr 30, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager to 1.2.1 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ConnectionManager#15 from justmobilize/update-docs

Updating https://github.com/adafruit/Adafruit_CircuitPython_IterTools to 2.1.1 from 2.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_IterTools#25 from kbsriram/add-types-3
  > Merge pull request adafruit/Adafruit_CircuitPython_IterTools#24 from kbsriram/add-types-2

Updating https://github.com/adafruit/Adafruit_CircuitPython_PIOASM to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_PIOASM#63 from kbsriram/add-out-tests

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants