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 #791

Open
wants to merge 6 commits into
base: rolling
Choose a base branch
from

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.

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