Coder Social home page Coder Social logo

Comments (3)

dantownsend avatar dantownsend commented on June 26, 2024 1

@Donokami That's a really nice solution!

What I've been doing in the past is something like:

MyTableBaseModel = create_pydantic_model(MyTable)


class MyTableModel(MyTableBaseModel):
    ...

But what you've done is definitely cleaner. I'll modify this bit of the docs to include it.

I upgraded VSCode, and Pylance is way more aggressive now. Previously I didn't encounter any warnings with the Piccolo project, but now there are quite a few I need to look into.

from piccolo.

dantownsend avatar dantownsend commented on June 26, 2024

Interesting - I haven't run Pylance in strict mode before. I just turned it on to have a look.

I've seen the Variable not allowed in type expression error before. It's because create_pydantic_model creates a new class, which is assigned to a variable. Type checkers don't like it when you use a variable as a type. Functionally it works fine, so it's OK to ignore these errors.

The way we'll fix that long term is having an alternative to create_pydantic_model which works like this instead:

class MyModel(PiccoloPydanticModel, table=MyTable):
    ...

As for the Type of "create_pydantic_model" is partially unknown error, I made this example script:

from piccolo.table import Table
from piccolo.columns import Varchar

from piccolo.utils.pydantic import create_pydantic_model


class MyTable(Table):
    name = Varchar()


MyModel = create_pydantic_model(
    table=MyTable,
)


def bar(model: MyModel):
    pass

The cause might be that Pylance is looking at Pydantic v1 for some reason. It tries to find pydantic.config.ConfigDict and it's can't because that's only available in Pydantic v2.

Screenshot 2023-11-03 at 15 42 33

Have a quick look to see if that's the case.

from piccolo.

Conobi avatar Conobi commented on June 26, 2024

Hey!
I've been trying things for a while, and I found something really quite cool, that works with basic mode.
This works with Pylance:

class Image_pydantic(
    create_pydantic_model(
        table=Image,
        exclude_columns=(Image.parameters, Image.author),
    )
):
    pass

This syntax, for declaring a Pydantic model, is really interesting because it also allows us to change how we serialize specific fields.
For example, I can do this:

class Expense_pydantic(
    create_pydantic_model(
        table=Expense,
        exclude_columns=(Expense.customer,),
        nested=True,
    )
):
    image: Image_pydantic

And it will automatically use the above pydantic model to serialize nested data from Expense.
Another example of interesting use of this declaration:

class Customer_pydantic(
    create_pydantic_model(
        table=Customer,
        include_columns=(
            Customer.id,
            Customer.email,
            Customer.created_on,
            Customer.updated_on,
            Customer.external_token,
        ),
    )
):
    external_token: Annotated[
        Optional[str],
        Field(
            max_length=40,
            examples=["r8_FoO**********************************"],
        ),
    ] = None

    @model_validator(mode="after")
    def post_root(self) -> "Customer_schema":
        if self.external_token is not None and len(self.external_token) > 7:
            self.external_token = self.external_token[:6] + "*" * (
                len(self.external_token) - 6
            )
        if self.external_token == "":
            self.external_token = None
        return self

This would share a field stored in the DB, but only give a hint about its content.

For the Pylance error on create_pydantic_model, the only one I see is this one:
Screenshot of my VSCode type error

Thanks for the fast answer and for your work btw, Piccolo is trully underrated!!

from piccolo.

Related Issues (20)

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.