Giter Site home page Giter Site logo

Comments (7)

PrettyWood avatar PrettyWood commented on September 16, 2024

It took me some time to understand at first the from_orm behaviour. Personally I like to think about "dict representation" (which try_instance kind of explains better).
I still don't understand why we need to add this in pydantic-core. Wouldn't it work to convert in pydantic world a from_orm to a function (dict representation) -> dict validation in pydantic-core?

from pydantic-core.

samuelcolvin avatar samuelcolvin commented on September 16, 2024

Well we could, but it would be slower.

The real problem would be with nested objects - hard to know the the python function how deep to go.

Since we have the functionality in pydantic-core, I think it's just about deciding when to enable it.

from pydantic-core.

PrettyWood avatar PrettyWood commented on September 16, 2024

Ah yes you're right. It's just that the current pydantic-core interface is neat and quite generic. Having a specific case of conversion for dict feels a bit odd. And I'm just thinking right now how we had issues in my company with this issue.
I don't know if it would make sense to generalize this behaviour for other types like lists where users want to use a custom representation my_object_as_dict, my_object_as_list, ...
It could be a simple "converter" key in the schema 🤔

Instead of

v = SchemaValidator({'type': 'dict', 'keys': {'type': 'str'}, 'try_instance_as_dict': True})

we would write

def cls_instance_to_dict(x):
    if hasattr(x, '__dict__'):
        return {k: serialize(v) for k, v in x.__dict__.items()}
    return x

v = SchemaValidator({'type': 'dict', 'keys': {'type': 'str'}, 'convertor': cls_instance_to_dict})

from pydantic-core.

samuelcolvin avatar samuelcolvin commented on September 16, 2024

I think we just need a config flag.

from pydantic-core.

samuelcolvin avatar samuelcolvin commented on September 16, 2024

"converters" are covered by the functions already implemented - e.g. you could use a function with mode "before" and convert the data before calling the main validator.

from pydantic-core.

samuelcolvin avatar samuelcolvin commented on September 16, 2024

think I'm going to use the name from_attributes.

I think this is better than try_instance and from_orm.

from pydantic-core.

PrettyWood avatar PrettyWood commented on September 16, 2024

the name is way better 👍

from pydantic-core.

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.