adafruit / Adafruit_CircuitPython_NTP

Network Time Protocol (NTP) Helper for CircuitPython
MIT License
9 stars 18 forks source link

Missing Type Annotations #18

Closed FoamyGuy closed 2 years ago

FoamyGuy commented 2 years ago

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @foamyguy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

process1183 commented 2 years ago

Hi @FoamyGuy and @kattni,

I have a question about annotating the esp arg: https://github.com/adafruit/Adafruit_CircuitPython_NTP/blob/97fae398122d94bc01ac7ec35e76a729beb36ea1/adafruit_ntp.py#L37

I'm not sure what I should put for the esp annotation. I thought about importing the ESP_SPIcontrol class from adafruit_esp32spi.adafruit_esp32spi, and using that to annotate the esp arg, but then mypy throws the following error:

adafruit_ntp.py:25: error: Skipping analyzing "adafruit_esp32spi.adafruit_esp32spi": found module but no type hints or library stubs

Simply using "ESP_SPIcontrol" does not seem to work either (...self, esp: "ESP_SPIcontrol", ...):

adafruit_ntp.py:37: error: Name "ESP_SPIcontrol" is not defined

One possible solution would be to use the Any annotation type. What do you think?

Thanks!

FoamyGuy commented 2 years ago

@process1183 Thanks for working on this! I think you had the right idea with using ESP_SPIcontrol from adafruit_esp32spi.adafruit_esp32spi

I think the error that it's reporting is due to the esp32SPI library also missing type information. I would guess that error goes away if / when we get types filled in to that library.

For now I think it's fine to use that type even though it does report that error at this time. For now we are focused primarily on the "missing" types. Once the majority of them are filled in across the libraries we will include mypy in the CI actions and resolve any other errors that remain.

process1183 commented 2 years ago

Thanks for the feedback! I've opened PR #19 to add the annotations.

FoamyGuy commented 2 years ago

resolved by #19