Code Monkey home page Code Monkey logo

Comments (9)

euri10 avatar euri10 commented on April 28, 2024 1

I begun a branch on that, I'm not a mypy pro so not sure I should PR an unfinished attempt, if you're interested in looking at it it's on https://github.com/euri10/fastapi/tree/pydantic_0.20.0
I have still some typing issues, I got no tests problems despite the breaking changes listed in pydantic changelog, which I find strange, so my work may be complete crap 🤤

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

Yes, I'll update it soon. There are a couple breaking changes (mainly in the type definitions).

It is pinned because as these packages are still in versions 0.x, it means that any change could be a breaking change, as we (Starlette, Pydantic, FastAPI, etc) are still all evolving and improving quite quickly.

But I'll update it soon.

from fastapi.

wshayes avatar wshayes commented on April 28, 2024

Completely understand the pinning. I don't understand the codebase well enough to do the PR most likely but I'm happy to tilt at this or something else to help out. We are really enjoying using FastAPI.

from fastapi.

edrodri2 avatar edrodri2 commented on April 28, 2024

Would updating this add support for skip_defaults for pydantic models added in 0.20.0 or would this need more changes? I would like to be able to not show null values in my responses.

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

It is updated now, compatible with the latest Pydantic 🎉

It's available in version 0.9.0.

Thanks @euri10, I couldn't see your branch (it gives 404 for some reason). But it's updated now.

@edrodri2 Nope, because skip_defaults must be called in the dict and json methods, that are called automatically.

One option would be to include, apart from a response_model parameter, another parameter (maybe something like model_extra) with a dict of kwarg options to pass to Pydantic's dict directly. It would be used internally by FastAPI.

How does that sound?

from fastapi.

StepanBakshayev avatar StepanBakshayev commented on April 28, 2024

I have a issue with pydantic 0.21, but pydantic 0.23 works fine. Please, update pydantic. Do you have schedule of next release?

from pydantic import BaseModel, ValidationError, constr
from typing import List, Optional


class Phone(BaseModel):
    id: Optional[int]

    phone: constr(min_length=10, max_length=30)
    additional_code: Optional[constr(min_length=1, max_length=30)]


class Representative(BaseModel):
    id: Optional[int]

    name_last: constr(min_length=1, max_length=100)
    name_first: constr(min_length=1, max_length=100)
    name_middle: Optional[constr(min_length=1, max_length=100)]
    phone: Phone


class CorporateEntity(BaseModel):
    id: Optional[int]

    name: constr(min_length=1, max_length=300)
    representatives: Optional[List[Representative]]


try:
    CorporateEntity(**{
        'name': 'Corporate',
        'representatives': [{
            'name_last': 'name_last',
            'name_first': 'name_first',
            'name_middle': 'name_middle',
            'phone': {
                'phone': '123'}}]})
except ValidationError as e:
    print(e.json())

#
## Unexpected result in 0.21
#
"""
[
  {
    "loc": [
      "phone",
      "phone"
    ],
    "msg": "ensure this value has at least 10 characters",
    "type": "value_error.any_str.min_length",
    "ctx": {
      "limit_value": 10
    }
  },
  {
    "loc": [
      "representatives"
    ],
    "msg": "value is not none",
    "type": "type_error.none.allowed"
  }
]
"""

#
## Expected result in 0.23
#
"""
[
  {
    "loc": [
      "representatives",
      0,
      "phone",
      "phone"
    ],
    "msg": "ensure this value has at least 10 characters",
    "type": "value_error.any_str.min_length",
    "ctx": {
      "limit_value": 10
    }
  },
  {
    "loc": [
      "representatives"
    ],
    "msg": "value is not none",
    "type": "type_error.none.allowed"
  }
]
"""

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

@StepanBakshayev The just-released FastAPI version 0.16.0 is compatible with Pydantic 0.23 🎉 (thanks to @euri10 's PR)

from fastapi.

StepanBakshayev avatar StepanBakshayev commented on April 28, 2024

I used it. You solved the issue fastly and completely.

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

Great @StepanBakshayev !

from fastapi.

Related Issues (20)

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.