I'm trying to find a way to distinguish whether an argument has been passed to the method or not.
For instance, I have the following function:
@dataclass
class Record:
id: int
name: str
completed_at: Optional[date] = None
records = [
Record(id=1, name="Foo", completed_at=date(2021, 1, 10)),
Record(id=2, name="Bar", completed_at=date(2021, 1, 11)),
]
def update_record(
id: int,
name: Optional[str] = None,
completed_at: Optional[date] = ..., # type: ignore
):
record = next(record for record in records if record.id == id)
if name is not None:
record.name = name
if completed_at is not ...:
record.completed_at = completed_at
It works like a charm, but when I remove # type: ignore
comment mypy complains with the following error:
error: Incompatible default for argument
"completed_at" (default has type "ellipsis", argument has type
"Optional[date]") [assignment]
... int, name: Optional[str] = None, completed_at: Optional[date] = ...
I've tried a solution with dummy "sentinel" object like:
DO_NOTHING = object()
def update_record(id, completed_at: Union[DO_NOTHING, None, date] = DO_NOTHING):
pass
...but in my opinion it is a little bit too verbose.
Is there a way to do it better in a less verbose way?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…