Giter Site home page Giter Site logo

pydantic-marshals's Introduction

pydantic-marshals's People

Contributors

niqzart avatar

Watchers

 avatar

pydantic-marshals's Issues

Support flattening JSON contents

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

Patch-like models from sqlalchemy

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

Support flattenning nested fields

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)

Support arbitrary types assertion

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

Support using pydantic model instances as type checkers

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),)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.