question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pydantic dataclasses support

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jtc42commented, Mar 14, 2022

It’s still planned

Oh brilliant, and will this still be happening over at https://github.com/tefra/xsdata-pydantic ? Thanks!

Yeap, just out of curiocity what are the features of pydantic that you actually need or how do you plan to use xsdata with pydantic?

To be honest pydantic doesn’t play very nice with inner classes and forward referenceces and it’s a bit hard to work around these limitations…

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.

1reaction
jtc42commented, Mar 11, 2022

It’s still planned

Oh brilliant, and will this still be happening over at https://github.com/tefra/xsdata-pydantic ?

Thanks!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found