Open
Conversation
Owner
|
@jorants Hi, For now you can define custom schema: from typing import Annotated
import pydantic_xml as pxml
from pydantic_core import core_schema as cs
class SpaceSeparatedValueListSchema:
@classmethod
def __get_pydantic_core_schema__(cls, source_type, handler):
schema = cs.no_info_after_validator_function(lambda val: val.split(' '), cs.str_schema())
serialization = cs.plain_serializer_function_ser_schema(lambda lst: ' '.join(lst))
return cs.json_or_python_schema(json_schema=schema, python_schema=schema, serialization=serialization)
class Person(pxml.BaseXmlModel):
children: Annotated[list[str], SpaceSeparatedValueListSchema] = pxml.attr()
name: str = pxml.element()
doc = """
<Person children="Bob Eve">
<name>Alice</name>
</Person>
"""
alice = Person.from_xml(doc)
print(alice.children) # prints ['Bob', 'Eve']
print(alice.to_xml()) # prints b'<Person children="Bob Eve"><name>Alice</name></Person>' |
Owner
|
I suppose this collection attribute implementation is not very intuitive since it only works if validator and serializer are provided. class Person(pxml.BaseXmlModel):
children: list[str] = pxml.attr()
name: str = pxml.element()but it fails with a misleading error. |
dapper91
reviewed
Jun 29, 2025
| return ElementSerializer.from_core_schema(schema, ctx) | ||
| elif ctx.entity_location is EntityLocation.ATTRIBUTE: | ||
| raise errors.ModelFieldError(ctx.model_name, ctx.field_name, "attributes of collection types are not supported") | ||
| return primitive.from_core_schema(pcs.StringSchema(type="str"), ctx) |
Owner
There was a problem hiding this comment.
I suppose it should work with any primitive type not only with a string one
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.
Currently collection types are not allowed for attributes. This is logical for the default serializers, but the user might overwrite default behavior with a custom validator that might return a collection. For example:
Instead of disallowing outright, this change parses these attributes as a string and leave it up to the user.
This might not be a good final solution, it would be nicer to check if a custom validator logic is present and error when it is not.
However, I do not see an easy way to add such a check.
What do you think? I at least got stuck parsing XML documents that contain space-separated lists in attributes.