Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Type annotations #37

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 15 commits into from
May 8, 2023
Merged

Conversation

HundredVisionsGuy
Copy link
Contributor

Still failing mypy and I have some questions to pose regarding the quaternion_packet.py script on line 44.

I need to head home, but I will continue working on this in the coming days. Here are the mypy results:


adafruit_bluefruit_connect\packet.py:70: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior  [str-bytes-safe]
adafruit_bluefruit_connect\packet.py:130: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior  [str-bytes-safe]
adafruit_bluefruit_connect\color_packet.py:45: error: Argument 1 to "len" has incompatible type "Optional[Packet]"; 
expected "Sized"  [arg-type]
adafruit_bluefruit_connect\color_packet.py:45: error: Item "Packet" of "Optional[Packet]" has no attribute "__iter__" (not iterable)  [union-attr]
adafruit_bluefruit_connect\color_packet.py:45: error: Item "None" of "Optional[Packet]" has no attribute "__iter__" 
(not iterable)  [union-attr]
adafruit_bluefruit_connect\color_packet.py:46: error: Cannot determine type of "_color"  [has-type]
adafruit_bluefruit_connect\color_packet.py:51: error: Argument 1 of "parse_private" is incompatible with supertype "Packet"; supertype defines the argument type as "bytes"  [override]
adafruit_bluefruit_connect\color_packet.py:51: note: This violates the Liskov substitution principle
adafruit_bluefruit_connect\color_packet.py:51: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
adafruit_bluefruit_connect\color_packet.py:56: error: Argument 1 to "ColorPacket" has incompatible type "Tuple[Any, 
...]"; expected "Optional[Packet]"  [arg-type]
adafruit_bluefruit_connect\color_packet.py:56: error: Argument 2 to "unpack" has incompatible type "Optional[Packet]"; expected "Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData, PickleBuffer]]"  [arg-type]        
adafruit_bluefruit_connect\color_packet.py:61: error: Cannot determine type of "_color"  [has-type]
adafruit_bluefruit_connect\color_packet.py:69: error: Cannot determine type of "_color"  [has-type]
adafruit_bluefruit_connect\quaternion_packet.py:44: error: Unsupported operand types for + ("bytes" and "int")  [operator]
Found 12 errors in 3 files (checked 11 source files)```

This is my model for other similar packet issues (especially those
with the cls parameter.
As per Jeff's recommendations - we are ignoring the mypy warning on
the format() method regarding bytes.
with a little house cleaning of _xyz_packet
We are still getting mypy complaints about the Liskov Principle,
and quarternion due to trying to return the sum of a bytes and an
int. Need to flag this as an issue.
We are still getting mypy complaints about the Liskov Principle,
and quarternion due to trying to return the sum of a bytes and an
int. Need to flag this as an issue.
To avoid the dreaded Liskov Substitution Principle (thanks to mypy's
fastidiousness)..
@FoamyGuy
Copy link
Contributor

resolves: #31

@FoamyGuy
Copy link
Contributor

Thanks for working on this!

Nice find on that issue at line 44 in quaternion packet. I'm not super familiar with this library, but after having a look around I am thinking that might be a bug. The code it warns about is this:

        partial_packet = struct.pack(
            self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z, self._w
        )
        return partial_packet + self.checksum(partial_packet)

Which does look to me like it would be trying add a bytes() and an int which as the error states isn't really allowed.

I'm thinking it should be doing something like inside of here in the base class:

def add_checksum(self, partial_packet):
"""Compute the checksum of partial_packet and return a new bytes
with the checksum appended.
"""
return partial_packet + bytes((self.checksum(partial_packet),))

And I'm guessing it hasn't been used in the right way for anyone to run into that issue (or at least not reported it if so).

Regarding the other mypy warnings: It's okay at this point if we're not passing 100% of the mypy checks. This first pass is aimed at getting the "low hanging fruit" of function arguments and returns. Once we get all of that taken care of eventually we'll enable automatic mypy check and decide on a configuration for it at that point with which warnings will be active in the automated check

@HundredVisionsGuy
Copy link
Contributor Author

HundredVisionsGuy commented Apr 27, 2023 via email

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

I do think it's okay to take the PR out of draft mode even though the line 44 thing is still outstanding.

You can go ahead and make the change on line 44 if you want I think we'd be wrapping the self.checksum() inside of a call to bytes() just like the one in the code linked above.

We'll try to get someone with more familiarity with this library to take a look and verify that it makes sense.

@HundredVisionsGuy
Copy link
Contributor Author

Closes #37

NOTE: on quaternion_packet.py to_bytes() line 44, I wrapped self.checksum(partial_packet, ) in bytes() per base class's to_bytes() (packet.py) per suggestion by @FoamyGuy .

@HundredVisionsGuy HundredVisionsGuy marked this pull request as ready for review April 28, 2023 23:49
Copy link
Contributor

@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.

One change needed for an import I believe.

The rest is looking good to me, but I do want to test it out on a device since there are a few changes that could possibly lead to different behavior. I'll report back if anything else comes up.

Copy link
Contributor

@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.

This looks good to me now. Thanks again for taking this on @HundredVisionsGuy!

I pushed one commit to move that import into it's try block.

I tested all examples in the repo successfully with a Feather Sense and Android phone.

@FoamyGuy FoamyGuy merged commit e18be23 into adafruit:main May 8, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 9, 2023
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
2 participants