Skip to content

Commit

Permalink
✨ Migrate Authx to use pydantic v2 (#531)
Browse files Browse the repository at this point in the history
✨ Migrate Authx to use pydantic v2
  • Loading branch information
yezz123 authored Feb 9, 2024
2 parents 30e66bf + 919f285 commit 331717f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
3 changes: 2 additions & 1 deletion authx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from typing import List, Optional, Sequence

from jwt.algorithms import get_default_algorithms, requires_cryptography
from pydantic import BaseSettings, Field
from pydantic import Field
from pydantic_settings import BaseSettings

from authx.exceptions import BadConfigurationError
from authx.types import (
Expand Down
12 changes: 5 additions & 7 deletions authx/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from hmac import compare_digest
from typing import Any, Dict, List, Optional, Sequence

from pydantic import BaseModel, Extra, Field, ValidationError, validator
from pydantic import BaseModel, ConfigDict, Field, ValidationError, validator

from authx._internal._utils import get_now, get_now_ts, get_uuid
from authx.exceptions import (
Expand All @@ -26,6 +26,7 @@


class TokenPayload(BaseModel):
model_config = ConfigDict(extra="allow")
jti: Optional[str] = Field(default_factory=get_uuid)
iss: Optional[str] = None
sub: Optional[str] = None
Expand All @@ -40,16 +41,13 @@ class TokenPayload(BaseModel):
scopes: Optional[List[str]] = None
fresh: bool = False

class Config:
extra = Extra.allow

@property
def _additional_fields(self):
return set(self.__dict__) - set(self.__fields__)
return set(self.__dict__) - set(self.model_fields)

@property
def extra_dict(self):
return self.dict(include=self._additional_fields)
return self.model_dump(include=self._additional_fields)

@property
def issued_at(self) -> datetime.datetime:
Expand Down Expand Up @@ -136,7 +134,7 @@ def decode(
issuer=issuer,
verify=verify,
)
return cls.parse_obj(payload)
return cls.model_validate(payload)


class RequestToken(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ classifiers = [
dependencies = [
"fastapi >=0.65.2,<0.110.0",
"pyjwt[crypto] >=2.6.0,<3.0.0",
"pydantic >=1.7.4,!=1.8,!=1.8.1,<2.0.0",
"pydantic >=2.0.0,<2.6.1",
"pydantic-settings >=2.1.0",
"python-dateutil>=2.8,<3.0.0",
"pytz>=2023.3,<2025.0",
"python-jose>=3.3.0,<4.0.0",
Expand Down
14 changes: 7 additions & 7 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#
babel==2.14.0
# via mkdocs-material
beautifulsoup4==4.12.2
beautifulsoup4==4.12.3
# via mkdocs-mermaid2-plugin
cairocffi==1.6.1
# via cairosvg
cairosvg==2.7.1
# via -r requirements/docs.in
certifi==2023.11.17
certifi==2024.2.2
# via requests
cffi==1.16.0
# via cairocffi
Expand Down Expand Up @@ -57,7 +57,7 @@ markdown==3.5.2
# pymdown-extensions
markdown-include==0.8.1
# via -r requirements/docs.in
markupsafe==2.1.3
markupsafe==2.1.5
# via
# jinja2
# mkdocs
Expand All @@ -73,7 +73,7 @@ mkdocs==1.5.3
# mkdocs-mermaid2-plugin
mkdocs-markdownextradata-plugin==0.2.5
# via -r requirements/docs.in
mkdocs-material==9.5.7
mkdocs-material==9.5.8
# via -r requirements/docs.in
mkdocs-material-extensions==1.3.1
# via mkdocs-material
Expand All @@ -89,7 +89,7 @@ pillow==10.2.0
# via
# -r requirements/docs.in
# cairosvg
platformdirs==4.1.0
platformdirs==4.2.0
# via mkdocs
pycparser==2.21
# via cffi
Expand Down Expand Up @@ -143,9 +143,9 @@ typer[all]==0.7.0
# typer-cli
typer-cli==0.0.13
# via -r requirements/docs.in
urllib3==2.1.0
urllib3==2.2.0
# via requests
watchdog==3.0.0
watchdog==4.0.0
# via mkdocs
webencodings==0.5.1
# via
Expand Down
4 changes: 2 additions & 2 deletions requirements/linting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packaging==23.2
# via black
pathspec==0.12.1
# via black
platformdirs==4.1.0
platformdirs==4.2.0
# via
# black
# virtualenv
Expand All @@ -38,7 +38,7 @@ pyupgrade==3.15.0
# via -r requirements/linting.in
pyyaml==6.0.1
# via pre-commit
ruff==0.2.0
ruff==0.2.1
# via -r requirements/linting.in
tokenize-rt==5.2.0
# via pyupgrade
Expand Down
20 changes: 16 additions & 4 deletions requirements/pyproject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#
# pip-compile --extra=all --output-file=requirements/pyproject.txt pyproject.toml
#
annotated-types==0.6.0
# via pydantic
anyio==4.2.0
# via starlette
cffi==1.16.0
# via cryptography
cryptography==42.0.0
cryptography==42.0.2
# via pyjwt
ecdsa==0.18.0
# via python-jose
Expand All @@ -22,19 +24,26 @@ pyasn1==0.5.1
# rsa
pycparser==2.21
# via cffi
pydantic==1.10.13
pydantic==2.6.1
# via
# authx (pyproject.toml)
# fastapi
# pydantic-settings
pydantic-core==2.16.2
# via pydantic
pydantic-settings==2.1.0
# via authx (pyproject.toml)
pyjwt[crypto]==2.8.0
# via
# authx (pyproject.toml)
# pyjwt
python-dateutil==2.8.2
# via authx (pyproject.toml)
python-dotenv==1.0.1
# via pydantic-settings
python-jose==3.3.0
# via authx (pyproject.toml)
pytz==2023.3.post1
pytz==2024.1
# via authx (pyproject.toml)
rsa==4.9
# via python-jose
Expand All @@ -47,4 +56,7 @@ sniffio==1.3.0
starlette==0.36.3
# via fastapi
typing-extensions==4.9.0
# via pydantic
# via
# fastapi
# pydantic
# pydantic-core
14 changes: 6 additions & 8 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
#
anyio==4.2.0
# via httpx
certifi==2023.11.17
certifi==2024.2.2
# via
# httpcore
# httpx
# requests
charset-normalizer==3.3.2
# via requests
coverage[toml]==7.4.0
coverage[toml]==7.4.1
# via
# coverage
# pytest-cov
freezegun==1.4.0
# via -r requirements/testing.in
greenlet==3.0.3
# via sqlalchemy
h11==0.14.0
# via httpcore
httpcore==1.0.2
Expand All @@ -38,14 +36,14 @@ itsdangerous==2.1.2
# via -r requirements/testing.in
packaging==23.2
# via pytest
pluggy==1.3.0
pluggy==1.4.0
# via pytest
pytest==7.4.4
pytest==8.0.0
# via
# -r requirements/testing.in
# pytest-asyncio
# pytest-cov
pytest-asyncio==0.23.4
pytest-asyncio==0.23.5
# via -r requirements/testing.in
pytest-cov==4.1.0
# via -r requirements/testing.in
Expand All @@ -63,7 +61,7 @@ sqlalchemy==2.0.25
# via -r requirements/testing.in
typing-extensions==4.9.0
# via sqlalchemy
urllib3==2.1.0
urllib3==2.2.0
# via requests
websockets==12.0
# via -r requirements/testing.in

0 comments on commit 331717f

Please sign in to comment.