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

Adding annotations #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

andrib-94
Copy link

Adding type-hints for the module. Added asserts needed for type checking, as well as moved the init function to the beginning of the class for additional compliance with mypy.

@tekktrik tekktrik linked an issue Apr 27, 2023 that may be closed by this pull request
14 tasks
Copy link

@FoamyGuy FoamyGuy 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 working on this @perkin-borowa!

I have a few suggested changes, and some that I am less sure about but will do some testing to verify. Also one about re-using an Alias from our typing library that I'm interested in input from some other team members on.

@@ -31,7 +37,7 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"


def _check_color(color):
def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> Optional[int]:
Copy link

Choose a reason for hiding this comment

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

We have an Alias that covers these types of Color arguments. But it's in a file that associates it with leds: https://github.com/adafruit/Adafruit_CircuitPython_Typing/blob/bd358db434d2e2c155c897d7f3c9674b04a3b67f/circuitpython_typing/led.py#L18

@tekktrik do you think it's worth making a slightly more generic Alias for Color inside of circuitpython_typing that can be used in other libraries for hex / tuple colors? Many of our libraries expect both in the context of displayio as well as LEDs.

Or alternatively is it problematic to use the typing from inside of led.py here even though this context isn't dealing with LEDs?

Copy link
Author

Choose a reason for hiding this comment

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

Hi, let me know when you decide on the way forward with Color implementation and I am happy to import it from appropriate module : )

Copy link

Choose a reason for hiding this comment

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

I've put a note to discuss this in the weeds for the upcoming weekly meeting.

Copy link
Member

Choose a reason for hiding this comment

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

I think having multiple aliases is a good idea, sorry for the late response!

Copy link
Member

Choose a reason for hiding this comment

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

We could always change up the way that colors are defined, potentially moving them out of led.py and into a new file like color.py.

adafruit_button.py Show resolved Hide resolved
style: int = RECT,
fill_color: Optional[int] = 0xFFFFFF,
outline_color: Optional[int] = 0x0,
label: Optional[Label] = None,
Copy link

Choose a reason for hiding this comment

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

label is expecting a string type. It will eventually put that string into a Label object, but when passed in here it should be a string I believe.

Copy link
Author

Choose a reason for hiding this comment

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

Hi, so here is where I was struggling during Sprint. When label is not Label object, both property and property setter for it throw errors, as we utilize Label object's arguments (.text, .bounding_box etc.) on variable we hinted as string.

Is there any way to work-around this issue, so mypy does not throw errors, when checking?

Thanks!

Copy link

Choose a reason for hiding this comment

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

@perkin-borowa I think the type hinter may be confused by this line in the init function:

self._label = label

This is setting the argument label onto self._label which is technically mixing types. The rest of the code is written to assume that self._label will be a Label object.

It never caused any other trouble because the last line of the init function ends up superceding this one that is erroneously setting self._label to the string argument.

self.label = label

That line calls the setter which is expecting the text to get passed into it.

Try removing

self._label = label

And see if mypy still reports any errors about Label object vs. string for the argument.

adafruit_button.py Show resolved Hide resolved
@property
def label(self):
def label(self) -> Optional[Union[Any, str]]:
Copy link

Choose a reason for hiding this comment

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

I'm not sure we need Any here for the return type. Are there cases where the return will be something other than a string or None?

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.

Missing Type Annotations
3 participants