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

Annotating Enum attributes with Final causes surprising type violations #8543

Closed
yangskyboxlabs opened this issue Jul 25, 2024 · 2 comments
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@yangskyboxlabs
Copy link

When Enum attributes are annoted with typing.Final, the "Type annotations are not allowed for enum members" warning is not raised as it would be for similar invalid annotations. Such attributes also also treated as having the annotated type instead of its Enum derived type.

For the code below:

from enum import Enum
from typing import Final


class BadEnum(Enum):
    VAL = "val"
    STR: str = "str"
    FINAL: Final = "final"


def get_enum(val: str) -> BadEnum:
    match val:
        case "val":
            return BadEnum.VAL
        case "str":
            return BadEnum.STR
        case "final":
            return BadEnum.FINAL
        case _:
            raise ValueError


assert type(get_enum("str")) is BadEnum
assert type(get_enum("final")) is BadEnum

pyright, in strict mode, will raise the following:

d:\pyright_scratch\enum_test.py
  d:\pyright_scratch\enum_test.py:7:5 - error: Type annotations are not allowed for enum members (reportGeneralTypeIssues)
  d:\pyright_scratch\enum_test.py:16:20 - error: Expression of type "str" is incompatible with return type "BadEnum"
    "str" is incompatible with "BadEnum" (reportReturnType)
  d:\pyright_scratch\enum_test.py:18:20 - error: Expression of type "Literal['final']" is incompatible with return type "BadEnum"
    "Literal['final']" is incompatible with "BadEnum" (reportReturnType)
3 errors, 0 warnings, 0 informations

To summarize:

  1. FINAL attribute should also be flagged by reportGeneralTypeIssues?
  2. BadEnum.STR and BadEnum.FINAL should still be treated as BadEnum instead of str and Literal['final']

Issue noticed after updating opentelemetry-semantic-convention to a version that contains open-telemetry/opentelemetry-python#3966

@yangskyboxlabs yangskyboxlabs added the bug Something isn't working label Jul 25, 2024
erictraut added a commit that referenced this issue Jul 25, 2024
…ute. This should be considered invalid. This addresses #8543.
@erictraut
Copy link
Collaborator

Here's the applicable wording from the typing spec:

Members defined within an enum class should not include explicit type annotations. Type checkers should infer a literal type for all members. A type checker should report an error if a type annotation is used for an enum member because this type will be incorrect and misleading to readers of the code.

Based on that wording, an annotation of Final should probably be flagged as an error. I say "probably" because the spec says "type annotation", which implies that there's a supplied type expression. I authored that part of the spec, and when I wrote that sentence, I didn't consider that anyone would attempt to use a "naked" Final annotation with no type expression. In retrospect, I should have used the term "annotation expression" or just "annotation" to make it clear.

In any case, you shouldn't use Final in this context if you want your code to type check without errors.

I'll update pyright to emit an error for a Final annotation within an enum. This will be included in the next release.

erictraut added a commit that referenced this issue Jul 26, 2024
…ute. This should be considered invalid. This addresses #8543. (#8544)
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jul 26, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.374.

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
Projects
None yet
Development

No branches or pull requests

2 participants