Skip to content

feat: annotated form of field serializer/validator support added. #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2025

Conversation

dapper91
Copy link
Owner

Adds support for annotated form for field serializer/validator.

Example:

import pathlib
from typing import Annotated, List, Type

import pydantic_xml as pxml
from pydantic_xml.element import XmlElementReader, XmlElementWriter


def validate_space_separated_list(
        cls: Type[pxml.BaseXmlModel],
        element: XmlElementReader,
        field_name: str,
) -> List[float]:
    if element := element.pop_element(field_name, search_mode=cls.__xml_search_mode__):
        return list(map(float, element.pop_text().split()))

    return []


def serialize_space_separated_list(
        model: pxml.BaseXmlModel,
        element: XmlElementWriter,
        value: List[float],
        field_name: str,
) -> None:
    sub_element = element.make_element(tag=field_name, nsmap=None)
    sub_element.set_text(' '.join(map(str, value)))

    element.append_element(sub_element)


SpaceSeparatedValueList = Annotated[
    List[float],
    pxml.XmlFieldValidator(validate_space_separated_list),
    pxml.XmlFieldSerializer(serialize_space_separated_list),
]


class Plot(pxml.BaseXmlModel):
    x: SpaceSeparatedValueList = pxml.element()
    y: SpaceSeparatedValueList = pxml.element()

Closes the issue

@codecov-commenter
Copy link

codecov-commenter commented May 18, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.15%. Comparing base (d528114) to head (5b5365e).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #261      +/-   ##
==========================================
+ Coverage   92.08%   92.15%   +0.07%     
==========================================
  Files          29       29              
  Lines        1680     1695      +15     
==========================================
+ Hits         1547     1562      +15     
  Misses        133      133              
Flag Coverage Δ
unittests 92.15% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dapper91 dapper91 merged commit 1af17d4 into dev May 18, 2025
6 checks passed
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.

2 participants