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

[RSDK-878] Typechecking #512

Merged
merged 18 commits into from
Jan 4, 2024
Merged

[RSDK-878] Typechecking #512

merged 18 commits into from
Jan 4, 2024

Conversation

njooma
Copy link
Member

@njooma njooma commented Jan 2, 2024

Add typechecking to the python SDK. I chose pyright over mypy for a few reasons:

  1. pyright is the built-in type checker in VS Code
  2. pyright is a bit more lenient, so easier to get started with
  3. pyright seems to get updates a bit more frequently than mypy

We will likely still want to eventually move over to mypy, but this is a great starting place for us.

Some notes on why I did what I did:

  • __eq__ functions
    • Technically equality functions should accept any object. So when we wrote our own __eq__ methods that were typed to the type of self, it was an incorrect override
  • SUBTYPE in resources
    • the SUBTYPE field is required for all resource types (anything that extends from ResourceBase). When we actually implement that in our concrete subtypes, we want it to be Final. However, since the original abstract definition is not Final (because it's abstract), the typechecker views it as an incompatible override. So I ignore it
  • **__ in method functions
    • Wherever an abstract resource type accepts **kwargs as parameters, those need to be included in the client to make a valid override. However, those would go unused. So I added **__ to signify unused kwargs
  • Component RPC services are now generic over component
    • You can genericize your RPC service so that when we call self.get_resource, it's correctly typed
  • Board sub-components (e.g. GPIOPin) no longer subclass ComponentBase
    • They are not components and do not need the additional features that subclassing gives them, e.g. from_robot

@njooma njooma requested a review from a team as a code owner January 2, 2024 20:57
Copy link
Contributor

github-actions bot commented Jan 2, 2024

Warning your change may break code samples. If your change modifies any of the following functions please contact @viamrobotics/fleet-management. Thanks!

component function
base is_moving
board gpio_pin_by_name
camera get_image
encoder get_position
motor is_moving
sensor get_readings
servo get_position
arm get_end_position
gantry get_lengths
gripper is_moving
movement_sensor get_linear_acceleration
input_controller get_controls
audio get_properties
pose_tracker get_poses
power_sensor get_power
motion get_pose
vision get_classifications_from_camera

@njooma njooma requested a review from stuqdog January 2, 2024 22:14
@njooma
Copy link
Member Author

njooma commented Jan 2, 2024

@randhid @penguinland FYI this PR removes the subclassing from GPIOPin, AnalogReader, DigitalIterrupt

with open(dest, "wb") as file:
file.write(data)
stream: Stream[GetInvoicePdfRequest, GetInvoicePdfResponse]
async with self._billing_client.GetInvoicePdf.open(timeout=timeout, metadata=self._metadata) as stream:
Copy link
Member Author

Choose a reason for hiding this comment

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

type checking surfaced this issue, where we weren't streaming the response properly

Comment on lines +32 to +39
myst-nb = [
{version = "<1.0.0", python = "<3.9"},
{version = ">=1.0.0", python = ">=3.9"},
]
sphinx-autoapi = [
{version = "<3.0.0", python = "<3.9"},
{version = ">=3.0.0", python = ">=3.9"},
]
Copy link
Member Author

Choose a reason for hiding this comment

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

Updating deps only support >3.9

@@ -44,6 +45,8 @@ def getLogger(name: str) -> logging.Logger:


def addHandlers(logger: logging.Logger):
logger.handlers.clear()
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to fix an issue where logs were getting doubled if the level was changed

@@ -41,15 +41,15 @@ async def get_position(self, *, timeout: Optional[float] = None) -> Pose:
response: GetPositionResponse = await self.client.GetPosition(request, timeout=timeout)
return response.pose

async def get_point_cloud_map(self, *, timeout: Optional[float] = None) -> List[GetPointCloudMapResponse]:
async def get_point_cloud_map(self, *, timeout: Optional[float] = None) -> List[bytes]:
Copy link
Member Author

Choose a reason for hiding this comment

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

type checking surfaced these incorrect return types

Copy link
Member

@stuqdog stuqdog left a comment

Choose a reason for hiding this comment

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

Nice! A couple questions and one small change, otherwise lgtm!

Comment on lines 1374 to 1375
return response.url
await stream.recv_trailing_metadata() # causes us to throw appropriate gRPC error.
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this got inverted from how it was before? we want to recv_trailing_metadata() to throw an error, when response is None.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure why I changed it, but isn't this functionally the same?

Copy link
Member

Choose a reason for hiding this comment

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

🤦 yes you're absolutely right, read it too quickly and thought the if statements were equivalent.

I do have a slight preference for either leaving it the way it was or updating the equivalent logic in data_client.py and audo_input/client.py just to all be consistent, but I don't feel strongly enough to hold off release.

src/viam/services/vision/client.py Show resolved Hide resolved
src/viam/__init__.py Show resolved Hide resolved
Copy link
Member

@purplenicole730 purplenicole730 left a comment

Choose a reason for hiding this comment

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

LGTM. It's cool to see the errors that type checking picked up!

Copy link
Member

@stuqdog stuqdog left a comment

Choose a reason for hiding this comment

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

lgtm!

@njooma njooma merged commit 49c354f into viamrobotics:main Jan 4, 2024
12 checks passed
@njooma njooma deleted the typechecking branch January 4, 2024 18:36
nicksanford pushed a commit to nicksanford/viam-python-sdk that referenced this pull request Jan 29, 2024
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.

3 participants