Wrong Signature for Enum with Specific Dataclass Mixin Scenario
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 82
描述
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.
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Python 复现和链接的 pyright playground 开始,比较为 SampleEnum 和 SampleEnum2 推断出的签名。跟踪 typeshed 中标准库 enum 和 dataclass 的相关 typing 定义,并在 dataclass-mixin 情况生成预期的 enum 构造函数签名且不改变运行时行为时确认完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- developer-experience
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100