Code Monkey home page Code Monkey logo

Comments (9)

dotX12 avatar dotX12 commented on June 16, 2024 1

Release alpha: https://pypi.org/project/pyfa-converter/1.0.4.0a0/

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

@falkben, sorry for the long reply, busy with work :(
I'll fix it this weekend!

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

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.

dotX12 avatar dotX12 commented on June 16, 2024

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")

@falkben

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024
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.

dotX12 avatar dotX12 commented on June 16, 2024

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.

dotX12 avatar dotX12 commented on June 16, 2024
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.

dotX12 avatar dotX12 commented on June 16, 2024

I have an idea how to get rid of the pydantic annotation override. I'm going to try now.

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.