Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
358 changes: 218 additions & 140 deletions packtype/types/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
# SPDX-License-Identifier: Apache-2.0
#

try:
from typing import Self
except ImportError:
from typing_extensions import Self # noqa: UP035

from .base import MetaBase


class Numeric:
def __int__(self) -> int:
Expand All @@ -18,122 +11,195 @@ def __int__(self) -> int:
# Operator overrides
# ==========================================================================

def __add__(self, other: int | Self) -> int:
return int(self) + int(other)

def __sub__(self, other: int | Self) -> int:
return int(self) - int(other)

def __mul__(self, other: int | Self) -> int:
if isinstance(other, MetaBase):
return int(self) * other
else:
return int(self) * int(other)

def __truediv__(self, other: int | Self) -> int:
return int(self) / int(other)

def __floordiv__(self, other: int | Self) -> int:
return int(self) // int(other)

def __mod__(self, other: int | Self) -> int:
return int(self) % int(other)

def __divmod__(self, other: int | Self) -> int:
return (int(self) // int(other), int(self) % int(other))

def __pow__(self, other: int | Self) -> int:
return int(self) ** int(other)

def __lshift__(self, other: int | Self) -> int:
return int(self) << int(other)

def __rshift__(self, other: int | Self) -> int:
return int(self) >> int(other)

def __and__(self, other: int | Self) -> int:
return int(self) & int(other)

def __xor__(self, other: int | Self) -> int:
return int(self) ^ int(other)

def __or__(self, other: int | Self) -> int:
return int(self) | int(other)

def __radd__(self, other: int | Self) -> int:
return int(other) + int(self)

def __rsub__(self, other: int | Self) -> int:
return int(other) - int(self)

def __rmul__(self, other: int | Self) -> int:
return int(other) * int(self)

def __rtruediv__(self, other: int | Self) -> int:
return int(other) / int(self)

def __rfloordiv__(self, other: int | Self) -> int:
return int(other) // int(self)

def __rmod__(self, other: int | Self) -> int:
return int(other) % int(self)

def __rdivmod__(self, other: int | Self) -> int:
return (int(other) // int(self), int(other) % int(self))

def __rpow__(self, other: int | Self) -> int:
return int(other) ** int(self)

def __rlshift__(self, other: int | Self) -> int:
return int(other) << int(self)

def __rrshift__(self, other: int | Self) -> int:
return int(other) >> int(self)

def __rand__(self, other: int | Self) -> int:
return int(other) & int(self)

def __rxor__(self, other: int | Self) -> int:
return int(other) ^ int(self)

def __ror__(self, other: int | Self) -> int:
return int(other) | int(self)

def __iadd__(self, other: int | Self) -> int:
return int(self) + int(other)

def __isub__(self, other: int | Self) -> int:
return int(self) - int(other)

def __imul__(self, other: int | Self) -> int:
return int(self) * int(other)

def __itruediv__(self, other: int | Self) -> int:
return int(self) / int(other)

def __ifloordiv__(self, other: int | Self) -> int:
return int(self) // int(other)

def __imod__(self, other: int | Self) -> int:
return int(self) % int(other)

def __ipow__(self, other: int | Self) -> int:
return int(self) ** int(other)

def __ilshift__(self, other: int | Self) -> int:
return int(self) << int(other)

def __irshift__(self, other: int | Self) -> int:
return int(self) >> int(other)

def __iand__(self, other: int | Self) -> int:
return int(self) & int(other)

def __ixor__(self, other: int | Self) -> int:
return int(self) ^ int(other)

def __ior__(self, other: int | Self) -> int:
return int(self) | int(other)
def __add__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) + other

def __sub__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) - other

def __mul__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) * other

def __truediv__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) / other

def __floordiv__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) // other

def __mod__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) % other

def __divmod__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return (int(self) // other, int(self) % other)

def __pow__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) ** other

def __lshift__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) << other

def __rshift__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) >> other

def __and__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) & other

def __xor__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) ^ other

def __or__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) | other

def __radd__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other + int(self)

def __rsub__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other - int(self)

def __rmul__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other * int(self)

def __rtruediv__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other / int(self)

def __rfloordiv__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other // int(self)

def __rmod__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other % int(self)

def __rdivmod__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return (other // int(self), other % int(self))

def __rpow__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other ** int(self)

def __rlshift__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other << int(self)

def __rrshift__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other >> int(self)

def __rand__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other & int(self)

def __rxor__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other ^ int(self)

def __ror__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return other | int(self)

def __iadd__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) + other

def __isub__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) - other

def __imul__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) * other

def __itruediv__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) / other

def __ifloordiv__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) // other

def __imod__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) % other

def __ipow__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) ** other

def __ilshift__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) << other

def __irshift__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) >> other

def __iand__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) & other

def __ixor__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) ^ other

def __ior__(self, other) -> int:
if isinstance(other, Numeric):
other = int(other)
return int(self) | other

def __neg__(self) -> int:
return -1 * int(self)
Expand All @@ -150,23 +216,35 @@ def __index__(self) -> int:
def __invert__(self) -> int:
return ~int(self)

def __lt__(self, other: int | Self) -> bool:
return int(self) < int(other)

def __le__(self, other: int | Self) -> bool:
return int(self) <= int(other)

def __eq__(self, other: int | Self) -> bool:
return int(self) == int(other)

def __ne__(self, other: int | Self) -> bool:
return int(self) != int(other)

def __gt__(self, other: int | Self) -> bool:
return int(self) > int(other)

def __ge__(self, other: int | Self) -> bool:
return int(self) >= int(other)
def __lt__(self, other) -> bool:
if isinstance(other, Numeric):
other = int(other)
return int(self) < other

def __le__(self, other) -> bool:
if isinstance(other, Numeric):
other = int(other)
return int(self) <= other

def __eq__(self, other) -> bool:
if isinstance(other, Numeric):
other = int(other)
return int(self) == other

def __ne__(self, other) -> bool:
if isinstance(other, Numeric):
other = int(other)
return int(self) != other

def __gt__(self, other) -> bool:
if isinstance(other, Numeric):
other = int(other)
return int(self) > other

def __ge__(self, other) -> bool:
if isinstance(other, Numeric):
other = int(other)
return int(self) >= other

def __hash__(self) -> int:
return id(self)
4 changes: 4 additions & 0 deletions tests/pysyntax/test_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class TestPkg:
assert idx == expected
expected += 1

# Test out replication operator
assert [1, 2, 3] * TestPkg.A == [1, 2, 3] * 35
assert TestPkg.A * [1, 2, 3] == [1, 2, 3] * 35


def test_constant_reference():
@packtype.package()
Expand Down