Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
claui
marked this pull request as draft
October 25, 2026 22:58
claui
force-pushed
the
xmltodict-postprocessor
branch
from
December 2, 2026 19:32
6eab0c2 to
4965c8a
Compare
claui
marked this pull request as ready for review
December 2, 2026 19:35
claui
force-pushed
the
xmltodict-postprocessor
branch
from
December 2, 2026 19:37
8573093 to
a68f9de
Compare
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
force-pushed
the
xmltodict-postprocessor
branch
from
December 2, 2026 19:40
93430c9 to
d3c6e75
Compare
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
PR [xmltodict] stub improvements and update to 1.0 #14695 added a type signature for the
postprocessorCallable but failed to take into account that upstream has a feature where that Callable may returnNone, 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
Noneso clients can continue to use that feature (like they used to in types-xmltodict 0.15.0.20250907 and earlier.)Additionally, this PR modifies the type of the postprocessor’s
valueparameter 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:
This worked in Mypy but not in Pyright. Similarly,
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:
https://github.com/martinblech/xmltodict/blob/v1.0.4/xmltodict.py#L93-L97
https://github.com/martinblech/xmltodict/blob/v1.0.4/xmltodict.py#L160-L162
That feature has existed pre-1.0, and is still around today.