Skip to content

Commit e8e74ba

Browse files
authored
Use object for value annotation in Enum.__new__ (#7752)
This pull request reverts part of #2539 that brought back a bug discussed in python/mypy#5788 and initially fixed in #2539 In short, the issue was that the following program always resulted in an error when running mypy with the `--disallow-any-expr` flag: from enum import Enum class MyEnum(Enum): FOO = 1 BAR = 2 blah = MyEnum # Error here The root issue was that because the signature of Enum's `__new__` method was typed as: def __new__(self: Type[T], value: Any) -> T: ... This caused mypy to decide that the type of `MyEnum` was `(Any) -> MyEnum`. This is correct based on the current type signature, but unfortunately means that it becomes impossible to ever use enums with the `--disallow-any-expr` flag.
1 parent 7e7562b commit e8e74ba

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

stdlib/enum.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ class Enum(metaclass=EnumMeta):
155155
def _missing_(cls, value: object) -> Any: ...
156156
@staticmethod
157157
def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any: ...
158-
def __new__(cls: type[Self], value: Any) -> Self: ...
158+
# It's not true that `__new__` will accept any argument type,
159+
# so ideally we'd use `Any` to indicate that the argument type is inexpressible.
160+
# However, using `Any` causes too many false-positives for those using mypy's `--disallow-any-expr`
161+
# (see #7752, #2539, mypy/#5788),
162+
# and in practice using `object` here has the same effect as using `Any`.
163+
def __new__(cls: type[Self], value: object) -> Self: ...
159164
def __dir__(self) -> list[str]: ...
160165
def __format__(self, format_spec: str) -> str: ...
161166
def __hash__(self) -> Any: ...

0 commit comments

Comments
 (0)