Skip to content

False positive: "Object of class X has no attribute value" for mixed-type Enum members #755

@am1ter

Description

@am1ter

Describe the Bug

Pyrefly incorrectly reports that enum members of mixed-type enums (inheriting from both a primitive type and Enum) don't have a value attribute.

Expected Behavior
Mixed-type enum members should be recognized as having the value attribute, as they inherit from Enum.

Actual Behavior
Pyrefly reports errors:
Object of class 'str' has no attribute 'value'
Object of class 'int' has no attribute 'value'

Minimal Reproduction Case

from enum import Enum

class StrEnum(str, Enum):
    FOO = "FOO"
    DEFAULT = "DEFAULT"
    
    @classmethod
    def normalize(cls, val: str) -> str:
        try:
            return cls(val).value  #  Pyrefly error: Object of class `str` has no attribute `value`
        except ValueError:
            return cls.DEFAULT.value

class IntEnum(int, Enum):
    FOO = 1
    DEFAULT = 0
    
    @classmethod
    def normalize(cls, val: int) -> int:
        try:
            return cls(val).value  # Pyrefly error: Object of class `int` has no attribute `value`
        except ValueError:
            return cls.DEFAULT.value

Context

  • cls(val) returns an enum member (e.g., StrEnum.FOO)
  • This code works correctly at runtime and passes other type checkers like mypy and pyright
  • python version is 3.12.6

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBApgdgV2gSwgBzCALlAooiAKEIGMAbAQwGcqoBlLEfJACisYBo8CBKALkJQhUAGIB5MVAC8UAETixswcIAiuEQEEAqgBkAKtLlrNuvUuFRlQgALlqVCDCwALMABMrUNzGBQ4mCAoyZAAvGBZyKi4ANyC+KHYQHigAWgA+BMYBCwtGAE9snKKQJwQQOChIlliyHgA6GoQYIQBiIQAFPJLgMjzYEHAQeLEAIwArGBIcMF87GigAA0SFqGdqPzAoCixGZBGELGaFxpgFzwsYAA8SGDQcADUgptwBzEKiixKsMorIuuNtPoGk8YMQ5rQAJJwLDMCAsZDQriw-ieBSGACMngBpkMAAZPJ5bJQaI4XO5PN5fP4QIFgmEImQolAavEEVhkukoGz3jl8jyPlAvj9KozqkF6idWlBOt1ev1BsNxpNprNibQFmyVmtaP4tjsQHsDkcTmcBVcbncoI8yM9XkNzh8heURVR-upAXpgTbQUA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions