-
Notifications
You must be signed in to change notification settings - Fork 128
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
Conversation
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>
There was a problem hiding this 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.
There was a problem hiding this 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]: |
There was a problem hiding this comment.
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:
) -> List[IdlContentElementT]: | |
) -> List[IdlContentElement]: |
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
rosidl_parser
@Mergifyio update |
✅ Branch has been successfully updated |
Pulls: #791 |
Thank you for the PR! |
Adding types to defintion.py to solve warnings raised in ros2/rosidl_python#206 and improve readability.