Skip to content

Use __new__ instead of __init__ for some builtin classes - #4555

Merged
hauntsaninja merged 7 commits into
python:masterfrom
srittau:new
Sep 30, 2020
Merged

hauntsaninja merged 7 commits into
python:masterfrom
srittau:new

Conversation

@srittau

@srittau srittau commented Sep 17, 2020

Copy link
Copy Markdown
Collaborator

Closes #4514 #2630 #2686

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

@srittau

srittau commented Sep 17, 2020

Copy link
Copy Markdown
Collaborator Author

@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
Copy Markdown
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

@hauntsaninja hauntsaninja left a comment •

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.

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

@srittau

srittau commented Sep 18, 2020

Copy link
Copy Markdown
Collaborator Author

@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
Copy Markdown
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