python / python/typeshed

Wrong Signature for Enum with Specific Dataclass Mixin Scenario

Open
#15,908 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

Note: I was thinking about putting this on pyright but it said standard library problems are more suited here, so I'll give it a shot.

I was trying to code an enum with a dataclass mixin where each value is overridden through the first string variable for easier referencing (rather than a dict from str to enum).

from dataclasses import dataclass
from enum import Enum, auto

@dataclass(frozen=True)
class SampleMixin:
    a_string: str
    a_number: int

class SampleEnum(SampleMixin, Enum):
    def __new__(cls, value: str, *_):
        obj = object.__new__(cls)
        obj._value_ = value

        return obj

    foo = "foo", 1
    bar = "bar", 2

class SampleEnum2(Enum):
    foo = auto()
    bar = auto()

baz = SampleEnum("foo")
qux = SampleEnum2("foo")

baz still gets SampleEnum.foo right, but the function signature is incorrectly identified. Ideally, it should be closer to SampleEnum2's function signature.

Image

I'm open to workarounds at the meantime. The code runs correctly, it's just that the inferred signature I got was wrong.

Code sample in pyright playground.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python reproduction and the linked pyright playground, comparing the inferred signatures for SampleEnum and SampleEnum2. Trace the relevant standard-library enum and dataclass typing definitions in typeshed, and confirm completion when the dataclass-mixin case produces the intended enum constructor signature without changing runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.