Comments (7)
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.
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.
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.
I think we just need a config flag.
from pydantic-core.
"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.
think I'm going to use the name from_attributes
.
I think this is better than try_instance
and from_orm
.
from pydantic-core.
the name is way better 👍
from pydantic-core.
Related Issues (20)
- Worst case validation performance scales with the number of pydantic objects in memory HOT 4
- Unable to build Pydantic-Core 2.16.3 on Alpine 3.19 HOT 3
- Datetime underflow causes validation to fail HOT 1
- Different behavior for `extra_behavior` and `config.extra_fields_behavior`
- Add support for simple ser schema with `any` (`lambda x: x` replacement)
- Strict bug :( with config vs runtime flag
- False-positive `SchemaError` for keyword-only args: Non-default argument follows default argument
- 2.18.0: pep517 based build fails HOT 15
- `serialization` logic not regognized in `json` and `python` schemas at lower levels
- Make `validate_json` accept `memoryview`. HOT 1
- [BUG] Serializing datetime.time loses timezone data when datetime.datetime is reqired for utc offset because of DST HOT 1
- generate_self_schema.py incompatible with Python 3.13.0b1 HOT 3
- Unexpected behavior for Partial=True HOT 2
- UUID validators with custom string object HOT 1
- Fail to build pydantic_core on OS X 10.11 with Python 3.10 HOT 8
- 2.18.2: pytest fails in 3 units HOT 7
- Recursive serialization of UUID seems to fail json.dumps HOT 1
- Lower MSRV to 1.75
- `include` and `exclude` are not passed to field serializer contexts in function serializer HOT 1
- fail-fast list schema configuration
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydantic-core.