Skip to content

[xmltodict] Fix regression in postprocessor - #16616

Open
claui wants to merge 2 commits into
python:mainfrom
claui:xmltodict-postprocessor
Open

claui wants to merge 2 commits into
python:mainfrom
claui:xmltodict-postprocessor

Conversation

@claui

@claui claui commented Oct 25, 2026 •

Copy link
Copy Markdown

Overview

  1. PR [xmltodict] stub improvements and update to 1.0 #14695 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.” That oversight caused a typechecking regression for all clients who are using this feature.

    This PR relaxes 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, this PR modifies 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.

Alternatives

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:

PPK = TypeVar("PPK", bound=Hashable)
_PostprocessorValue: TypeAlias = str | dict[str | PPK, Any] | None

This worked in Mypy but not in Pyright. Similarly,

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.

Relevant upstream code

For reference, the “throw away this node” upstream feature is implemented here:

That feature has existed pre-1.0, and is still around today.

@github-actions

This comment has been minimized.

@claui
claui marked this pull request as draft October 25, 2026 22:58
@claui
claui force-pushed the xmltodict-postprocessor branch from 6eab0c2 to 4965c8a Compare December 2, 2026 19:32
@claui
claui marked this pull request as ready for review December 2, 2026 19:35
@claui
claui force-pushed the xmltodict-postprocessor branch from 8573093 to a68f9de Compare December 2, 2026 19:37
1. Commit 15bc02e (PR python#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 python#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]: python#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
@claui
claui force-pushed the xmltodict-postprocessor branch from 93430c9 to d3c6e75 Compare December 2, 2026 19:40
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant