Skip to content

Fix complex constructor#8473

Merged
Akuli merged 3 commits intopython:masterfrom
smlerman:python-typeshed-8440
Aug 2, 2022
Merged

Fix complex constructor#8473
Akuli merged 3 commits intopython:masterfrom
smlerman:python-typeshed-8440

Conversation

@smlerman
Copy link
Copy Markdown
Contributor

@smlerman smlerman commented Aug 2, 2022

Fix for issue #8440

smlerman and others added 3 commits July 29, 2022 15:52
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
Add `SupportsFloat` to one-parameter overload, per python#8440 (comment)
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 2, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja changed the title Python typeshed 8440 Improve complex.__new__ Aug 2, 2022
@Akuli Akuli changed the title Improve complex.__new__ Fix complex constructor Aug 2, 2022
@Akuli
Copy link
Copy Markdown
Collaborator

Akuli commented Aug 2, 2022

Lol, we both changed the title :)

Copy link
Copy Markdown
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

Thanks! I used a script and all Python versions 3.6 to 3.10 to verify this.

my script
class SupportsFloat:
    def __float__(self):
        return 1.0

class SupportsComplex:
    def __complex__(self):
        return 1+2j

class SupportsIndex:
    def __index__(self):
        return 1

objects = {
    "SupportsFloat": SupportsFloat(),
    "SupportsComplex": SupportsComplex(),
    "SupportsIndex": SupportsIndex(),
    "float": 1.0,
    "complex": 1+2j,
    "int": 1,
}

#single_arg = []
#for name, obj in objects.items():
#    try:
#        complex(obj)
#    except Exception:
#        pass
#    else:
#        single_arg.append(name)
#print(single_arg)

double_arg = []
for name1, obj1 in objects.items():
    for name2, obj2 in objects.items():
        try:
            complex(obj1, obj2)
        except Exception:
            pass
        else:
            double_arg.append((name1, name2))

import pprint
pprint.pprint(double_arg)

else:
@overload
def __new__(
cls: type[Self], real: complex | SupportsComplex | SupportsFloat = ..., imag: complex | SupportsFloat = ...
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also accepts int, but that's included implicitly in complex.

@overload
def __new__(
cls: type[Self],
real: complex | SupportsComplex | SupportsFloat | SupportsIndex = ...,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I find it weird that SupportsComplex doesn't include complex.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're not the only one — I believe this specific oddity was why they added complex.__complex__ in 3.11 :)

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.

3 participants