Skip to content

Commit

Permalink
Add Static Typing to Validate files (#1230)
Browse files Browse the repository at this point in the history
* Add types to validate files

Signed-off-by: Michael Carlstrom <[email protected]>

* remove type annotations from docstrings

Signed-off-by: Michael Carlstrom <[email protected]>

* removed other type annotated docstrings

Signed-off-by: Michael Carlstrom <[email protected]>

---------

Signed-off-by: Michael Carlstrom <[email protected]>
  • Loading branch information
InvincibleRMC authored Mar 8, 2024
1 parent ff00de1 commit b3be0f1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
6 changes: 3 additions & 3 deletions rclpy/rclpy/expand_topic_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def expand_topic_name(topic_name: str, node_name: str, node_namespace: str) -> s
The :py:func:validate_full_topic_name(): should be used on the expanded
topic name to ensure it is valid after expansion.
:param topic_name str: topic name to be expanded
:param node_name str: name of the node that this topic is associated with
:param namespace str: namespace that the topic is within
:param topic_name: topic name to be expanded
:param node_name: name of the node that this topic is associated with
:param namespace: namespace that the topic is within
:returns: expanded topic name which is fully qualified
:raises: ValueError if the topic name, node name or namespace are invalid
"""
Expand Down
2 changes: 1 addition & 1 deletion rclpy/rclpy/topic_or_service_is_hidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def topic_or_service_is_hidden(name: str) -> bool:
http://design.ros2.org/articles/topic_and_service_names.html#hidden-topic-or-service-names
:param name str: topic or service name to test
:param name: topic or service name to test
:returns: True if name is hidden, otherwise False
"""
return any(token for token in name.split('/') if token.startswith(HIDDEN_TOPIC_PREFIX))
8 changes: 5 additions & 3 deletions rclpy/rclpy/validate_full_topic_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal

from rclpy.exceptions import InvalidServiceNameException
from rclpy.exceptions import InvalidTopicNameException
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy


def validate_full_topic_name(name, *, is_service=False):
def validate_full_topic_name(name: str, *, is_service: bool = False) -> Literal[True]:
"""
Validate a given topic or service name, and raise an exception if invalid.
Expand All @@ -26,8 +28,8 @@ def validate_full_topic_name(name, *, is_service=False):
If the name is invalid then rclpy.exceptions.InvalidTopicNameException
will be raised.
:param name str: topic or service name to be validated
:param is_service bool: if true, InvalidServiceNameException is raised
:param name: topic or service name to be validated
:param is_service: if true, InvalidServiceNameException is raised
:returns: True when it is valid
:raises: InvalidTopicNameException: when the name is invalid
"""
Expand Down
6 changes: 4 additions & 2 deletions rclpy/rclpy/validate_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal

from rclpy.exceptions import InvalidNamespaceException
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy


def validate_namespace(namespace):
def validate_namespace(namespace: str) -> Literal[True]:
"""
Validate a given namespace, and raise an exception if it is invalid.
Expand All @@ -27,7 +29,7 @@ def validate_namespace(namespace):
If the namespace is invalid then rclpy.exceptions.InvalidNamespaceException
will be raised.
:param namespace str: namespace to be validated
:param namespace: namespace to be validated
:returns: True when it is valid
:raises: InvalidNamespaceException: when the namespace is invalid
"""
Expand Down
6 changes: 4 additions & 2 deletions rclpy/rclpy/validate_node_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal

from rclpy.exceptions import InvalidNodeNameException
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy


def validate_node_name(node_name):
def validate_node_name(node_name: str) -> Literal[True]:
"""
Validate a given node_name, and raise an exception if it is invalid.
If the node_name is invalid then rclpy.exceptions.InvalidNodeNameException
will be raised.
:param node_name str: node_name to be validated
:param node_name: node_name to be validated
:returns: True when it is valid
:raises: InvalidNodeNameException: when the node_name is invalid
"""
Expand Down
6 changes: 4 additions & 2 deletions rclpy/rclpy/validate_parameter_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal

from rclpy.exceptions import InvalidParameterException


def validate_parameter_name(name: str) -> bool:
def validate_parameter_name(name: str) -> Literal[True]:
"""
Validate a given parameter name, and raise an exception if invalid.
Expand All @@ -24,7 +26,7 @@ def validate_parameter_name(name: str) -> bool:
If the name is invalid then rclpy.exceptions.InvalidParameterException
will be raised.
:param name str: parameter name to be validated.
:param name: parameter name to be validated.
:raises: InvalidParameterException: when the name is invalid.
"""
# TODO(jubeira): add parameter name check to be implemented at RCL level.
Expand Down
8 changes: 5 additions & 3 deletions rclpy/rclpy/validate_topic_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal

from rclpy.exceptions import InvalidServiceNameException
from rclpy.exceptions import InvalidTopicNameException
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy

TOPIC_SEPARATOR_STRING = '/'


def validate_topic_name(name, *, is_service=False):
def validate_topic_name(name: str, *, is_service: bool = False) -> Literal[True]:
"""
Validate a given topic or service name, and raise an exception if invalid.
Expand All @@ -28,8 +30,8 @@ def validate_topic_name(name, *, is_service=False):
If the name is invalid then rclpy.exceptions.InvalidTopicNameException
will be raised.
:param name str: topic or service name to be validated
:param is_service bool: if true, InvalidServiceNameException is raised
:param name: topic or service name to be validated
:param is_service: if true, InvalidServiceNameException is raised
:returns: True when it is valid
:raises: InvalidTopicNameException: when the name is invalid
"""
Expand Down

0 comments on commit b3be0f1

Please sign in to comment.