ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.61k stars 243 forks source link

Python dataclasses integration #524

Open tonycpsu opened 4 years ago

tonycpsu commented 4 years ago

There are times when I'd like to work with entities in my applications without necessarily persisting them in the database or having every required field set to a value. Maybe there's a method that creates entities to have fields filled in later on by another method, or maybe there's a use case for ephemeral and persistent entities of the same category, e.g. live video streams that will disappear in an hour vs. archived streams.

Python >3.7's dataclasses seem like an obvious solution to creating thin entity objects for the ephemeral case, but there's no easy integration with Pony for them. It seems to me that being able to define entities as dataclasses and having Pony use those as the basis for the database schema would be very valuable, though I'm sure there's a lot of work involved, and perhaps some reasons that it wouldn't work at all.

Anyway, I thought I'd ask the question. Making a change like this would limit backwards compatibility to -- there's a backport of dataclasses for 3.6, but that's it -- but could greatly simplify things for people who start out defining their entities as dataclasses and then migrate to Pony for persistence.

tonycpsu commented 4 years ago

See here for a project that's trying to do this sort of thing for SQLAlchemy.

wozniakty commented 4 years ago

Would kill for an integration of pony and https://github.com/samuelcolvin/pydantic honestly. Like dataclasses but way nicer

andreymal commented 4 years ago

@wozniakty unfortunately pydantic is worse than dataclasses because it does not respect mypy --strict as of today