ahrefs/atd

atdpy: fix handling of fields defaulting to the empty list

mjambon opened this issue · 1 comments

type t = {
  ~items: int list;
}

results in incorrect Python code in atdpy 2.11.0:

  • the list property is not initialized with an empty list (having to do with its mutability): items: List[int]
  • the JSON writer doesn't check that the list is not None: res['items'] = _atd_write_list(_atd_write_int)(self.items)

Update: having the None value where a list was expected was a bug in a user program where typechecking by mypy had been bypassed. However, the issue of the mutability of [] still had to be addressed.

The solution may be to emit field(default_factory=list) as the default value for lists instead of []. See https://docs.python.org/3/library/dataclasses.html