Skip to content

bpo-33284 Add to a unit test to improve coverage for numbers.py #6480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
88 changes: 88 additions & 0 deletions Lib/test/test_abstract_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import operator
import unittest
from random import random
from numbers import Complex, Real, Rational, Integral

class TestNumbers(unittest.TestCase):
Expand Down Expand Up @@ -40,5 +41,92 @@ def test_complex(self):
self.assertRaises(TypeError, int, c1)


class TestAbstractNumbers(unittest.TestCase):
def testComplex(self):

class SubComplex(Complex):
def __complex__(self):
pass

@property
def real(self):
return self._imag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be return self._real, right?


@property
def imag(self):
return self._real
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same here with self._imag.


def __add__(self, other):
pass

def __radd__(self, other):
pass

def __neg__(self):
pass

def __pos__(self):
pass

def __mul__(self, other):
pass

def __rmul__(self, other):
pass

def __truediv__(self, other):
pass

def __rtruediv__(self, other):
pass

def __pow__(self, exponent):
pass

def __rpow__(self, base):
pass

def __abs__(self):
pass

def conjugate(self):
pass

def __init__(self, r, i):
self.real = r
self.imag = i

def __eq__(self, other):
return isinstance(other, SubComplex) and self.imag == other.imag and self.real == other.real

@imag.setter
def imag(self, value):
self._imag = value

@real.setter
def real(self, value):
self._real = value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These setters would be better placed alongside the getters.


sc = SubComplex(0,0)
self.assertIsInstance(sc, Complex)

nonInstances = [None, "hat", lambda x: x + 1]

for nonInstance in nonInstances:
self.assertNotIsInstance(nonInstance, Complex)

self.assertFalse(SubComplex.__bool__(0))
for i in range(100):
self.assertTrue(SubComplex.__bool__(random() + 1e-6))
x = random()
y = random()
self.assertEqual(SubComplex.__sub__(x, y), x - y)
self.assertEqual(SubComplex.__rsub__(x, y), - x + y)
sc1 = SubComplex(x, y)
sc2 = SubComplex(x, y)
self.assertEqual(sc1, sc2)
self.assertNotEqual(x, y + 1e-6)


if __name__ == "__main__":
unittest.main()