From d3c6e75219839991a940593c8a80fee348bd070b Mon Sep 17 00:00:00 2001 From: Claudia Pellegrino Date: Wed, 2 Dec 2026 20:39:55 +0100 Subject: [PATCH 1/2] [xmltodict] Fix regression in postprocessor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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]: https://github.com/python/typeshed/pull/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 --- .../@tests/test_cases/check_postprocessor.py | 38 +++++++++++++++++++ stubs/xmltodict/xmltodict.pyi | 18 +++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 stubs/xmltodict/@tests/test_cases/check_postprocessor.py diff --git a/stubs/xmltodict/@tests/test_cases/check_postprocessor.py b/stubs/xmltodict/@tests/test_cases/check_postprocessor.py new file mode 100644 index 000000000000..efdcfe7d96f6 --- /dev/null +++ b/stubs/xmltodict/@tests/test_cases/check_postprocessor.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import xmltodict +from xmltodict import _AttrDict, _PostprocessorValue + + +# XML post-processor that eats all `b` elements +def b_eater(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[str, object] | None: + if key == "b": + return None # discard the node entirely + return key, value + + +xmltodict.parse("", postprocessor=b_eater) +# returns a tree of just + + +# Post-processors may return arbitrary values +class League: + def __init__(self, league_id: int) -> None: + self.league_id = league_id + + +def league_by_id(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[str, League] | None: + if key == "league": + return '42', League(42) + return None # discard all other nodes + + +xmltodict.parse("", postprocessor=league_by_id) + + +# Unhashable keys are rejected: upstream would crash when using them as dict keys. +def unhashable_key(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[list[str], object] | None: + return [key], value + + +xmltodict.parse("", postprocessor=unhashable_key) # type: ignore diff --git a/stubs/xmltodict/xmltodict.pyi b/stubs/xmltodict/xmltodict.pyi index d0e1237d3360..a15c7d984afc 100644 --- a/stubs/xmltodict/xmltodict.pyi +++ b/stubs/xmltodict/xmltodict.pyi @@ -8,6 +8,16 @@ class ParsingInterrupted(Exception): ... _AttrValue: TypeAlias = str | dict[str, str] _AttrDict: TypeAlias = dict[str, _AttrValue] +# Dictionary keys are either XML names or whatever key an earlier +# postprocessor invocation returned. So keys are usually strings. +# Strictly speaking, upstream allows non-string keys. But allowing them +# in the signature would unduly burden all implementors, who would all +# have to accept non-string keys to pass their typechecks. +# So to keep the common case easy, annotate keys as `str`. +# If your postprocessor must produce non-string keys, annotate its +# `value` parameter as `Any` to override the constraint. +_PostprocessorValue: TypeAlias = str | dict[str, Any] | None + class _DictSAXHandler: path: list[tuple[str, _AttrDict | None]] stack: list[tuple[_AttrDict | None, list[str]]] @@ -20,7 +30,7 @@ class _DictSAXHandler: cdata_key: str force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool] cdata_separator: str - postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _AttrValue], tuple[str, _AttrValue]] | None + postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None dict_constructor: type strip_whitespace: bool namespace_separator: str @@ -37,7 +47,9 @@ class _DictSAXHandler: cdata_key: str = "#text", force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool] = False, cdata_separator: str = "", - postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _AttrValue], tuple[str, _AttrValue]] | None = None, + postprocessor: ( + Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None + ) = None, dict_constructor: type = ..., strip_whitespace: bool = True, namespace_separator: str = ":", @@ -68,7 +80,7 @@ def parse( cdata_key: str = "#text", force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool] = False, cdata_separator: str = "", - postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _AttrValue], tuple[str, _AttrValue]] | None = None, + postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None = None, dict_constructor: type = ..., strip_whitespace: bool = True, namespaces: Mapping[str, str | None] | None = None, From bda94ced5966d518e5a5c8cf3dd327b70d31e92f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:44:05 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/xmltodict/@tests/test_cases/check_postprocessor.py | 6 ++++-- stubs/xmltodict/xmltodict.pyi | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/stubs/xmltodict/@tests/test_cases/check_postprocessor.py b/stubs/xmltodict/@tests/test_cases/check_postprocessor.py index efdcfe7d96f6..998ea69c7328 100644 --- a/stubs/xmltodict/@tests/test_cases/check_postprocessor.py +++ b/stubs/xmltodict/@tests/test_cases/check_postprocessor.py @@ -23,7 +23,7 @@ def __init__(self, league_id: int) -> None: def league_by_id(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[str, League] | None: if key == "league": - return '42', League(42) + return "42", League(42) return None # discard all other nodes @@ -31,7 +31,9 @@ def league_by_id(path: list[tuple[str, _AttrDict | None]], key: str, value: _Pos # Unhashable keys are rejected: upstream would crash when using them as dict keys. -def unhashable_key(path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue) -> tuple[list[str], object] | None: +def unhashable_key( + path: list[tuple[str, _AttrDict | None]], key: str, value: _PostprocessorValue +) -> tuple[list[str], object] | None: return [key], value diff --git a/stubs/xmltodict/xmltodict.pyi b/stubs/xmltodict/xmltodict.pyi index a15c7d984afc..feaa7b832e31 100644 --- a/stubs/xmltodict/xmltodict.pyi +++ b/stubs/xmltodict/xmltodict.pyi @@ -80,7 +80,9 @@ def parse( cdata_key: str = "#text", force_cdata: bool | Container[str] | Callable[[tuple[str, _AttrDict | None], str, str], bool] = False, cdata_separator: str = "", - postprocessor: Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None = None, + postprocessor: ( + Callable[[list[tuple[str, _AttrDict | None]], str, _PostprocessorValue], tuple[str, object] | None] | None + ) = None, dict_constructor: type = ..., strip_whitespace: bool = True, namespaces: Mapping[str, str | None] | None = None,