slots declared on attrs-decorated class are ignored/lost
维护者通常 2 天内回复
@anshurajbisoyi98-ctrl 已经在做这个了。
开始于 2026年10月3日。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 68/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
Start by reproducing the two examples in the issue and compare how attrs processes slots declared on the class with slots inherited from a base class. Trace the slot-processing entry point and add a regression test for preserving a class-declared private slot; done means assigning that slot works while it remains outside attrs fields and generated methods.
由索引模型根据 Issue 内容生成。
描述
If a slotted attrs class declares its own slots, those slots are lost/ignored when the class is processed by attrs. E.g.
@attrs.define
class A:
__slots__ = ("private",)
public: str = "foo"
a = A()
a.private = 1
produces the error TypeError: descriptor 'private' for 'A' objects doesn't apply to a 'A' object.
However, declaring an attrs class that subclasses a class that defines slots, the parent class's slots are preserved. E.g.
class Base:
__slots__ = ("private",)
@attrs.define
class B(Base):
public: str = "bar"
b = B()
b.private = 1
sets the slots attribute without issue.
Is this intentional behavior or dictated by the design of attrs? I've looked at the code and it looks like it goes out and gets slots defined on base classes and adds them to the slots defined on the new class. Using that same method, it doesn't look like it would be hard to preserve slots declared on the slotted attrs class itself, but I haven't tried to code it and I don't know if it would break something I'm not seeing. (Note that declaring __slots__ on a class decorated with @attrs.define(slotted=False) doesn't work for other reasons, so this question/issue really only applies to slotted attrs classes).
It can often be useful to have private attributes on a class that may be used for internal instance management but aren't really part of the data model represented by the class and should be ignored by attrs--not included in the init or hash or equality functions. This can, of course, be accomplished by declaring the attribute with init=False, hash=False, and eq=False, but the attribute still shows up in the collections returned by attrs.fields or attrs.fields_dict and is serialized by attrs.asdict (which, similarly, can be worked around with a filter). Declaring slots would be a simple way to allow an instance to have attributes that attrs is entirely unaware of. Using a base class is a workaround, but it would be preferable not to have to create a separate base class or mixin solely to add extra slots.
- 主要语言
- Python
- 星标
- 5.9k
- 派生
- 501
- 平均合并
- 4 天 18 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/attrs 的其他 Issue
-
Docs: `attrs.filters` is missing stated examples可能已有人在做 @feiiiiii5 于 23 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
python-attrs/attrs#1627 ·
维护者通常 2 天内回复
-
难度 5/5 一周以上 新手友好度 20/100
python-attrs/attrs#1620 ·
维护者通常 2 天内回复
-
Incorrect annotation for forward reference in generated constructor可能已有人在做 @MohammedAnasNathani 于 80 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
python-attrs/attrs#1596 · 2 条评论 · 2 个 reaction ·
维护者通常 2 天内回复
-
Decorated functions do not work with inheritance可能已有人在做 @sb123sb123 于 27 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 35/100
python-attrs/attrs#1549 · 3 条评论 ·
维护者通常 2 天内回复
-
难度 4/5 3-5 天 新手友好度 38/100
python-attrs/attrs#1543 · 2 条评论 ·
维护者通常 2 天内回复
查看 python-attrs/attrs 的全部 Issue
相似的 Issue
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
ianktoo/project-monitor#14 ·
-
security
难度 2/5 1-3 小时 新手友好度 84/100
btclib-org/btclib-wallet#478 ·
维护者通常 1 天内回复
-
security
难度 2/5 1-3 小时 新手友好度 75/100
btclib-org/btclib#2727 ·
维护者通常 1 天内回复
-
good first issue lead variant:container
难度 2/5 1-3 小时 新手友好度 76/100
radialmonster/orf-cleanroom#10 ·
维护者通常 1 天内回复
-
gdsa opencode: keep-alive cleanup trap fails with 'keepalive_pid: unbound variable'可能已有人在做 @darribas 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100