Special handling of descriptor field like in dataclasses
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start by reading the descriptor-typed fields documentation linked in the issue, then review the earlier discussions in #881 and #1108. Done means attrs supports descriptor fields without losing descriptor behavior during initialization or assignment, matching the documented dataclasses example.
由索引模型根据 Issue 内容生成。
描述
dataclass dataclasses handle descriptor fields differently from other fields, so the descriptor type is not lost when assigning:
https://docs.python.org/3/library/dataclasses.html#descriptor-typed-fields
This does not work in attrs, it's a pity because it could be used as an alternative to converters called with the instance (#1108).
Previous discussion about this was not very conclusive (#881).
# Example from python docs
from dataclasses import dataclass
from attrs import define
class IntConversionDescriptor:
def __init__(self, *, default):
self._default = default
def __set_name__(self, owner, name):
self._name = "_" + name
def __get__(self, obj, type):
if obj is None:
return self._default
return getattr(obj, self._name, self._default)
def __set__(self, obj, value):
setattr(obj, self._name, int(value))
# dataclass: WORKS AS EXPECTED
@dataclass
class InventoryItem:
quantity_on_hand: IntConversionDescriptor = IntConversionDescriptor(default=100)
i = InventoryItem()
print(i.quantity_on_hand) # 100
i.quantity_on_hand = 2.5 # calls __set__ with 2.5
print(i.quantity_on_hand) # 2
# attrs: DOES NOT WORK
@define
class InventoryItem2:
quanttity_on_hand: IntConversionDescriptor = IntConversionDescriptor(default=100)
i2 = InventoryItem2()
print(i2.quantity_on_hand) # <__main__.IntConversionDescriptor object at 0x78c3a12a1250>
i2.quantity_on_hand = 2.5 # set InventoryItem2 attribute to a float, erasing the descriptor
print(i2.quantity_on_hand) # 2.5
- 主要语言
- Python
- 星标
- 5.9k
- 派生
- 499
- 平均合并
- 3 天 12 小时
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/attrs 的其他 Issue
-
Docs: `attrs.filters` is missing stated examples可能已有人在做 @feiiiiii5 于 12 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
python-attrs/attrs#1627 ·
维护者通常 1 天内回复
-
slots declared on attrs-decorated class are ignored/lost可能已有人在做 @anshurajbisoyi98-ctrl 于 4 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 68/100
python-attrs/attrs#1637 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 20/100
python-attrs/attrs#1620 ·
维护者通常 1 天内回复
-
Incorrect annotation for forward reference in generated constructor可能已有人在做 @MohammedAnasNathani 于 69 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
python-attrs/attrs#1596 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
Decorated functions do not work with inheritance可能已有人在做 @sb123sb123 于 15 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 35/100
python-attrs/attrs#1549 · 3 条评论 ·
维护者通常 1 天内回复
查看 python-attrs/attrs 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
-
Task
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
war-and-code/dircue#200 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 87/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复