Skip to content

Commit

Permalink
ORM: Add unary operations +, - and abs to NumericType (#5946)
Browse files Browse the repository at this point in the history
Three unary operations are added to the `NumericType` data subclass:

* `+`: Implemented through `__pos__`
* `-`: Implemented through `__neg__`
* `abs`: Implemented through `__abs__`

The `__pos__` dunder overloads the unary plus operator `+` and returns
the value of the object as-is, whereas the `__neg__` overloads the unary
minus operator `-` and returns a new instance of the same class with the
negation of the value of the original object. The `__abs__` dunder is
called when the object is passed to the `abs` built-in and returns the
absolute value.
  • Loading branch information
Mahhheshh authored Mar 30, 2023
1 parent 728f266 commit a298c14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.eggs
.vscode
.tox
Pipfile

# files created by coverage
.cache
Expand Down
9 changes: 9 additions & 0 deletions aiida/orm/nodes/data/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,12 @@ def __float__(self):

def __int__(self):
return int(self.value)

def __pos__(self):
return self.value

def __neg__(self):
return self.new(-self.value)

def __abs__(self):
return abs(self.value)
42 changes: 42 additions & 0 deletions tests/orm/nodes/data/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,45 @@ def test_equality(node_type, a, b):
# Test equality comparison with other `BaseType` nodes
assert node_a == node_a_clone
assert node_a != node_b


@pytest.mark.parametrize('numeric_type', (Float, Int))
def test_unary_pos(numeric_type):
"""Test the ``__pos__`` unary operator for all ``NumericType`` subclasses."""

node_positive = numeric_type(1)
node_negative = numeric_type(-1)

assert +node_positive == node_positive
assert +node_negative == node_negative


@pytest.mark.parametrize('numeric_type', (Float, Int))
def test_unary_neg(numeric_type):
"""Test the ``__neg__`` unary operator for all ``NumericType`` subclasses."""

node_positive = numeric_type(1)
node_negative = numeric_type(-1)

assert -node_positive != node_positive
assert -node_negative != node_negative
assert -node_positive == node_negative
assert -node_negative == node_positive
assert -node_negative == node_positive


@pytest.mark.parametrize('numeric_type', (Float, Int))
def test_unary_abs(numeric_type):
"""Test the ``__abs__`` unary operator for all ``NumericType`` subclasses"""

node_positive = numeric_type(1)
node_negative = numeric_type(-1)

# Test positive number
abs_positive = abs(node_positive)
assert abs_positive == node_positive

# Test negative number
abs_negative = abs(node_negative)
assert abs_negative != node_negative
assert abs_positive == abs_negative

0 comments on commit a298c14

Please sign in to comment.