Skip to content

Type conversion design

Toby Fleming edited this page Apr 9, 2021 · 1 revision

Can type conversion be implemented sanely? There are several issues:

  • Would converters be declared in Options (pro: specify each converter once, con: runtime errors) or on the fields?
  • What does a default converter look like for a boolean? only true and false (exception otherwise)? YAML-like?
  • Similarly, is it okay for float or int converters to be lossy or not round-trip? Does that happen in Python, e.g. if a high-precision float is converted to a str?
  • How would the logic for a Union look like? Try the converter for each type in the union in order? If so, how is None/Optional handled?
  • How would type converters interact with child declarations? Would the order have to be reversed? (First try and resolve a type hint down to one or more XmlDataclass, and if that fails, assume it's an attribute with a converter?)
  • Can Mypy be prevented from subtyping converter arguments to object?
Clone this wiki locally