niqzart / pydantic-marshals Goto Github PK
View Code? Open in Web Editor NEWLibrary for creating partial pydantic models (automatic converters) from different mappings
Library for creating partial pydantic models (automatic converters) from different mappings
Same as #5, but getting a model like:
{
"a": 1,
"b": "string",
}
From the Model
class:
class DataAB(TypedDict):
a: int
b: str
class Model(Base):
...
data: Mapped[DataAB] = mapped_column()
# TODO check if using TypedDict is even possible here
Support the following (syntax may differ):
class User(Base):
...
name: Mapped[str] = mapped_column()
description: Mapped[str | None] = mapped_coumn()
ExampleModel = MappedModel.create(columns=[name], patch_like=True)
Test cases and behaviour:
{} # not changing anything
{"name": "hey"} # only setting name to "hey"
{"description": "test"} # only setting description to "test"
{"description": null} # setting description to None
It could be useful to be able to get a model like:
{
"a_f": 1,
"b_f": 2,
}
From B
in this setup:
class A(Base):
id: Mapped[int] = mapped_column(primary_key=True)
a_f: Mapped[int] = mapped_column()
class B(Base):
b_f: Mapped[int] = mapped_column(primary_key=True)
a_id: Mapped[int] = mapped_column(ForeignKey("a.id"))
a: Mapped[A] = relationship()
So a
will be accessed via the relationship, but it's fields are not nested (i.e. it's not a.a_f
)
This should work:
from pydantic_marshals.contains import assert_contains
class MyClass:
pass
assert_contains(
MyClass(),
MyClass,
)
But instead this raises an error:
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class '__main__.MyClass'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.
For further information visit https://errors.pydantic.dev/2.6/u/schema-for-unknown-type
This should work:
from pydantic import BaseModel
from pydantic_marshals.contains import assert_contains
class MyModel(BaseModel):
some_field: int
assert_contains(
MyModel(some_field=3),
MyModel(some_field=3),
)
But instead this raises an error:
RuntimeError: Couldn't convert field: (MyModel(some_field=3),)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.