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

Add types to definition.py in rosidl_parser #791

Merged
merged 7 commits into from
Oct 19, 2024

Conversation

InvincibleRMC
Copy link
Contributor

Adding types to defintion.py to solve warnings raised in ros2/rosidl_python#206 and improve readability.

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Copy link

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

lgtm with green CI. I would like to have 2nd review.

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

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

I've left one comment with a possible problem; otherwise this looks good to me.

def get_elements_of_type(
self,
type_: Type[IdlContentElementT]
) -> List[IdlContentElementT]:
Copy link
Contributor

Choose a reason for hiding this comment

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

I could be wrong, but shouldn't this be:

Suggested change
) -> List[IdlContentElementT]:
) -> List[IdlContentElement]:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Due to the isinstance check we know that they have to match type_ so using IdlContentElementT is correct.

return [e for e in self.elements if isinstance(e, type_)]

This make it so when you use get_elements_of_type it knows that returned List only has objects of type Action

foo = IdlContent().get_elements_of_type(Action)
reveal_type(foo)  # List[Action]

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess I don't understand then. This method is essentially returning a subset of self.elements, which is typed as List[IdlContentElement]. Thus it seems to me that the type of self.elements and the return type of get_elements_of_type should be the same, no?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, never mind that last comment. I guess I don't understand https://github.com/ros2/rosidl/pull/791/files#diff-0734e88e493f2337273d2bcfe9baee715ded7b09926541c65b53cd46bb9de41fR821-R822 ; can you explain more about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok so in line 821 is a Union of all possible entries into the elements list. I got these from the extract_content_from_ast in ros_idl.parser. These are all the possible values that can go into the elements list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Line 822 is a TypeVar that is upper bound by the TypeAlias from 821. Because of the isinstance check inside the list interpolation we know that the returned List will be a subset of self.elements which all match type_. Since we know the the list in a subset of only type_ objects we can make the function generic over type_ and have the list match the type of type_. I'm sorry if my explanations are not super clear, for more information check out mypy's page about generic functions.

@InvincibleRMC InvincibleRMC changed the title Add types to definition.py Add types to definition.py in rosidl_parser Oct 6, 2024
@sloretz
Copy link
Contributor

sloretz commented Oct 18, 2024

@Mergifyio update

Copy link
Contributor

mergify bot commented Oct 18, 2024

update

✅ Branch has been successfully updated

@sloretz
Copy link
Contributor

sloretz commented Oct 18, 2024

Pulls: #791
Gist: https://gist.githubusercontent.com/sloretz/2c0fe5761de1071f7ded2b57f31a82d9/raw/c5235f4fdd81c7d9d5e27a042385ff870be0cee4/ros2.repos
BUILD args: --packages-above-and-dependencies rosidl_parser
TEST args: --packages-above rosidl_parser
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/14723

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@sloretz
Copy link
Contributor

sloretz commented Oct 19, 2024

Thank you for the PR!

@sloretz sloretz merged commit a2706a5 into ros2:rolling Oct 19, 2024
4 checks passed
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.

4 participants