Skip to content

Commit

Permalink
Avoid errors about Unsupported operand types for >= ("int" and "Durat…
Browse files Browse the repository at this point in the history
…ion")

mypy does not have support for @functools.total_ordering:

python/mypy#4610
  • Loading branch information
chadrik committed Oct 27, 2019
1 parent 0791ebb commit baa6091
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions sdks/python/apache_beam/utils/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from __future__ import division

import datetime
import functools
from builtins import object
from typing import Any
from typing import Union
Expand All @@ -36,7 +35,6 @@
from apache_beam.portability import common_urns


@functools.total_ordering
class Timestamp(object):
"""Represents a Unix second timestamp with microsecond granularity.
Expand Down Expand Up @@ -174,6 +172,18 @@ def __lt__(self, other):
other = Timestamp.of(other)
return self.micros < other.micros

def __gt__(self, other):
# type: (Union[int, float, Timestamp, Duration]) -> bool
return not (self < other or self == other)

def __le__(self, other):
# type: (Union[int, float, Timestamp, Duration]) -> bool
return self < other or self == other

def __ge__(self, other):
# type: (Union[int, float, Timestamp, Duration]) -> bool
return not self < other

def __hash__(self):
return hash(self.micros)

Expand Down Expand Up @@ -203,7 +213,6 @@ def __mod__(self, other):
common_urns.constants.MAX_TIMESTAMP_MILLIS.constant)*1000)


@functools.total_ordering
class Duration(object):
"""Represents a second duration with microsecond granularity.
Expand Down Expand Up @@ -273,12 +282,24 @@ def __ne__(self, other):
return not self == other

def __lt__(self, other):
# type: (Union[int, float, Duration, Timestamp]) -> bool
# type: (Union[int, float, Timestamp, Duration]) -> bool
# Allow comparisons between Duration and Timestamp values.
if not isinstance(other, Timestamp):
other = Duration.of(other)
return self.micros < other.micros

def __gt__(self, other):
# type: (Union[int, float, Timestamp, Duration]) -> bool
return not (self < other or self == other)

def __le__(self, other):
# type: (Union[int, float, Timestamp, Duration]) -> bool
return self < other or self == other

def __ge__(self, other):
# type: (Union[int, float, Timestamp, Duration]) -> bool
return not self < other

def __hash__(self):
return hash(self.micros)

Expand Down

0 comments on commit baa6091

Please sign in to comment.