-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Unable to use typing.Self
with Enum
s
#18345
Comments
I think mypy's behavior is correct here. See this pyright issue for details. |
I mean, In my real world use case I have an enum with many class methods that return |
An annotation of |
In this specific case, it's not possible to subclass |
I made the same argument in #16558 (comment), but others argued that doing this would require a change to the typing spec. I still think it would be a useful improvement that would make life more ergonomic for users of enums without any detriment to type safety (but I haven't got the time/energy to push for a change to the spec on this point right now, if it does indeed require a chnage to the spec) |
I found a workaround that will let me use from enum import *
from typing import *
T = TypeVar("T")
class _FooImpl:
@classmethod
def get_bar(cls) -> Self:
return cls.BAR # type: ignore
class Foo(_FooImpl, Enum):
BAR = "bar"
reveal_type(Foo.get_bar()) although it would be nicer if we could just define the |
plus, it requires to use a |
Bug Report
I expect to be able to use
typing.Self
for classmethods of anEnum
subclass, but mypy fails the type check for such methods.To Reproduce
https://mypy-play.net/?mypy=master&python=3.12&gist=9f529a65422ee83aba0312c91e74a935
Expected Behavior
Type checking should pass.
Actual Behavior
I get the following error:
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: