UC-Davis-molecular-computing / scadnano-python-package

Python scripting library for generating designs readable by scadnano.
https://scadnano.org
MIT License
13 stars 7 forks source link

change label type to string #261

Closed dave-doty closed 1 year ago

dave-doty commented 1 year ago

Scadnano supports the notion of a label attached to a strand, domain, loopout, or extension.

Currently, the type of label is an arbitrary JSON-serializable object. However, this makes type checking awkward, and since such objects can be mutable, the web interface (which relies on immutable state date) might have unforeseen problems due to this.

It would be simpler to simply say the type of label is a string. If a user wants more structured data, they can do the JSON serialization and deserialization themselves. Note that this will be a breaking change.

See https://github.com/UC-Davis-molecular-computing/scadnano/issues/878

dave-doty commented 1 year ago

Release notes

Breaking change: labels must be strings

Changed the type of label field in Strand, Domain, Loopout, and Extension to str instead of an arbitrary object.

This is a breaking change because existing code using non-string labels will have to be altered to change the data to a string before storing and change it back to structured data when reading.

Users can serialize to a string and deserialize back to structured data manually using the json package.

Before, this was possible:

from typing import List

# previously was possible, now is not supported
nums = [1, 2, 3]
strand.label = nums   # stores strand.label as the list [1, 2, 3]; would be a mypy type error now

# and to get the structured data back out:
nums: List[int] = strand.label  # would be a mypy type error now

Now this is necessary to store a list of int's in the label:

import json
from typing import List

nums = [1, 2, 3]
strand.label = json.dumps(nums)  # stores strand.label as the string '[1, 2, 3]'

# and to get the structured data back out:
nums: List[int] = json.loads(strand.label)  # nums is now the list [1, 2, 3]