这是indexloc提供的服务,不要输入任何密码
Skip to content

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
@dapper91 dapper91 deleted the feat/annotated-field-validator-serializer branch June 13, 2025 11:53
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.

3 participants