tefra / xsdata-pydantic

Naive XML & JSON Bindings for python pydantic classes!
https://xsdata-pydantic.readthedocs.io/
MIT License
52 stars 10 forks source link

feat: add pydantic.BaseModel variant support, wip #18

Closed tlambert03 closed 7 months ago

tlambert03 commented 1 year ago

I also found myself wanting a pydantic.BaseModel subclass, as @Nicholas-Schaub requested in #17 ...

This is my stab at it. I was able to use it to convert a pretty large model, and bind relatively complicated XML data using the generated model, so I don't think it's too far off... but perhaps that's naive, given how different it is from the existing patterns.

@tefra, would love to get your input on this. Obviously, there are some discussions to be had about namespaces and exposing it, but probably best to pause here and see what you think

Nicholas-Schaub commented 1 year ago

@tlambert03 we seem to keep running into each other...

I was thinking of you and ome-types when I found this library.

tlambert03 commented 1 year ago

I know! fancy seeing you here 👋

yes... I too have been kicking myself for not finding it earlier!

tlambert03 commented 1 year ago

i butchered your bindings test to parametrize it with the BaseModel variant, just as a proof of principle. I can clean that up if you're actually interested in including this...

tlambert03 commented 1 year ago

@Nicholas-Schaub, if you want to kick this around a little, I made a new repo to play with for now: https://github.com/tlambert03/xsdata-pydantic-basemodel

pip install git+https://github.com/tlambert03/xsdata-pydantic-basemodel.git

and then use --output pydantic-basemodel

Nicholas-Schaub commented 1 year ago

What bindings test?

tlambert03 commented 1 year ago

Sorry, that was a note to @tefra

tefra commented 1 year ago

So this is a hybrid dataclass + pydantic base model right?

I really want to use pure pydantic models if possible, I honestly don't have the time to work on this for a few more months, so any contribution is welcome.

tlambert03 commented 1 year ago

yep, it's all working great in ome-types, and it's even working for pydantic v2 with a nice speed bump. Not a hybrid per-se, they are all pure BaseModel (no dataclass decorators). I wasn't sure how soon you'd get around to this, so I've been developing it as a package that just gets shipped with ome-types for now (so as not to clutter pypi with new package names).

You can see what I've got in this directory (and also in https://github.com/tlambert03/xsdata-pydantic-basemodel ... which lacks a few of the updates in ome-types)

To me, the big question is whether you'd (personally) want to have this BaseModel variant in this library, perhaps with a switch in the config to decide whether to use BaseModel or pydantic.dataclasses, or if you think it's different enough to just make a new package.

tefra commented 7 months ago

I finally managed to pass the suite with pydantic 2.6