-
Notifications
You must be signed in to change notification settings - Fork 20
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
Any
no longer a valid field type in v2
#100
Comments
@Jacob-Flasheye Hi. Could you please provide some examples of how you use In v1 class Model(BaseXmlModel):
field: Any = element()
M(field=1).to_xml() # Ok
M(field=[1, 2, 3]).to_xml() # Error whereas class Model(BaseXmlModel):
field: List[int] = element()
M(field=[1, 2, 3]).to_xml() # Ok |
Thanks for the quick reply, appreciate your work 🙏 The problem at hand is generating pydantic_xml models from xml schema files, where I don't control the files. <xs:complexType name="AnyHolder">
<xs:sequence>
<xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:anyAttribute processContents="lax"/>
</xs:complexType> In v1 I could do as you show in your first example, and while serialization errors out, I could still instantiate the model and do the serialization by hand. What I was thinking (without knowing any of the pydantic internals) is that the serialization of an I'm currently leaning towards representing the model as class AnyHolder(Generic[ElemT, AttrT], BaseModel): # I will admit I haven't looked up the specifics of generic models in pydantic v2
any_elem: list[ElemT] = element()
any_attr: AttrT = attr() but technically that model is wrong ( One solution that would work well here (I think) is the suggestion to support raw xml in #14. I think that would cover Sorry for the ranting nature of this post, I'm not frustrated with |
@Jacob-Flasheye Hi, Thanks for the thorough answer! I am working on raw xml fields right now but I am not sure it will solve your pain with class AnyHolder(BaseXmlModel):
any_elem: list[etree.Element] = element() but it will not fit your schema since the sequence element may have any tag. Speaking of class AnyHolder(BaseXmlModel):
any_attrs: Dict[str, str] Although raw xml still could help you. class AnyHolder(BaseXmlElement, tag='AnyHolder'):
...
class OuterModel(BaseXmlModel, arbitrary_types_allowed=True):
any_holder_raw: etree.Element = element(tag='AnyHolder', exclude=True)
@computed_element
def any_holder(self) -> AnyHolder:
# manual parsing here Will that help you to deal with AnyHolder? |
Wow, I didn't know
My current solution looks like this:
I think this issue can be closed, it seems you are aware of the problems I've mentioned and you've carefully given suggestions on what I can do. Now it's up to me to use those suggestions! |
There's one more thing to this that I hadn't thought of until I just now started generating xmlschema elements, and that is that they can have any name. IIUC class CaptureUnassignedElements(BaseXmlModel, allow_arbitrary_types=True, capture_unassigned_elements=True):
test: int = element("Test")
input_str = """
<CaptureUnassignedElements>
<test>23</test>
<abc123>test</abc123>
<empty/>
</CaptureUnassignedElements>
"""
cua_test = CaptureUnassignedElements.from_xml(input_str)
print(cua_test.test) # prints 23
print(cua_test.any_) # print [lxml.etree._Element(...), lxml.etree._Element(...)] or something similar I'm not sure if this is good design, nor how hard it is to do, but it does reflect a real use case caused by xmlschema so I think it should at least be considered. |
This is something that I'm bumping into as well, that I understand is frankly against the nature and benefit of these strongly defined models, but is a use case I'm nevertheless having to support. The schema definition for the element is:
which, in practice, might look like:
which I'm struggling to support. I've documented some more of this in the issue tracker for the project here: spacetelescope/vo-models#18 |
@Jacob-Flasheye I also need to generate models from XSD files (which you described in #100 (comment)) so would be interested in collaborating here. @dapper91 would this be something that you would be willing to integrate? I know that XSD can be thorny, but that feature would be a natural fit for pydantic-xml. edit: Could we maybe parially support https://docs.pydantic.dev/latest/concepts/models/#dynamic-model-creation in pydantic-xml so that we could use e.g. https://xmlschema.readthedocs.io/en/latest/usage.html#meta-schemas-and-xsd-sources for dynamic model creation? This task alone would not need support for serializing (e.g. #92) so might be feasible? |
I added dynamic model creation experimental support in 2.10.0. |
(Disclaimer: This is my work account and I'm posting this on behalf of my $work.)
Hi.
I'm working on upgrading our code to v2, and the only
pydantic_xml
-related snag I've hit is thatAny
is no longer a valid type.I think I can work around this by using generics, but I'm curious as to why their support has been removed.
Could you explain why
Any
is no longer a valid field type and if there are any plans to make it a valid field type again?The text was updated successfully, but these errors were encountered: