Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use __new__ instead of __init__ for some builtin classes #4555

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
merged 7 commits into from
Sep 30, 2020

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Sep 17, 2020

Closes #4514 #2630 #2686

I tested this against our internal codebases and against the example in #1464 and it seems to works fine.

@srittau
Copy link
Collaborator Author

srittau commented Sep 17, 2020

@hauntsaninja There is something strange going on with the mro here, see the comment in the stubtest whitelist. In my opinion, this special case (quirk in Python?) should be handled by stubtest (or mypy?), instead of putting the enums on the whitelist. What do you think?

An alternative is to override __new__ explicitly in IntEnum, IntFlag, and another enum I forgot in the stubs. That doesn't feel right to me, though, as the implementation doesn't do that either.

@gvanrossum
Copy link
Member

I'm guessing that the Enum metaclass plunks a reference to Enum.__new__ in the __dict__ of all new Enum subclasses. I think here: https://github.com/python/cpython/blob/a33f2c2bae759fc9d06e1c032fd2026135f2df45/Lib/enum.py#L273

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Oh interesting. I'd probably agree that this should be handled by stubtest / mypy.

@srittau
Copy link
Collaborator Author

srittau commented Sep 18, 2020

@gvanrossum @hauntsaninja

  1. Good catch!
  2. Yikes!
  3. In that case I am not sure that stubtest needs fixing. Since every enum sub-class basically gets Enum.__new__ assigned to it, manually overriding it in sub-classes that derive from both int/str and Enum might be the correct solution.

@hauntsaninja
Copy link
Collaborator

I was a little worried about this PR, so I ran mypy_primer:

(mypy_primer) ~/dev/mypy_primer master λ python -m primer --new v0.782 --old v0.782 --custom-typeshed-repo ~/dev/typeshed --new-typeshed dundernew --old-typeshed 98667b18 -o concise     
+ isort/output.py:561: error: "_LineWithComments" has no attribute "comments"

- src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
+ src/aiortc/mediastreams.py:22: note:     def __new__(cls, x: Union[str, SupportsInt, _SupportsIndex] = ...) -> int

+ sphinx/builders/html/__init__.py:94: error: unused 'type: ignore' comment
+ sphinx/builders/html/__init__.py:117: error: unused 'type: ignore' comment

Seems fine!

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.

Perhaps declare a __new__ instead of an __init__ for str and other similar types
3 participants