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

Conversation

@dapper91
Copy link
Owner

@dapper91 dapper91 commented Aug 24, 2024

Adds support for exclude_none and exclude_unset serialization flags.

  • exclude_none - excludes model fields set to None:
>>> from typing import Literal, Optional
>>> from xml.etree.ElementTree import canonicalize
>>> 
>>> from pydantic_xml import BaseXmlModel, element
>>> 
>>> class Product(BaseXmlModel, tag='Product'):
...     title: Optional[str] = element(tag='Title', default=None)
...     status: Optional[Literal['running', 'development']] = element(tag='Status', default=None)
...     launched: Optional[int] = element(tag='Launched', default=None)
... 
>>> product = Product(title="Starlink", status=None)
>>> print(product.to_xml(exclude_none=True, pretty_print=True).decode())
<Product>
  <Title>Starlink</Title>
</Product>
  • exclude_unset - excludes model fields that haven't been explicitly set:
>>> from typing import Literal, Optional
>>> from xml.etree.ElementTree import canonicalize
>>> 
>>> from pydantic_xml import BaseXmlModel, element
>>> 
>>> class Product(BaseXmlModel, tag='Product'):
...     title: Optional[str] = element(tag='Title', default=None)
...     status: Optional[Literal['running', 'development']] = element(tag='Status', default=None)
...     launched: Optional[int] = element(tag='Launched', default=None)
... 
>>> product = Product(title="Starlink", status=None)
>>> print(product.to_xml(exclude_unset=True, pretty_print=True).decode())
<Product>
  <Title>Starlink</Title>
  <Status></Status>
</Product>

Fixes the issues #203 and #194

@codecov-commenter
Copy link

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

Codecov Report

Attention: Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.83%. Comparing base (e928d4b) to head (bfd52b2).

Files Patch % Lines
pydantic_xml/serializers/factories/model.py 90.90% 1 Missing ⚠️

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

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #204      +/-   ##
==========================================
- Coverage   91.86%   91.83%   -0.03%     
==========================================
  Files          29       29              
  Lines        1610     1617       +7     
==========================================
+ Hits         1479     1485       +6     
- Misses        131      132       +1     
Flag Coverage Δ
unittests 91.83% <96.15%> (-0.03%) ⬇️

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.

@dapper91 dapper91 merged commit 672e594 into dev Aug 24, 2024
@dapper91 dapper91 mentioned this pull request Aug 24, 2024
Merged
@dapper91 dapper91 deleted the skip-empty-or-unset branch August 24, 2024 16:00
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