pydantic dataclasses support
See original GitHub issueI saw prior issue https://github.com/tefra/xsdata/issues/522 and I’m using fastapi as well.
It looks like pydantic has support for standard dataclasses. https://pydantic-docs.helpmanual.io/usage/dataclasses/
This is easy enough to achieve in xsdata with a slight import change via build_import_patterns.
@classmethod
def build_import_patterns(cls) -> Dict[str, Dict]:
type_patterns = cls.build_type_patterns
return {
"dataclasses": {"field": [" = field("]},
"pydantic.dataclasses": {"dataclass": ["@dataclass"]},
"decimal": {"Decimal": type_patterns("Decimal")},
"enum": {"Enum": ["(Enum)"]},
"typing": {
"Dict": [": Dict"],
"List": [": List["],
"Optional": ["Optional["],
"Tuple": ["Tuple["],
"Type": ["Type["],
"Union": ["Union["],
},
"xml.etree.ElementTree": {"QName": type_patterns("QName")},
"xsdata.models.datatype": {
"XmlDate": type_patterns("XmlDate"),
"XmlDateTime": type_patterns("XmlDateTime"),
"XmlDuration": type_patterns("XmlDuration"),
"XmlPeriod": type_patterns("XmlPeriod"),
"XmlTime": type_patterns("XmlTime"),
},
}
I don’t know if this 2 line change is worth an entire plugin or if it could be accomplished with a new output -o pydantic-dataclasses
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Dataclasses - pydantic
Keep in mind that pydantic.dataclasses.dataclass is a drop-in replacement for dataclasses.dataclass with validation, not a replacement for pydantic.BaseModel ( ...
Read more >Pydantic or dataclasses? Why not both? Convert Between Them
Pydantic's arena is data parsing and sanitization, while dataclasses a is a fast and memory-efficient (especially using slots, Python 3.10+) ...
Read more >Using Dataclasses - FastAPI
FastAPI is built on top of Pydantic, and I have been showing you how to use Pydantic models to declare requests and responses....
Read more >Does pydantic.dataclasses.dataclass support classic mapping ...
So my question is does pydantic.dataclasses.dataclass support classic mapping in SQLAlchemy? I am working on a project and hopefully can build ...
Read more >Attrs, Dataclasses and Pydantic - Stefan Scherfke
The recommended way for creating pydantic models is to subclass pydantic.BaseModel . This means that in contrast to data classes, all models ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For us, it’s really coming down to being able to do more complex validation on incoming XML files. We create our models using XSData which gives us some level of validation, but we want to do more complicated things like put limits on date ranges based on other data elsewhere in the model, or check data against something in an existing database, which we already have Pydantic-based code to do.
There’s other ways of achieving this though, it was more a case of this seems like a good starting point if it’s feasible on the XSData side, but if not we can work around it.
Oh brilliant, and will this still be happening over at https://github.com/tefra/xsdata-pydantic ?
Thanks!