-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
returning an enum variant from a classmethod returning Self
produces an error.
#7798
Comments
The current behavior is correct. If your intent is for |
Huh, but annotating it as I can see how annotating it as |
Let's ignore the fact that it's an class Foo:
@classmethod
def bar(cls) -> Self:
return Foo() One could argue that if There has been an ongoing effort to clarify the typing spec and increase conformance with the spec. I've been diligently fixing bugs in pyright so it passes the conformance test suite. This change was part of that effort. |
Yeah, this makes perfect sense. What's surprising that even if we assumed for a second that enums would support inheritance, in the example, I don't use Now that I'm thinking about that, is it because of the way the variants are created, where they're basically class variables holding instances of the
The reason I used Note that I don't want to bother you too much with this, so it's perfectly fine if you're too busy / have more important things to do, I completely understand, I'm just curious about this. |
This mainly addresses two types of issues: - <microsoft/pyright#7798>: Using `Self` as a return type from `Enum` classmethods isn't supported, the class (name) should be used instead. - <microsoft/pyright#5933>: Mutable field invariance - subclasses shouldn't be able to contravariantly narrow an inherited variable.
This mainly addresses two types of issues: - <microsoft/pyright#7798>: Using `Self` as a return type from `Enum` classmethods isn't supported, the class (name) should be used instead. - <microsoft/pyright#5933>: Mutable field invariance - subclasses shouldn't be able to contravariantly narrow an inherited variable.
Describe the bug
As the title says, when returning a specific enum variant from a classmethod in that enum class, which is annotated with
Self
as the return type, pyright complains with:However, if the classmethod is instead annotated as returning the class type (
-> Foo
for enum classFoo
), no errors are reported.Note that this seems to also be a regression, I found this when upgrading a fairly old project, I'm not sure since which exact version of pyright this started to fail, but I know it wasn't reported as an error on
1.1.326
.Code or Screenshots
Code sample in pyright playground
The text was updated successfully, but these errors were encountered: