Skip to content

Commit

Permalink
Time.py Types (#1237)
Browse files Browse the repository at this point in the history
* Start typing time.py

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

* Testing out Enum wrapper for ClockType

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

* convert to rcl_clock_type_t

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

* Undo Change to time_point.cpp

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

* Update create_time_point

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

* Lint fixes

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

* Add debug message

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

* Remove test file

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

* Try extending the type assert

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

* Add types to logging_service.py (#1227)

* add types to logging_service

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

* Add types to duration.py

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

* Add newlines for class definintions

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

* update type alias name

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

* Remove newline

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

* Merge?

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

* Fix failed merge

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

* Update to use Protocols

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

* Fix import error

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

* Add types to time.py

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

* Linty

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

---------

Signed-off-by: Michael Carlstrom <[email protected]>
  • Loading branch information
InvincibleRMC authored Mar 8, 2024
1 parent b06baef commit 6f2507f
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions rclpy/rclpy/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Tuple
from typing import Protocol, Tuple, Union

import builtin_interfaces.msg

from rclpy.constants import S_TO_NS
from rclpy.duration import Duration
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy

from .clock_type import ClockType

CONVERSION_CONSTANT = 10 ** 9

class TimeType(Protocol):
"""Type alias of _rclpy.rcl_time_point_t."""

nanoseconds: int
clock_type: ClockType


class Time:
Expand All @@ -35,21 +42,21 @@ class Time:

def __init__(
self, *,
seconds=0, nanoseconds=0,
seconds: Union[int, float] = 0, nanoseconds: int = 0,
clock_type: ClockType = ClockType.SYSTEM_TIME):
if not isinstance(clock_type, (ClockType, _rclpy.ClockType)):
raise TypeError('Clock type must be a ClockType enum')
if seconds < 0:
raise ValueError('Seconds value must not be negative')
if nanoseconds < 0:
raise ValueError('Nanoseconds value must not be negative')
total_nanoseconds = int(seconds * CONVERSION_CONSTANT)
total_nanoseconds = int(seconds * S_TO_NS)
total_nanoseconds += int(nanoseconds)
if total_nanoseconds >= 2**63:
# pybind11 would raise TypeError, but we want OverflowError
raise OverflowError(
'Total nanoseconds value is too large to store in C time point.')
self._time_handle = _rclpy.rcl_time_point_t(total_nanoseconds, clock_type)
self._time_handle: TimeType = _rclpy.rcl_time_point_t(total_nanoseconds, clock_type)

@property
def nanoseconds(self) -> int:
Expand All @@ -63,18 +70,18 @@ def seconds_nanoseconds(self) -> Tuple[int, int]:
:return: 2-tuple seconds and nanoseconds
"""
nanoseconds = self.nanoseconds
return (nanoseconds // CONVERSION_CONSTANT, nanoseconds % CONVERSION_CONSTANT)
return (nanoseconds // S_TO_NS, nanoseconds % S_TO_NS)

@property
def clock_type(self) -> ClockType:
""":return: the type of clock that produced this instance."""
return self._time_handle.clock_type

def __repr__(self):
def __repr__(self) -> str:
return 'Time(nanoseconds={0}, clock_type={1})'.format(
self.nanoseconds, self.clock_type.name)

def __add__(self, other):
def __add__(self, other: Duration) -> 'Time':
if isinstance(other, Duration):
try:
return Time(
Expand All @@ -85,10 +92,10 @@ def __add__(self, other):
else:
return NotImplemented

def __radd__(self, other):
def __radd__(self, other: Duration) -> 'Time':
return self.__add__(other)

def __sub__(self, other):
def __sub__(self, other: Union['Time', Duration]) -> Union['Time', Duration]:
if isinstance(other, Time):
if self.clock_type != other.clock_type:
raise TypeError("Can't subtract times with different clock types")
Expand All @@ -106,40 +113,40 @@ def __sub__(self, other):
else:
return NotImplemented

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
if isinstance(other, Time):
if self.clock_type != other.clock_type:
raise TypeError("Can't compare times with different clock types")
return self.nanoseconds == other.nanoseconds
return NotImplemented

def __ne__(self, other):
def __ne__(self, other: object) -> bool:
if isinstance(other, Time):
return not self.__eq__(other)
return NotImplemented

def __lt__(self, other):
def __lt__(self, other: object) -> bool:
if isinstance(other, Time):
if self.clock_type != other.clock_type:
raise TypeError("Can't compare times with different clock types")
return self.nanoseconds < other.nanoseconds
return NotImplemented

def __le__(self, other):
def __le__(self, other: object) -> bool:
if isinstance(other, Time):
if self.clock_type != other.clock_type:
raise TypeError("Can't compare times with different clock types")
return self.nanoseconds <= other.nanoseconds
return NotImplemented

def __gt__(self, other):
def __gt__(self, other: object) -> bool:
if isinstance(other, Time):
if self.clock_type != other.clock_type:
raise TypeError("Can't compare times with different clock types")
return self.nanoseconds > other.nanoseconds
return NotImplemented

def __ge__(self, other):
def __ge__(self, other: object) -> bool:
if isinstance(other, Time):
if self.clock_type != other.clock_type:
raise TypeError("Can't compare times with different clock types")
Expand Down

0 comments on commit 6f2507f

Please sign in to comment.