gh-109218: Refactor tests for the complex() constructor#119635
gh-109218: Refactor tests for the complex() constructor#119635serhiy-storchaka merged 9 commits intopython:mainfrom
Conversation
* Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases.
skirpichev
left a comment
There was a problem hiding this comment.
LGTM, just few nitpicks. No test coverage regressions.
Lib/test/test_complex.py
Outdated
| class MockIndex: | ||
| def __init__(self, value): | ||
| self.value = value | ||
| def __index__(self): | ||
| return self.value | ||
|
|
||
| class MockFloat: | ||
| def __init__(self, value): | ||
| self.value = value | ||
| def __float__(self): | ||
| return self.value | ||
|
|
||
| class ComplexSubclass(complex): | ||
| pass | ||
|
|
||
| class MockComplex: | ||
| def __init__(self, value): | ||
| self.value = value | ||
| def __complex__(self): | ||
| return self.value | ||
|
|
There was a problem hiding this comment.
Note, that similar code could be shared also with test_long.py and test_float.py. That refactoring of the scope here, but maybe in this pr you could put these classes somewhere in the support module? // see #110956
There was a problem hiding this comment.
I proposed this in #84310. But for some reasons this idea was not liked, so now we need to duplicate the code in many test files. cc @rhettinger
| self.assertFloatsAreIdentical(z1.imag, 0.0) | ||
| self.assertFloatsAreIdentical(z2.imag, -0.0) |
There was a problem hiding this comment.
This pattern is quite common. Maybe there should be helper method like:
def assertComplexesAreIdentical(self, x, y):
self.assertFloatsAreIdentical(x.real, y.real)
self.assertFloatsAreIdentical(x.imag, y.imag)There was a problem hiding this comment.
Yes, I seen this in your PR, and this looks helpful. There are also similar methods (with different names) in other test files. Perhaps we could move the best implementation in a common file and use it everywhere.
Lib/test/test_complex.py
Outdated
| (1, 0+0j), | ||
| ) | ||
|
|
||
| class MockIndex: |
There was a problem hiding this comment.
Naming nitpick: the Mock prefix on these classes doesn't feel appropriate: nothing's being mocked here. I'd probably go with something like ImplementsIndex or HasIndex.
There was a problem hiding this comment.
I once added common FakePath class which implements the __path__ method, and it is now used in many tests for the path protocol. I proposed to add similar FakeIndex, FakeFloat, etc, but Raymond rejected this idea. Here I use the Mock prefix which looks more neutral and test specific than Fake. Different tests currently use similar classes with different names. The most common prefix is perhaps My. Prefix Supports cannot be used because it can conflict with typing.SupportIndex, etc. Suffix Like can conflict with os.PathLike.
I see that several tests use the With prefix: WithStr, WithRepr, WithIterAnext, etc. What do you think about WithIndex?
There was a problem hiding this comment.
WithIndex works. I'm also happy for this to be left as-is - it's not a strong objection, just a niggling feeling that the name isn't quite right.
Lib/test/test_complex.py
Outdated
| check(complex(4.25, imag=1.5), 4.25, 1.5) | ||
|
|
||
| # check that the sign of a zero in the real or imaginary part | ||
| # is preserved when constructing from two floats. (These checks |
There was a problem hiding this comment.
We could drop the parenthetical here, given that we're now requiring IEEE 754 for CPython.
Lib/test/test_complex.py
Outdated
| check(complex(MockIndex(42)), 42.0, 0.0) | ||
| check(complex(MockIndex(42), 1.5), 42.0, 1.5) | ||
| check(complex(1.5, MockIndex(42)), 1.5, 42.0) | ||
| self.assertRaises(OverflowError, complex, MockIndex(2**2000)) |
There was a problem hiding this comment.
Interesting - looks like these tests were already assuming IEEE 754 before.
| check(complex(complex0(1j)), 0.0, 42.0) | ||
| with self.assertWarns(DeprecationWarning): | ||
| self.assertEqual(complex(complex1(1j)), 2j) | ||
| check(complex(complex1(1j)), 0.0, 2.0) |
There was a problem hiding this comment.
@serhiy-storchaka, maybe it's time to drop this? Ditto for float's.
Returning non-complex(float) values was deprecated in v3.7.
There was a problem hiding this comment.
This is a different issue.
|
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…GH-119635) * Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases. (cherry picked from commit bf098d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…GH-119635) * Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases. (cherry picked from commit bf098d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
|
GH-119795 is a backport of this pull request to the 3.13 branch. |
|
GH-119796 is a backport of this pull request to the 3.12 branch. |
…9635) (GH-119796) * Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases. (cherry picked from commit bf098d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…GH-119635) * Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases.
…GH-119635) * Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases.
…9635) (GH-119795) * Share common classes. * Use exactly representable floats and exact tests. * Check the sign of zero components. * Remove duplicated tests (mostly left after merging int and long). * Reorder tests in more consistent way. * Test more error messages. * Add tests for missed cases. (cherry picked from commit bf098d4) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Uh oh!
There was an error while loading. Please reload this page.