-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Types for complex.__new__ should be any numeric type #8440
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
Comments
The docs for
So your analysis seems correct. PR welcome! |
I tried changing |
Yeah, that's expected (see the resources I laid out here for why For the first argument, the docs state:
That means that the first argument should be For the second argument, it seems >>> class Foo:
... def __index__(self):
... return 5
...
>>> complex(1, Foo())
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: complex() second argument must be a number, not 'Foo'
>>> class Foo:
... def __complex__(self):
... return 5
...
>>> complex(1, Foo())
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: complex() second argument must be a number, not 'Foo'
>>> class Foo:
... def __float__(self):
... return 5
...
>>> complex(1, Foo())
(1+5j) |
|
Actually, looks like that's only the case for when you only provide a single argument to |
This is more complicated than I realised; I can prepare the PR if you like :) |
I was going to ask what Python version you were using, because I was getting different results with your code, then I saw your comment about using 3.7. There's already a one-parameter overload declared, which has |
complex(1, FooSupportsIndex()) only works in 3.8+. Is it worth putting in a version check with different declarations for <3.8 and >=3.8? |
Yes, absolutely! And, good catch! |
python_typeshed_8440.py.txt |
For some bizarre reason, Python gives |
Some aspects of Python's data model are a little rough around the edges :) |
I assume the declaration here should match Python's actual behavior, and not what it probably should be? |
Yes, absolutely. "What the behaviour should be" is the question you ask over at the CPython repo, not at typeshed :) |
For two-parameter overload: - Change first parameter to complex | SupportsComplex | SupportsFloat - Change second parameter to complex | SupportsFloat (Python doesn't allow SupportsComplex for the second parameter, though it isn't clear why) - Add SupportsIndex to both parameters for Python >= 3.8 For one-parameter overload: - Remove SupportsIndex for Python < 3.8
Take a look at smlerman@bc74659 and let me know if that looks right. |
That looks great! The only thing to change is that For the two-argument overload, it is utterly bizarre that |
The C code (for 3.12) is here: https://github.com/python/cpython/blob/cd26595232ac1b5061460d5949d5204c90287c1c/Objects/complexobject.c#L898. It indeed uses |
Add `SupportsFloat` to one-parameter overload, per python#8440 (comment)
Just added smlerman@070a113 |
Looks great! Please feel free to submit a PR with the fix :) |
The types for the arguments to
complex.__new__
should be any numeric type, not just float.This code is valid, but the second line reports an error that complex is incompatible with float.
typeshed/stdlib/builtins.pyi
Line 368 in f550c24
The text was updated successfully, but these errors were encountered: