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..998ea69c7328
--- /dev/null
+++ b/stubs/xmltodict/@tests/test_cases/check_postprocessor.py
@@ -0,0 +1,40 @@
+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..feaa7b832e31 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,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, _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,