Skip to content

bpo-24234: implement complex.__complex__ #27887

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

Merged

Conversation

mdickinson
Copy link
Member

@mdickinson mdickinson commented Aug 22, 2021

This PR implements the __complex__ method for the complex type. This slightly simplifies the check for whether a Python object can be used as a complex number, and means that that check can be implemented purely in terms of the existence of special methods - an object x can be used as a complex number if its type implements at least one of __index__, __float__ or __complex__.

https://bugs.python.org/issue24234

@mdickinson
Copy link
Member Author

mdickinson commented Aug 22, 2021

Hmm; there's an annoying test_doctest failure:

**********************************************************************
File "/Users/runner/work/cpython/cpython/Lib/test/test_doctest.py", line 671, in test.test_doctest.test_DocTestFinder.non_Python_modules
Failed example:
    816 < len(tests) < 836 # approximate number of objects with docstrings
Expected:
    True
Got:
    False

Given that there have probably been other docstrings added to builtins since this was last touched (e.g., int.bitcount), and that the failures appear to be the result of hitting the upper end of the (816, 836) range, I've bumped the expected numbers up to 820 < len(tests) < 840. But this feels like a rather awkward and fragile test.

Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

@mdickinson
I would like to suggest adding test in test_typing.py

--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1540,6 +1540,7 @@ def __complex__(self):

         self.assertIsSubclass(C, typing.SupportsComplex)
         self.assertNotIsSubclass(str, typing.SupportsComplex)
+        self.assertIsSubclass(complex, typing.SupportsComplex)

mdickinson and others added 2 commits August 22, 2021 15:44
@mdickinson
Copy link
Member Author

I would like to suggest adding test in test_typing.py

Thanks! Done.

…complex' into bpo-24234-implement-complex-complex
@mdickinson mdickinson requested a review from gvanrossum as a code owner August 22, 2021 14:48
Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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


(.oss) ➜  cpython git:(pr/27887) ✗ ./python.exe -m test test_typing.py test_comp
lex -R 3:3
0:00:00 load avg: 3.41 Run tests sequentially
0:00:00 load avg: 3.41 [1/2] test_typing
beginning 6 repetitions
123456
......
0:00:02 load avg: 3.41 [2/2] test_complex
beginning 6 repetitions
123456
......

== Tests result: SUCCESS ==

All 2 tests OK.

Total duration: 5.6 sec
Tests result: SUCCESS

LGTM, no leak was found.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Thanks!

@mdickinson mdickinson merged commit 6082bb5 into python:main Aug 23, 2021
@mdickinson mdickinson deleted the bpo-24234-implement-complex-complex branch August 23, 2021 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants