python / python/mypy

Incorrect __post_init__ order when KW_ONLY used

未关闭
#17,564 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

from dataclasses import dataclass, InitVar, KW_ONLY


@dataclass
class A:
    _: KW_ONLY
    x: InitVar[int]

    def __post_init__(self, x: int) -> None:
        pass


@dataclass
class C(A):
    y: InitVar[str]

    def __post_init__(self, x: int, y: str) -> None:  # Correct order.
        super().__post_init__(x)

gives

a.py:19: error: Argument 2 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "str"  [override]
        def __post_init__(self, x: int, y: str) -> None:
                                ^~~~~~
a.py:19: error: Argument 3 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "int"  [override]
        def __post_init__(self, x: int, y: str) -> None:

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现报告中的 Python 示例,并确认涉及 post_init 的两个 override 错误。然后跟踪 mypy 对 KW_ONLY 和 InitVar 的 dataclass 处理,以确定参数顺序是在哪里推导出来的。当示例能够在没有这些错误的情况下通过类型检查,同时保留所展示的 post_init 顺序时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
devtools
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。