Skip to content

Commit

Permalink
Merge pull request #1118 from maykinmedia/feature/2279-pydantic-v2
Browse files Browse the repository at this point in the history
⬆️ [#2279] Upgrade pydantic to 2.6.4
  • Loading branch information
stevenbal authored Mar 28, 2024
2 parents b494d66 + 739a7ac commit 4341c59
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
7 changes: 6 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ aldryn-apphooks-config==0.7.0
# via -r requirements/base.in
amqp==5.1.1
# via kombu
annotated-types==0.6.0
# via pydantic
ape-pie==0.1.0
# via zgw-consumers
asgiref==3.7.2
Expand Down Expand Up @@ -413,8 +415,10 @@ psycopg2==2.9.9
# via -r requirements/base.in
pycparser==2.20
# via cffi
pydantic[email]==1.10.14
pydantic[email]==2.6.4
# via -r requirements/base.in
pydantic-core==2.16.3
# via pydantic
pydyf==0.1.2
# via weasyprint
pyee==11.0.1
Expand Down Expand Up @@ -506,6 +510,7 @@ typing-extensions==4.10.0
# via
# -r requirements/base.in
# pydantic
# pydantic-core
# pyee
# xsdata
uritemplate==4.1.1
Expand Down
13 changes: 12 additions & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ amqp==5.1.1
# -c requirements/base.txt
# -r requirements/base.txt
# kombu
annotated-types==0.6.0
# via
# -c requirements/base.txt
# -r requirements/base.txt
# pydantic
ape-pie==0.1.0
# via
# -c requirements/base.txt
Expand Down Expand Up @@ -760,7 +765,12 @@ pycparser==2.20
# -c requirements/base.txt
# -r requirements/base.txt
# cffi
pydantic[email]==1.10.14
pydantic[email]==2.6.4
# via
# -c requirements/base.txt
# -r requirements/base.txt
# pydantic
pydantic-core==2.16.3
# via
# -c requirements/base.txt
# -r requirements/base.txt
Expand Down Expand Up @@ -931,6 +941,7 @@ typing-extensions==4.10.0
# -r requirements/base.txt
# polyfactory
# pydantic
# pydantic-core
# pyee
# xsdata
uritemplate==4.1.1
Expand Down
13 changes: 12 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ amqp==5.1.1
# -c requirements/ci.txt
# -r requirements/ci.txt
# kombu
annotated-types==0.6.0
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
# pydantic
ape-pie==0.1.0
# via
# -c requirements/ci.txt
Expand Down Expand Up @@ -889,7 +894,12 @@ pycparser==2.20
# -c requirements/ci.txt
# -r requirements/ci.txt
# cffi
pydantic[email]==1.10.14
pydantic[email]==2.6.4
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
# pydantic
pydantic-core==2.16.3
# via
# -c requirements/ci.txt
# -r requirements/ci.txt
Expand Down Expand Up @@ -1119,6 +1129,7 @@ typing-extensions==4.10.0
# -r requirements/ci.txt
# polyfactory
# pydantic
# pydantic-core
# pyee
# xsdata
uritemplate==4.1.1
Expand Down
8 changes: 1 addition & 7 deletions src/open_inwoner/laposta/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ class LapostaList(JSONEncoderMixin, BaseModel):
account_id: str
members: Members

class Config(JSONEncoderMixin.Config):
pass


class UserData(BaseModel):
class UserData(JSONEncoderMixin, BaseModel):
ip: IPvAnyAddress
email: EmailStr
source_url: HttpUrl | None
Expand All @@ -46,6 +43,3 @@ class Member(JSONEncoderMixin, BaseModel):
ip: IPvAnyAddress
source_url: HttpUrl | Literal[""] | None = None
custom_fields: dict | None = None

class Config(JSONEncoderMixin.Config):
pass
5 changes: 1 addition & 4 deletions src/open_inwoner/qmatic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BranchDetail(BaseModel):


class Appointment(JSONEncoderMixin, BaseModel):
url: str | None
url: str | None = None
services: list[QmaticService]
title: str
start: datetime
Expand All @@ -77,9 +77,6 @@ class Appointment(JSONEncoderMixin, BaseModel):
branch: BranchDetail
notes: str | None

class Config(JSONEncoderMixin.Config):
pass


class NoServiceConfigured(RuntimeError):
pass
Expand Down
31 changes: 14 additions & 17 deletions src/open_inwoner/utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any

import requests
from pydantic_core import Url

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -31,23 +32,19 @@ def get_json_response(response: requests.Response) -> dict | None:


class JSONEncoderMixin:
"""
Mixin for `pydantic.BaseModel` to make `BaseModel.dict()` produce JSON serialized data
"""

# To make `BaseModel.dict()` produce JSON serialized data, i.e. for usage in tests
# in tandem with `requests_mock`, we cast the data using the configured JSON encoders
# Source: https://github.com/pydantic/pydantic/issues/1409#issuecomment-1381655025
def _iter(self, **kwargs):
for key, value in super()._iter(**kwargs):
yield key, self.__config__.json_encoders.get(type(value), lambda v: v)(
value
)

class Config:
json_encoders = {
# We need to specify a serializable format for datetimes and IPv4Addresses, otherwise
# `BaseModel.dict()` will complain about certain types not being JSON serializable
def model_dump(self, **kwargs):
"""
To make `BaseModel.model_dump()` produce JSON serialized data, i.e. for usage in tests
in tandem with `requests_mock`, we cast the data using the configured JSON encoders
Source: https://github.com/pydantic/pydantic/issues/1409#issuecomment-1130601015
"""
json_encoders: dict = {
datetime: lambda dt: dt.isoformat(sep=" "),
IPv4Address: str,
Url: str,
}
result = super().model_dump(**kwargs)
for key, value in result.items():
if mapping_func := json_encoders.get(type(value)):
result[key] = mapping_func(value)
return result

0 comments on commit 4341c59

Please sign in to comment.