Python @attr.s 创建带有可选属性的对象时出错

Python @attr.s causes error when creating object with optional attribute

我有一个带有可选属性的 class。当我尝试在不指定可选属性的情况下实例化 class 时,出现错误 TypeError: __init__() missing 1 required positional argument: 'time_range'.

import attr
from typeguard import typechecked
from typing import Optional

@attr.s(auto_attribs=True)
class TimeRange:
    start_time: Optional[str] = attr.ib()
    end_time: Optional[str] = attr.ib()

    @typechecked
    def __init__(
        self,
        start_time: Optional[str] = None,
        end_time: Optional[str] = None,
    ):
        self.start_time = start_time
        self.end_time = end_time

@attr.s(auto_attribs=True)
class Date:
    date: int
    time_range: Optional[TimeRange] = attr.ib()

    @typechecked
    def __init__(
        self,
        date: int,
        time_range: Optional[TimeRange] = None,
    ):
        self.date = date
        self.time_range = time_range


# This throws an error
new_date = Date(date=731)
# This also throws an error
new_date = Date(731)

如何在不指定可选参数的情况下实例化对象?

attr.s 创建一个 __init__ 函数来覆盖你的函数。要解决此问题,只需提供 __init__=False:

import attr
from typeguard import typechecked
from typing import Optional

# note the init=False
@attr.s(auto_attribs=True, init=False)
class TimeRange:
    start_time: Optional[str] = attr.ib()
    end_time: Optional[str] = attr.ib()

    @typechecked
    def __init__(
        self,
        start_time: Optional[str] = None,
        end_time: Optional[str] = None,
    ):
        self.start_time = start_time
        self.end_time = end_time

@attr.s(auto_attribs=True, init=False)
class Date:
    date: int
    time_range: Optional[TimeRange] = attr.ib()

    @typechecked
    def __init__(
        self,
        date: int,
        time_range: Optional[TimeRange] = None,
    ):
        self.date = date
        self.time_range = time_range


new_date = Date(date=731)
new_date = Date(731)

更简单的是,您既不需要编写自定义初始化函数,也不需要阻止 attr 来生成一个。

您只需在每个可选 属性 的 attr.ib 调用中添加一个默认值(就像您在自定义初始化函数中所做的那样)。

import attr
from typing import Optional

@attr.s(auto_attribs=True)
class TimeRange:
    start_time: Optional[str] = attr.ib(default=None)
    end_time: Optional[str] = attr.ib(default=None)

@attr.s(auto_attribs=True)
class Date:
    date: int
    time_range: Optional[TimeRange] = attr.ib(default=None)

# No more errors
new_date = Date(date=731)
new_date = Date(731)