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
Generic method to create Strict<type> fields #780
Comments
I'm happy with However I would note the following:
|
Great. Thanks for pointing out "bool is subclass of int" legacy. I was not aware of that and now understand the reason behind https://github.com/samuelcolvin/pydantic/blob/5f634067daa0236632af22c0a8b15dac310b18a1/pydantic/validators.py#L105 Let's be practical then. My original intent was to solve We can delay the generic case till there will be more demand for it (and there is already a prototype for people to borrow and play with).
|
We're working on v1 now. Only bugs are being released until we get v1 out. I will accept a PR for
|
I did a PR that addresses this issue in some respect: #799 |
Whatever solution is accepted for #1060 may affect this so linking here. |
Is there a way to create strict types using For example this is completely what I want but class Kek(BaseModel):
value: conint(strict=True, ge=0, le=10) # type: ignore Is it possible to do it using class Kek(BaseModel):
value: int = Field(..., strict=True, ge=0, le=10) # doesn't work, just an example/suggestion |
If this can help there is a possible solution here #2079 (comment) |
An alternative view on this: Developers are most likely split into two camps:
Field-specific ability to enable strict parsing might be useful to some,but I believe masses would just want to toggle "strictness" on and write: class Kek(BaseModel):
f1: str
f2: bool
f3: int Rather than more explicit: class Kek(BaseModel):
f1: StrictStr
f2: bool = Field(..., strict=True) # won't work, as `strict` not supported here, btw.
f3: conint(strict=True) # mypy will nag about this, btw. Latter looks ugly and kludgy. On a larger project, multiply that by 100-1000+ and you have lots of wasted characters (and clarity). |
On V2, you can use the Lines 82 to 92 in f50ae9f
|
Feature Request
This is a followup from tiangolo/fastapi#453 (comment)
Description
We'd like have a general way to create Strict field classes, e.g. StrictInt, StrictFloat, etc. that do value instance type checking during validation phase.
Rationale
My usecase for this feature is to be able properly support Unions for native types. Today if you have a field coming from JSON data that can be either
bool
,str
, orint
, you can't define a model in pydantic that will keep original data field's type.Here is an example:
The above will:
{"field": "foo"}
as boolean, sincebool
"eats" anything (as well asstr
)I.e.
MyModel(**json.loads('{"field": "foo"}')).json() != '{"field": "foo"}'
Implementation
@dmontagu suggested two approaches to implement this.
Creator function
It works just fine. However mypy does not like dynamic types and prints errors for the above code:
Generic class
This version is mypy friendly but is less convenient to the user - requires to create classes in a bit of a magical manner.
The text was updated successfully, but these errors were encountered: