Skip to content
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

Missing Type Annotations #31

Closed
7 tasks
FoamyGuy opened this issue Sep 27, 2021 · 4 comments
Closed
7 tasks

Missing Type Annotations #31

FoamyGuy opened this issue Sep 27, 2021 · 4 comments
Labels
documentation good first issue Good for newcomers Hacktoberfest DigitalOcean's Hacktoberfest

Comments

@FoamyGuy
Copy link
Contributor

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:

  • adafruit_crickit.py:85
  • adafruit_crickit.py:145
  • adafruit_crickit.py:210
  • adafruit_crickit.py:244
  • adafruit_crickit.py:290
  • adafruit_crickit.py:318
  • adafruit_crickit.py:334
@adafruit-adabot adafruit-adabot added the Hacktoberfest DigitalOcean's Hacktoberfest label Oct 26, 2021
@FoamyGuy FoamyGuy removed the Hacktoberfest DigitalOcean's Hacktoberfest label Nov 4, 2021
@tcfranks
Copy link

tcfranks commented Jul 8, 2022

I was going to work on this, but feel 'in over my head', perhaps. For example: A return type of Any seems like it is an over simplification of theses return types, but since they could return an object | None, it it appropriate?

@Property
def feather_drive_stepper_motor(self):
"""adafruit_motor.motor.StepperMotor object on Drive terminals on Crickit FeatherWing"""
return self._motor(tuple(reversed(_DRIVE_STEPPER)), StepperMotor)

def _motor(self, terminals, motor_class):
    device = self._devices.get(terminals, None)
    if not isinstance(device, motor_class):
        device = motor_class(
            *(PWMOut(self._seesaw, terminal) for terminal in terminals)
        )
        self._devices[terminals] = device
    return device

@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Jul 9, 2022

There is Optional keyword for when it can return a thing or None i.e.:

from typing import Optional

def some_function(arg1: int) -> Optional[int]:
    if arg1 > 42:
        return 178
    return None

It can be done similarly for object types instead of primatives like int here is another example with an object type instead:

import board
import microcontroller
def get_neopixel_pin() -> Optional[microcontroller.Pin]:
    if board.hasattr("NEOPIXEL"):
        return board.NEOPIXEL
    return None

It should work similarly for StepperMotor or any classes more specific to this library. If you're still stuck a bit though let me know and I can take a closer look at the specific chunk of code in this library and try to offer guidance on type.

@tcfranks
Copy link

tcfranks commented Jul 9, 2022

Thanks! That means I have a couple of things to fix here and on the circuitplayground code base!

@adafruit-adabot adafruit-adabot added the Hacktoberfest DigitalOcean's Hacktoberfest label Sep 29, 2022
@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Jun 5, 2023

resolved by #37

@FoamyGuy FoamyGuy closed this as completed Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation good first issue Good for newcomers Hacktoberfest DigitalOcean's Hacktoberfest
Projects
None yet
Development

No branches or pull requests

3 participants