Comments (8)
As you may already know I love unions: smart, strict and tagged ones 😄
What about trade unions?
syntax looks good.
Questions:
- Does the field have to be a literal? I think yes.
- We currently have multiple different sorts of literal, are you going to allow all of them?
- Maybe this should be a separate validator? Not for performance, more to make it less complex and easier to develop.
- How are you going to implement it? perhaps you could expose some of the logic in literal as public functions, then reuse them in
TaggedUnion
? The other option is to change the schema syntax to add a intermediate dict under choice that can include "tags" - this would make generating schema more complex, but it would avoid the need (at least in pydantic-core schema) for the tag as a field, and would make your logic much simpler, you could also limit tags to being string more explicitly. Something like:
'type': 'model',
'fields': {
'pet': {
'schema': {
'type': 'union',
'tag': 'species',
'choices': [
{
'tag': 'cat',
'schema': {
'type': 'model',
'fields': {
'species': {'schema': {'type': 'literal', 'expected': ['cat']}},
'lives': {'schema': {'type': 'int'}, 'default': 9},
},
}
},
{
'tag': 'dog',
# or maybe
'tags': ['dog', 'fox'],
{
'type': 'model',
'fields': {
# note that here we don't **need** to have species as a field
'barks': {'schema': {'type': 'bool'}},
},
},
}
],
}
},
},
This is completely up to you, I can see both working and we can probably support both in pydantic.
from pydantic-core.
Does the field have to be a literal? I think yes.
Yes I think we need to check for tagged unions that
- all fields are models
- all models have a field with the tag name and a type literal
- all literal values have no overlap
and then cache them into aHashmap
(or evenFxHashMap
?) for immediate validator lookup
We currently have multiple different sorts of literal, are you going to allow all of them?
I was planning to only support string literals (so LiteralSingleString
and LiteralMultipleStrings
). We could support int literals but I've never seen a usecase in my life so 🤷
Maybe this should be a separate validator? Not for performance, more to make it less complex and easier to develop.
Yep I was tempted! Maybe suggest tagged-union
as type. I'll probably go with this approach
The other option is to change the schema syntax to add a intermediate dict under choice that can include "tags"
Probably a good idea! It should avoid adding public methods on fields just for tagged unions. I'll play a bit with the code tonight and come back with a proposal 👍
from pydantic-core.
sounds good, just strings (both single and multiple) makes sense so they fix into map.
If we went with the different schema, then we no longer care that all choices are models, since we only look at the tag
or tags
key.
Probably a good idea! It should avoid adding public methods on fields just for tagged unions.
If we did go with the first syntax, no need to add new methods to the trait, just implement that logic as standalone functions in literal.rs
, then import and use them in union.rs
as well as in the build
functions in literal types.
from pydantic-core.
Might be worth looking at LookupKey
in #88 we could reuse it in tagged unions to allow the tag to be somewhere other than the top level dict while reusing existing logic.
from pydantic-core.
Might be worth looking at
LookupKey
in #88 we could reuse it in tagged unions to allow the tag to be somewhere other than the top level dict while reusing existing logic.
This is what pydantic/pydantic#4180 wants.
Having thought about this more, I think moving tag
/ tags
into a parent dict and reusing LookupKey
is a good idea. LookupKey
is now well optimised with support for reusing PyString
s etc.
from pydantic-core.
@PrettyWood any idea when you might get to work on this?
I need it for #131.
No big hurry. I'm happy to wait a while or take over this if you think you'll be able to work on it.
from pydantic-core.
@samuelcolvin I'm still on holiday until Sunday. And I'll first work on launching Q3 for my company before working more on OSS (even though I'd love to right now).
I had planned to start working on it in 2-3 weeks. I didn't know you needed it too 😆
IMO if you can work on it right now it's better. I know you have everything in mind and probably want to release a v1 asap.
I have only my phone on me but it should be enough to review the code if you wish 🙂
from pydantic-core.
Understood. Enjoy your holiday and don't worry about it.
I'll let you know when I have a PR and it's completely up to you if you want to review it.
from pydantic-core.
Related Issues (20)
- Segmentation fault - datetime python module HOT 3
- [Heads up] test_invalid_regex fails with Python 3.13.0a3 (re.Error renamed to PatternError) HOT 3
- Consider setting a fixed minimum Rust version HOT 18
- unable to build pydantic-core on openbsd HOT 1
- SPSS 29 Statistics cannot import module HOT 2
- Build failure of pydantic-core 2.14.6 with Rust 1.68.2 and Python 3.11 HOT 2
- No Module pydantic_core._pydantic_core (armv7l, built from source) HOT 4
- Outdated emscripten version, incompatible with PyScript HOT 1
- 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
- Optimistically close string values in from_json if allow_partial is set to true HOT 3
- 2.18.0: pep517 based build fails HOT 9
- `serialization` logic not regognized in `json` and `python` schemas at lower levels
- Make `validate_json` accept `memoryview`.
- [BUG] Serializing datetime.time loses timezone data when datetime.datetime is reqired for utc offset because of DST
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.