Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions stubs/xmltodict/@tests/test_cases/check_postprocessor.py
Original file line number Diff line number Diff line change
@@ -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("<a><b/><c><b/></c></a>", postprocessor=b_eater)
# returns a tree of just <a><c/></a>


# 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("<leagues><league/></leagues>", 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("<a/>", postprocessor=unhashable_key) # type: ignore
20 changes: 17 additions & 3 deletions stubs/xmltodict/xmltodict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]
Expand All @@ -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
Expand All @@ -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 = ":",
Expand Down Expand Up @@ -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,
Expand Down
Loading