Repository navigation
Commit d3c6e75
committed
[xmltodict] Fix regression in postprocessor
1. Commit 15bc02e (PR #14695 on GitHub [1]) added a type signature for
the `postprocessor` Callable but failed to take into account that
upstream has a feature where that Callable may return `None`, and
upstream interprets that as “throw away the node entirely in
post-processing.”
See [2] and [3] in the upstream code.
Relax the postprocessor return value to allow `None` so clients can
continue to use that feature (like they used to in types-xmltodict
0.15.0.20250907 and earlier.)
2. Additionally, modify the type of the postprocessor’s `value`
parameter and its return type to allow values of arbitrary type.
This is supported by upstream, and can be used e.g. for parsing XML
directly into domain objects.
An alternative idea for #2 would have been to allow non-string keys, and
make the key type generic so only implementors whose postprocessors
actually *use* non-string keys would have been billed accordingly.
But that didn’t work due to individual quirks in type checkers.
I tried:
```py
PPK = TypeVar("PPK", bound=Hashable)
_PostprocessorValue: TypeAlias = str | dict[str | PPK, Any] | None
```
This worked in Mypy but not in Pyright. Similarly,
```py
PPK = TypeVar("PPK", bound=Hashable)
_PostprocessorValue: TypeAlias = str | dict[str, Any] | dict[PPK, Any] | None
```
worked in Pyright but not in Mypy.
I ultimately abandoned the type variable idea, so keys are simply
strings now.
[1]: #14695
[2]: https://github.com/martinblech/xmltodict/blob/v1.0.4/xmltodict.py#L93-L97
[3]: https://github.com/martinblech/xmltodict/blob/v1.0.4/xmltodict.py#L160-L1621 parent e4a2f22 commit d3c6e75
2 files changed
Lines changed: 53 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
11 | 21 | | |
12 | 22 | | |
13 | 23 | | |
| |||
20 | 30 | | |
21 | 31 | | |
22 | 32 | | |
23 | | - | |
| 33 | + | |
24 | 34 | | |
25 | 35 | | |
26 | 36 | | |
| |||
37 | 47 | | |
38 | 48 | | |
39 | 49 | | |
40 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
41 | 53 | | |
42 | 54 | | |
43 | 55 | | |
| |||
68 | 80 | | |
69 | 81 | | |
70 | 82 | | |
71 | | - | |
| 83 | + | |
72 | 84 | | |
73 | 85 | | |
74 | 86 | | |
| |||
0 commit comments