Skip to content
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

Enum Literal Handling Regression #7926

Closed
max-muoto opened this issue May 15, 2024 · 3 comments
Closed

Enum Literal Handling Regression #7926

max-muoto opened this issue May 15, 2024 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working regression

Comments

@max-muoto
Copy link
Contributor

max-muoto commented May 15, 2024

To preface, apologies if there's a simpler reproducible example here, mostly trying to replicate what's happening across modules in my codebase. In Pyright 1.1.362, something like this would have been fine:

import enum
from typing import TypeAlias

class MyEnum(enum.StrEnum):
    ARG_1 = "ARG_1"
    ARG_2 = "ARG_1"
    

MyEnumAlias: TypeAlias = MyEnum

def get_enum() -> MyEnumAlias: ...

z = get_enum()

def foo(enum: MyEnum): ...

foo(z.ARG_1)

That being, we alias an enum, and pass in value off the aliased type, now in 1.1.362, you'll get a warning when trying to pass in ARG_1:

In this example, it's obviously pointless to alias, in reality, we have an enum setting that's we're aliasing to change the typing of, think, MY_ENUM as MyEnum, for a wrapper we have on some project settings.

Pyright Playground

Version: 1.1.363

@max-muoto max-muoto added the bug Something isn't working label May 15, 2024
@max-muoto max-muoto changed the title Regression with Enum Literal Handling Enum Literal Handling Regression May 15, 2024
@erictraut
Copy link
Collaborator

I agree this is a bug, but I think the code sample is suspect and arguably should be considered a bug. Accessing enum members from an instance of an enum happens to work with Python's enum class, but it's probably not intended.

Consider the following. Does this really make sense?

print(MyEnum.ARG_1.ARG_2.ARG_1.ARG_2.ARG_1)

Perhaps your code sample intends to return type[MyEnumAlias] from get_enum?

I'll update pyright to match the runtime behavior even though I think it doesn't make much sense in this case.

erictraut added a commit that referenced this issue May 16, 2024
…ng an enum member from an instance of an enum class. This addresses #7926.
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label May 16, 2024
@max-muoto
Copy link
Contributor Author

I agree this is a bug, but I think the code sample is suspect and arguably should be considered a bug. Accessing enum members from an instance of an enum happens to work with Python's enum class, but it's probably not intended.

Consider the following. Does this really make sense?

print(MyEnum.ARG_1.ARG_2.ARG_1.ARG_2.ARG_1)

Perhaps your code sample intends to return type[MyEnumAlias] from get_enum?

I'll update pyright to match the runtime behavior even though I think it doesn't make much sense in this case.

You're right! My code did mean to do that, unfortunately we're accessing some configuration settings through Django typed as Any, so didn't catch this. Thanks.

@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.364.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working regression
Projects
None yet
Development

No branches or pull requests

2 participants