Skip to content

Commit d3c6e75

Browse files
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-L162
1 parent e4a2f22 commit d3c6e75

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from __future__ import annotations
2+
3+
import xmltodict
4+
from xmltodict import _AttrDict, _PostprocessorValue
5+
6+
7+
# XML post-processor that eats all `b` elements
8+
def b_eater(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[str, object] | None:
9+
if key == "b":
10+
return None # discard the node entirely
11+
return key, value
12+
13+
14+
xmltodict.parse("<a><b/><c><b/></c></a>", postprocessor=b_eater)
15+
# returns a tree of just <a><c/></a>
16+
17+
18+
# Post-processors may return arbitrary values
19+
class League:
20+
def __init__(self, league_id: int) -> None:
21+
self.league_id = league_id
22+
23+
24+
def league_by_id(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[str, League] | None:
25+
if key == "league":
26+
return '42', League(42)
27+
return None # discard all other nodes
28+
29+
30+
xmltodict.parse("<leagues><league/></leagues>", postprocessor=league_by_id)
31+
32+
33+
# Unhashable keys are rejected: upstream would crash when using them as dict keys.
34+
def unhashable_key(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[list[str], object] | None:
35+
return [key], value
36+
37+
38+
xmltodict.parse("<a/>", postprocessor=unhashable_key) # type: ignore

‎stubs/xmltodict/xmltodict.pyi‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ class ParsingInterrupted(Exception): ...
88
_AttrValue: TypeAlias = str | dict[str, str]
99
_AttrDict: TypeAlias = dict[str, _AttrValue]
1010

11+
# Dictionary keys are either XML names or whatever key an earlier
12+
# postprocessor invocation returned. So keys are usually strings.
13+
# Strictly speaking, upstream allows non-string keys. But allowing them
14+
# in the signature would unduly burden all implementors, who would all
15+
# have to accept non-string keys to pass their typechecks.
16+
# So to keep the common case easy, annotate keys as `str`.
17+
# If your postprocessor must produce non-string keys, annotate its
18+
# `value` parameter as `Any` to override the constraint.
19+
_PostprocessorValue: TypeAlias = str | dict[str, Any] | None
20+
1121
class _DictSAXHandler:
1222
path: list[tuple[str, _AttrDict | None]]
1323
stack: list[tuple[_AttrDict | None, list[str]]]
@@ -20,7 +30,7 @@ class _DictSAXHandler:
2030
cdata_key: str
2131
force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool]
2232
cdata_separator: str
23-
postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _AttrValue], tuple[str, _AttrValue]] | None
33+
postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None
2434
dict_constructor: type
2535
strip_whitespace: bool
2636
namespace_separator: str
@@ -37,7 +47,9 @@ class _DictSAXHandler:
3747
cdata_key: str = "#text",
3848
force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool] = False,
3949
cdata_separator: str = "",
40-
postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _AttrValue], tuple[str, _AttrValue]] | None = None,
50+
postprocessor: (
51+
Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None
52+
) = None,
4153
dict_constructor: type = ...,
4254
strip_whitespace: bool = True,
4355
namespace_separator: str = ":",
@@ -68,7 +80,7 @@ def parse(
6880
cdata_key: str = "#text",
6981
force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool] = False,
7082
cdata_separator: str = "",
71-
postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _AttrValue], tuple[str, _AttrValue]] | None = None,
83+
postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None = None,
7284
dict_constructor: type = ...,
7385
strip_whitespace: bool = True,
7486
namespaces: Mapping[str, str | None] | None = None,

0 commit comments

Comments
 (0)