You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+14-1Lines changed: 14 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,19 @@ if __name__ == "__main__":
65
65
* Lists of child elements are supported
66
66
* Inheritance does work, but has the same limitations as dataclasses. Inheriting from base classes with required fields and declaring optional fields doesn't work due to field order. This isn't recommended
67
67
* Namespace support is decent as long as correctly declared. I've tried on several real-world examples, although they were known to be valid. `lxml` does a great job at expanding namespace information when loading and simplifying it when saving
68
+
* Union child types are supported. When loading XML, they are attempted to be parsed in order
69
+
70
+
## Gotchas
71
+
72
+
### Whitespace
73
+
74
+
If you are able to, it is strongly recommended you strip whitespace from the input via `lxml`:
75
+
76
+
```python
77
+
parser = etree.XMLParser(remove_blank_text=True)
78
+
```
79
+
80
+
By default, `lxml` preserves whitespace. This can cause a problem when checking if elements have no text. The library does attempt to strip these; literally via Python's `strip()`. But `lxml` is likely faster and more robust.
68
81
69
82
## Limitations and Assumptions
70
83
@@ -77,8 +90,8 @@ Most of these limitations/assumptions are enforced. They may make this project u
77
90
* It isn't possible to pass any parameters to the wrapped `@dataclass` decorator
78
91
* Some properties of dataclass `field`s are not exposed: `default_factory`, `repr`, `hash`, `init`, `compare`. For most, it is because I don't understand the implications fully or how that would be useful for XML. `default_factory` is hard only because of [the overloaded type signatures](https://github.com/python/typeshed/blob/master/stdlib/3.7/dataclasses.pyi), and getting that to work with `mypy`
79
92
* Deserialisation is strict; missing required attributes and child elements will cause an error. I want this to be the default behaviour, but it should be straightforward to add a parameter to `load` for lenient operation
80
-
* Unions of types aren't yet supported
81
93
* Dataclasses must be written by hand, no tools are provided to generate these from, DTDs, XML schema definitions, or RELAX NG schemas
94
+
* Union types must have the same element/tag name and namespace. Otherwise, two different dataclass attributes (XML child fields) may be used
0 commit comments