New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Field
and extra
#236
Comments
Couldn't you have
You're currently using Pydantic's
In [4]: class Model(pydantic.BaseModel):
...: field: int = pydantic.Field(a_custom_key=1)
...:
In [5]: Model.schema() # view the JSON schema as generated by Pydantic
Out[5]:
{'title': 'Model',
'type': 'object',
'properties': {'field': {'title': 'Field',
'a_custom_key': 1,
'type': 'integer'}},
'required': ['field']}
In [1]: from msgspec import Meta
In [2]: from typing import Annotated
In [3]: inner = Annotated[int, Meta(gt=0)] # an existing type annotation
In [4]: outer = Annotated[inner, Meta(lt=10)] # wrap an existing annotation in another Annotated
In [5]: import msgspec
In [6]: msgspec.json.decode('1', type=outer)
Out[6]: 1
In [7]: msgspec.json.decode('100', type=outer) # this should error
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
Cell In [7], line 1
----> 1 msgspec.json.decode('100', type=outer) # this should error
ValidationError: Expected `int` <= 9 |
Well, I can do a lot. The question really is why not support this and require the user to do all sorts of manipulation. I gave Dataclasses expose an You can decide not to support this feature, but really I don't see why it would hurt msgspec to do this. |
As I said above, the
I'm not for or against this, I'm mostly confused what you're asking for here. I think part of the issue is that you're asking for a presumed solution, rather than clearly stating the problem you currently have (an example of the XY Problem).
Can you list those use cases here? Knowing what you intend to use this for would be useful when designing a new API. Perhaps it'd be easier if you show the code you'd like to write. What would that code look like? For example, how would this new feature look if you used it in the following struct definition? from msgspec import Struct, Meta
from typing import Annotated
class User(Struct):
name: Annotated[str, Meta(min_length=3, max_length=32)]
groups: list[str] | None = None
email: str | None = None |
Bump @Goldziher - any response to the above? Would a new |
I've gone ahead and added an |
Yes, that would be what we need. Cheers! |
Hi again,
In pydantic I can pass any kwargs to a
Field
and these will be set as a dictionary under theModelField.field_info.extra
dictionary (if extra is allowed). I can also pass an extra dict directly. This is useful for library authors such as myself because it allows us to pass meta data as part of a field defintion.For example, we have stuff like this: https://starlite-api.github.io/starlite/usage/3-parameters/3-the-parameter-function/.
At present the closest thing possible with msgspec is to use the
Meta
object withAnnotated
, and hijack theextra_json_schema
dictionary. This though is not a proper solution - first, because this is not about an extra json schema but rather about passing kwargs, and secondly because this is not necessarily about constraints and the usage ofAnnotated
might be redundant in this case.The text was updated successfully, but these errors were encountered: