dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

nillable element support added. #146

Closed dapper91 closed 7 months ago

dapper91 commented 7 months ago

Adds nillable elements support. See https://www.w3.org/TR/xmlschema-1/#xsi_nil.

Code example:

>>> from typing import Optional
>>> from pydantic_xml import BaseXmlModel, element
>>> 
>>> class Company(BaseXmlModel):
...     title: Optional[str] = element(default=None, nillable=True)
... 
>>> company = Company()
>>> print(company.to_xml(pretty_print=True).decode())
<Company>
  <title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></title>
</Company>

>>> company = Company(title='SpaceX')
>>> print(company.to_xml(pretty_print=True).decode())
<Company>
  <title>SpaceX</title>
</Company>

Fixes https://github.com/dapper91/pydantic-xml/issues/145 issue.

codecov-commenter commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (7f26d33) 91.79% compared to head (af59b91) 91.98%.

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

Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #146 +/- ## ========================================== + Coverage 91.79% 91.98% +0.18% ========================================== Files 25 26 +1 Lines 1378 1410 +32 ========================================== + Hits 1265 1297 +32 Misses 113 113 ``` | [Flag](https://app.codecov.io/gh/dapper91/pydantic-xml/pull/146/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/dapper91/pydantic-xml/pull/146/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin) | `91.98% <100.00%> (+0.18%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.