Comments (9)
Release alpha: https://pypi.org/project/pyfa-converter/1.0.4.0a0/
from pyfa-converter.
@falkben, sorry for the long reply, busy with work :(
I'll fix it this weekend!
from pyfa-converter.
I see a problem but I can't figure out how to solve it...
I can suggest a workaround that surprisingly works :)
from pydantic import conint
id: conint(gt=10) = Field(..., description="gwa")
from pyfa-converter.
I see a problem but I can't figure out how to solve it...
I can suggest a workaround that surprisingly works :)
from pydantic import conint id: conint(gt=10) = Field(..., description="gwa")
from pyfa-converter.
curl -X 'POST' \
'http://127.0.0.1:8000/register' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'id=5'
{
"detail": [
{
"loc": [
"body",
"id"
],
"msg": "ensure this value is greater than 10",
"type": "value_error.number.not_gt",
"ctx": {
"limit_value": 10
}
}
]
}
from pyfa-converter.
UPD
from pydantic import BaseModel
from pydantic import ConstrainedInt
from pydantic import Field
class Model(BaseModel):
foo: ConstrainedInt = Field(..., ge=10)
print(Model(foo=10))
Traceback (most recent call last):
File "C:\Users\dotX12\Documents\PycharmProjects\pydantic-to-fastapi\test.py", line 6, in <module>
class Model(BaseModel):
File "pydantic\main.py", line 198, in pydantic.main.ModelMetaclass.__new__
File "pydantic\fields.py", line 504, in pydantic.fields.ModelField.infer
File "pydantic\schema.py", line 1011, in pydantic.schema.get_annotation_from_field_info
ValueError: On field "foo" the following field constraints are set but not enforced: ge.
For more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints
I figured out the reason, it's a pydantic mechanic :)
https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints
Due to double conversion, pydantic-form-pydantic int becomes ConstrainedInt
@classmethod
def __fill_params(
cls, param: Callable[..., FieldInfo], model_field: ModelField, default: Any
) -> FieldInfo:
print(model_field.annotation)
<class '__main__.ConstrainedIntValue'>
typing.Optional[str]
typing.Optional[typing.List[int]]
from pyfa-converter.
class PostContractSmallDoubleBodySchema(BaseModel):
id: int = Field(..., description="gwa", ge=10)
title: Optional[str] = Field(None)
data: Optional[List[int]]
print(list(PostContractSmallDoubleBodySchema.__fields__.values())[0].annotation)
<class '__main__.ConstrainedIntValue'>
its fine :D
from pyfa-converter.
I have an idea how to get rid of the pydantic annotation override. I'm going to try now.
from pyfa-converter.
Your example works if you do
annotation=model.__annotations__.get(field.name)
but the optional typing stops working.
annotation=model.__annotations__.get(field.name) or field.annotation,
works, but it's magical crap, so if something breaks, I returned it as it was.
from pyfa-converter.
Related Issues (10)
- Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type' HOT 4
- Optional form data HOT 6
- Error with some specific field types HOT 4
- Default value "False" of BaseModel is ignored HOT 5
- support pydantic v2 HOT 3
- update python-multipart version to 0.0.6
- ERROR: No matching distribution found for pyfa-converter==1.0.4.1 HOT 1
- ImportError: cannot import name 'ModelField' from 'pydantic.fields' HOT 1
- Add License HOT 3
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 pyfa-converter.