Skip to content

Commit

Permalink
Add support for pydantic v2 (#144)
Browse files Browse the repository at this point in the history
* update dependencies

* update trio to fix a test issue

* fix pydantic v2 compatibility issue

* add pydantic v2 to ci

* setup pydantic version before fastapi version in ci

* remove pydantic settings before downgrading to pydantic v1

* update python patch version in ci

* update pytest-aiohttp

* no cover for line only run by pydantic v1
  • Loading branch information
enadeau authored Jul 31, 2023
1 parent f6605a5 commit e8bfae5
Show file tree
Hide file tree
Showing 8 changed files with 789 additions and 781 deletions.
24 changes: 20 additions & 4 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ "3.8.13", "3.11.2", "3.12.0-alpha.5" ]
fastapi-version: [ "0.68.0", "0.86.0", "0.95.0"]
python-version: [ "3.8.17", "3.11.4", "3.12.0-beta.4" ]
fastapi-version: [ "0.68.0", "0.86.0", "0.95.0", "0.100.1"]
pydantic-version: [ "1.10.12", "2.1.1"]
exclude:
# Don't test python 3.12 on old FastAPI versions
- python-version: "3.12.0-alpha.5"
- python-version: "3.12.0-beta.4"
fastapi-version: "0.68.0"
- python-version: "3.12.0-alpha.5"
- python-version: "3.12.0-beta.4"
fastapi-version: "0.86.0"
- fastapi-version: "0.68.0"
pydantic-version: "2.1.1"
- fastapi-version: "0.86.0"
pydantic-version: "2.1.1"
- fastapi-version: "0.95.0"
pydantic-version: "2.1.1"
steps:
- name: Check out repository
uses: actions/checkout@v3
Expand All @@ -58,6 +65,15 @@ jobs:
if: steps.cache-venv.outputs.cache-hit != 'true'
- name: Install package
run: poetry install --no-interaction
- name: Remove pydantic-settings
if: matrix.pydantic-version == '1.10.12'
run: |
source .venv/bin/activate
poetry remove pydantic-settings
- name: Install Pydantic ${{ matrix.pydantic-version }}
run: |
source .venv/bin/activate
poetry add "pydantic==${{ matrix.pydantic-version }}"
- name: Install FastAPI ${{ matrix.fastapi-version }}
run: |
source .venv/bin/activate
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ repos:
[
fastapi,
pydantic,
pydantic-settings,
starlette,
httpx
]
6 changes: 3 additions & 3 deletions demo_project/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ async def __call__(self, tid: str) -> str:

azure_scheme_auto_error_false_b2c = B2CMultiTenantAuthorizationCodeBearer(
app_client_id=settings.APP_CLIENT_ID,
openapi_authorization_url=settings.AUTH_URL,
openapi_token_url=settings.TOKEN_URL,
openid_config_url=settings.CONFIG_URL,
openapi_authorization_url=str(settings.AUTH_URL),
openapi_token_url=str(settings.TOKEN_URL),
openid_config_url=str(settings.CONFIG_URL),
scopes={
f'api://{settings.APP_CLIENT_ID}/user_impersonation': 'User impersonation',
},
Expand Down
10 changes: 8 additions & 2 deletions demo_project/core/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import List, Optional, Union

from pydantic import AnyHttpUrl, BaseSettings, Field, HttpUrl
import pydantic
from pydantic import AnyHttpUrl, Field, HttpUrl

if pydantic.VERSION.startswith('1.'):
from pydantic import BaseSettings
else:
from pydantic_settings import BaseSettings

class AzureActiveDirectory(BaseSettings):

class AzureActiveDirectory(BaseSettings): # type: ignore[misc, valid-type]
OPENAPI_CLIENT_ID: str = Field(default='', env='OPENAPI_CLIENT_ID')
TENANT_ID: str = Field(default='', env='TENANT_ID')
APP_CLIENT_ID: str = Field(default='', env='APP_CLIENT_ID')
Expand Down
2 changes: 1 addition & 1 deletion fastapi_azure_auth/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def scopes_to_list(cls, v: object) -> object:
"""
if isinstance(v, str):
return v.split(' ')
return v
return v # pragma: no cover


class User(Claims):
Expand Down
1,518 changes: 751 additions & 767 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fastapi = ">0.68.0"
cryptography = ">=40.0.1"
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
httpx = ">0.18.2"
pydantic-settings = "^2.0.2"


[tool.poetry.group.dev.dependencies]
Expand All @@ -57,12 +58,12 @@ pytest-mock = "^3.5.1"
requests-mock = "^1.8.0"
pytest-socket = "^0.4.0"
pytest-dotenv = "^0.5.2"
pytest-aiohttp = "^0.3.0"
pytest-aiohttp = "^1.0.4"
uvicorn = "^0.17.5"
pytest-freezegun = "^0.4.2"
pytest-cases = "^3.6.3"
anyio = "^3.3.4"
trio = "^0.19.0"
trio = "^0.22.2"
respx = "^0.20.1"
ipython = "^8.2.0"

Expand Down
4 changes: 2 additions & 2 deletions tests/multi_tenant_b2c/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ async def issuer_fetcher(tid):
current_issuer = issuer
return B2CMultiTenantAuthorizationCodeBearer(
app_client_id=settings.APP_CLIENT_ID,
openapi_authorization_url=settings.AUTH_URL,
openapi_token_url=settings.TOKEN_URL,
openapi_authorization_url=str(settings.AUTH_URL),
openapi_token_url=str(settings.TOKEN_URL),
# The value below is used only for testing purpose you should use:
# https://login.microsoftonline.com/common/v2.0/oauth2/token
openid_config_url='https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration',
Expand Down

0 comments on commit e8bfae5

Please sign in to comment.