用数据类子类覆盖非数据类属性会导致属性错误
Overriding non dataclass attribute with dataclass subclass causes attribute error
给定以下数据模型
from typing import List
class A:
a: List[int] = []
class B(A):
def __init__(self, b: str, a: List[int] = []):
self.b = b
self.a = a
事实
- 福音
A
不能是 dataclass
(这会使问题变得微不足道)
- 我们想通过
B
继承A
- 我们不希望在
A
的实例化时设置参数 a
- 我们希望能够在
B
的实例化时设置参数 a
以下我假设的工作是
from typing import List
from dataclasses import dataclass, field
class A:
a: List[int] = []
@dataclass
class B(A):
b: str
a: List[int]
正在更正错误 ValueError: mutable default <class 'list'> for field babies is not allowed: use default_factory
我得到
from typing import List
from dataclasses import dataclass, field
class A:
a: List[int] = field(default_factory=list)
@dataclass
class B(A):
b: str
a: List[int]
但这会产生以下错误AttributeError: a
如果我对 a
使用整数类型,则以下工作正常,表明理论上我所做的是好的,但我表达不正确:
from typing import List
from dataclasses import dataclass, field
class A:
a: int = 1
@dataclass
class B(A):
b: str
a: int
我在这里做错了什么?我如何让它与 a
一起工作作为 B
中的空列表
我引用了引发错误的 dataclasses
模块中的片段(函数 _process_class
):
# If the class attribute (which is the default value for this
# field) exists and is of type 'Field', replace it with the
# real default. This is so that normal class introspection
# sees a real default value, not a Field.
if isinstance(getattr(cls, f.name, None), Field):
if f.default is MISSING:
# If there's no default, delete the class attribute.
# This happens if we specify field(repr=False), for
# example (that is, we specified a field object, but
# no default value). Also if we're using a default
# factory. The class attribute should not be set at
# all in the post-processed class.
delattr(cls, f.name)
else:
setattr(cls, f.name, f.default)
我认为评论表明实现并不期望它必须处理继承的属性。我认为这意味着只能继承处理过的属性,即它们必须来自基础数据类。
给定以下数据模型
from typing import List
class A:
a: List[int] = []
class B(A):
def __init__(self, b: str, a: List[int] = []):
self.b = b
self.a = a
事实
- 福音
A
不能是dataclass
(这会使问题变得微不足道) - 我们想通过
B
继承 - 我们不希望在
A
的实例化时设置参数 - 我们希望能够在
B
的实例化时设置参数
A
a
a
以下我假设的工作是
from typing import List
from dataclasses import dataclass, field
class A:
a: List[int] = []
@dataclass
class B(A):
b: str
a: List[int]
正在更正错误 ValueError: mutable default <class 'list'> for field babies is not allowed: use default_factory
我得到
from typing import List
from dataclasses import dataclass, field
class A:
a: List[int] = field(default_factory=list)
@dataclass
class B(A):
b: str
a: List[int]
但这会产生以下错误AttributeError: a
如果我对 a
使用整数类型,则以下工作正常,表明理论上我所做的是好的,但我表达不正确:
from typing import List
from dataclasses import dataclass, field
class A:
a: int = 1
@dataclass
class B(A):
b: str
a: int
我在这里做错了什么?我如何让它与 a
一起工作作为 B
我引用了引发错误的 dataclasses
模块中的片段(函数 _process_class
):
# If the class attribute (which is the default value for this
# field) exists and is of type 'Field', replace it with the
# real default. This is so that normal class introspection
# sees a real default value, not a Field.
if isinstance(getattr(cls, f.name, None), Field):
if f.default is MISSING:
# If there's no default, delete the class attribute.
# This happens if we specify field(repr=False), for
# example (that is, we specified a field object, but
# no default value). Also if we're using a default
# factory. The class attribute should not be set at
# all in the post-processed class.
delattr(cls, f.name)
else:
setattr(cls, f.name, f.default)
我认为评论表明实现并不期望它必须处理继承的属性。我认为这意味着只能继承处理过的属性,即它们必须来自基础数据类。