Skip to content
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

optional raw element bug fixed. #160

Merged
merged 1 commit into from
Jan 13, 2024
Merged

optional raw element bug fixed. #160

merged 1 commit into from
Jan 13, 2024

Conversation

dapper91
Copy link
Owner

Fixes the bug with optional raw element.

Example:

>>> class Model(BaseXmlModel, tag='model', arbitrary_types_allowed=True):
...     element1: Optional[ElementT] = element(default=None)
...
>>> Model().to_xml()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/model.py", line 399, in to_xml
    return etree.tostring(self.to_xml_tree(skip_empty=skip_empty), **kwargs)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/model.py", line 380, in to_xml_tree
    self.__xml_serializer__.serialize(
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/serializers/factories/model.py", line 170, in serialize
    field_serializer.serialize(
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/serializers/factories/raw.py", line 38, in serialize
    sub_element = element.from_native(value)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/element/native/lxml.py", line 22, in from_native
    tag=element.tag,
        ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'tag'

Expected behavior:

>>> from pydantic_xml import BaseXmlModel, element
>>> from xml.etree.ElementTree import Element
>>> from typing import Optional
>>> 
>>> class Model(BaseXmlModel, tag='model', arbitrary_types_allowed=True):
...     element1: Optional[Element] = element(default=None)
... 
>>> print(Model().to_xml().decode())
<model/>

fixes the issue #158

@codecov-commenter
Copy link

codecov-commenter commented Jan 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (73228d7) 91.71% compared to head (c5e77d5) 91.77%.

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

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #160      +/-   ##
==========================================
+ Coverage   91.71%   91.77%   +0.06%     
==========================================
  Files          26       26              
  Lines        1508     1508              
==========================================
+ Hits         1383     1384       +1     
+ Misses        125      124       -1     
Flag Coverage Δ
unittests 91.77% <100.00%> (+0.06%) ⬆️

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 fe48042 into dev Jan 13, 2024
7 checks passed
@dapper91 dapper91 deleted the raw-element-bugfix branch January 13, 2024 07:32
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