Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atdpy: fix handling of fields defaulting to the empty list #339

Closed
mjambon opened this issue May 4, 2023 · 1 comment · Fixed by #341
Closed

atdpy: fix handling of fields defaulting to the empty list #339

mjambon opened this issue May 4, 2023 · 1 comment · Fixed by #341
Labels
bug target:python Issues related to the Python backend (atdpy)

Comments

@mjambon
Copy link
Collaborator

mjambon commented May 4, 2023

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.

@mjambon
Copy link
Collaborator Author

mjambon commented May 10, 2023

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

mjambon added a commit that referenced this issue May 11, 2023
sharing the same physical value across all objects of the same class.
Fixes #339
mjambon added a commit that referenced this issue May 11, 2023
… of (#341)

sharing the same physical value across all objects of the same class.
Fixes #339
mjambon added a commit to mjambon/opam-repository that referenced this issue May 12, 2023
…n-codec-runtime and atd (2.12.0)

CHANGES:

* atdgen: Annotate generated code with types to disambiguate OCaml
  classic variants (ahrefs/atd#331)
* atdpy: Support the option type more correctly so that it follows
  ATD's convention for JSON encoding. This allows compatibility with
  JSON produced by other tools of the ATD suite. The Python type,
  however, is still a nullable (`Optional`) to make things simpler for
  Python programmers. This prevents distinguishing `["Some", "None"]`
  from `"None"` which both translate to `None` in Python. (ahrefs/atd#332)
* (BREAKING) atdgen: revert default encoding of int64 values as string (ahrefs/atd#330)
* atdgen: Support `<json repr="string">` for `int` values (ahrefs/atd#330)
* atdpy: Treat default field values as expressions to evaluate each time
  they're assigned to a field. This allows the use of mutable defaults such as
  lists (ahrefs/atd#339)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug target:python Issues related to the Python backend (atdpy)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant