[ty] Treat custom enum __new__ values as dynamic#25136
Merged
Merged
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 89.36%. The percentage of expected errors that received a diagnostic held steady at 85.49%. The number of fully passing files held steady at 88/134. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
trio
sphinx
flake8
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-assignment |
3 | 0 | 0 |
| Total | 3 | 0 | 0 |
Raw diff:
mypy (https://github.com/python/mypy)
+ mypy/typeshed/stdlib/ssl.pyi:268:5 error[invalid-assignment] Enum member `SERVER_AUTH` is incompatible with `__new__`
+ mypy/typeshed/stdlib/ssl.pyi:269:5 error[invalid-assignment] Enum member `CLIENT_AUTH` is incompatible with `__new__`
typeshed-stats (https://github.com/AlexWaygood/typeshed-stats)
+ tests/test_gather.py:114:13 error[invalid-assignment] Enum member `NOT_A_STRING` is incompatible with `__new__`| reveal_type(Planet2.MERCURY._value_) # revealed: Any | ||
| ``` | ||
|
|
||
| ### `__new__` without `_value_` annotation |
Collaborator
There was a problem hiding this comment.
Should we test for __new__ coming from a parent class? The code makes explicit allowance for inherited __new__.
| class Base(Enum): | ||
| def __new__(cls, value: str, connector_id: int = 0) -> "Base": | ||
| obj = object.__new__(cls) | ||
| obj._value_ = value |
Collaborator
There was a problem hiding this comment.
Do we give any errors if this is set to a different type instead? Probably not essential but would be nice to note.
I tried this sample in multiplay, just changing the annotation on the __new__ to value: int, and only pyright detects the issue.
Member
Author
There was a problem hiding this comment.
It appears not today, but I can fix that in a follow-up.
JelleZijlstra
approved these changes
May 13, 2026
a2c5b26 to
8226155
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
For enums with a custom
__new__, the member RHS is input to the constructor, but__new__can assign_value_independently. We now treat such values as dynamically typed, unless_value_is explicitly annotated.To quote @JelleZijlstra: "Looks right and fairly simple, behavior matches pyright and mypy."