Issue with `attrs` custom `__init__` defined

未关闭 适合新手
#20,818 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

从 mypy/plugins/attrs.py 第 345 行附近开始,跟踪当一个类已经定义了 init 时,attrs.define 如何确定生成的构造函数。复现 issue 中的 A 和 B 示例,然后添加回归覆盖,证明两个自定义构造函数都受到遵循,并且会报告预期的参数类型错误。

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

描述

bug topic-attrs

Bug Report
__init__ definition is ignored when generating attrs.define custom version.

To Reproduce


import attrs


@attrs.define
class A:
    x: int

    def __init__(self, x: str) -> None: ...


@attrs.define(init=False)
class B:
    x: int

    def __init__(self, x: str) -> None: ...

Expected Behavior

A(1) # error: Argument 1 to "A" has incompatible type "int"; expected "str"  [arg-type]
A("1") 
B(1) # error: Argument 1 to "B" has incompatible type "int"; expected "str"  [arg-type]
B("1")

Actual Behavior

A("1") # error: Argument 1 to "A" has incompatible type "str"; expected "int"  [arg-type]
B(1) # error: Argument 1 to "B" has incompatible type "int"; expected "str"  [arg-type]
B("1")

Your Environment

  • Mypy version used: mypy 1.20.0+dev
  • Mypy command-line flags: file.py --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): NA
  • Python version used: Python 3.13.9

Notes
I think the issue comes from:
https://github.com/python/mypy/blob/a86f7e3ab0ce17b9839239951430348e69c3284b/mypy/plugins/attrs.py#L345
as it does not check whether the __init__ method exists, it only checks that init=False when attrs.define is called.

主要语言
Python
星标
20.6k
派生
3.3k
平均合并
1 天 18 小时
30 天内合并 PR
54

贡献指南

打开贡献指南

从这里开始

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

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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