Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/pydantic v2 #217

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Set up Python 3.8
- name: Set up Python 3.13
uses: actions/[email protected]
with:
python-version: 3.8
python-version: 3.13
- name: Install dependencies
run: pip install -qU setuptools wheel twine
- name: Generating distribution archives
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Python
uses: actions/[email protected]
with:
python-version: 3.8
python-version: 3.13
- name: Install dependencies
run: make install-test
- name: Lint
Expand All @@ -20,7 +20,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Setup Python
uses: actions/[email protected]
with:
python-version: 3.8
python-version: 3.13
- name: Install dependencies
run: make install-test
- name: Generate coverage report
Expand All @@ -50,4 +50,5 @@ jobs:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
SHELL := bash
PATH := ./venv/bin:${PATH}
PYTHON = python3.8
PYTHON = python3.13
PROJECT = fast_agave
isort = isort $(PROJECT) tests setup.py
black = black -S -l 79 --target-version py38 $(PROJECT) tests setup.py examples
black = black -S -l 79 --target-version py313 $(PROJECT) tests setup.py examples


.PHONY: all
Expand Down
2 changes: 1 addition & 1 deletion examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
app.include_router(resources)

app.add_middleware(AuthedMiddleware)
app.add_middleware(FastAgaveErrorHandler)
app.add_middleware(FastAgaveErrorHandler) # type: ignore


@app.get('/')
Expand Down
11 changes: 11 additions & 0 deletions examples/middlewares/authed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@


class AuthedMiddleware(ContextMiddleware):
def __init__(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remover este init no veo que haga algo extra

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseHTTPMiddleware cambió en versiones recientes de Starlette. Ahora el parámetro app debe pasarse explícitamente como argumento al inicializar un middleware basado en BaseHTTPMiddleware.

De lo contrario obtenemos: BaseHTTPMiddleware.__init__() missing 1 required positional argument: 'app'

Podemos hacer algo más simple como:

    def __init__(self, app):
        super().__init__(app=app)

Sería mejor?

self, app, plugins=None, default_error_response=None, *args, **kwargs
):
super().__init__(
app=app,
plugins=plugins,
default_error_response=default_error_response,
*args,
**kwargs,
)

def required_user_id(self) -> bool:
"""
Example method so we can easily mock it in tests environment
Expand Down
4 changes: 2 additions & 2 deletions examples/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def health_auth_check() -> Dict:


@app.get('/raise_cuenca_errors')
def raise_cuenca_errors() -> NoReturn:
def raise_cuenca_errors() -> None:
raise WrongCredsError('you are not lucky enough!')


@app.get('/raise_fast_agave_errors')
def raise_fast_agave_errors() -> NoReturn:
def raise_fast_agave_errors() -> None:
raise UnauthorizedError('nice try!')


Expand Down
2 changes: 1 addition & 1 deletion examples/resources/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ async def update(
user: UserModel, request: UserUpdateRequest, api_request: Request
) -> Response:
user.name = request.name
user.ip = api_request.client.host
user.ip = api_request.client.host if api_request.client else None
await user.async_save()
return Response(content=user.to_dict(), status_code=200)
3 changes: 1 addition & 2 deletions examples/tasks/retry_task_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
QUEUE_URL = 'http://127.0.0.1:4000/123456789012/core.fifo'


class YouCanTryAgain(Exception):
...
class YouCanTryAgain(Exception): ...


def test_your_luck(message):
Expand Down
26 changes: 10 additions & 16 deletions examples/validators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Optional

from cuenca_validations.types import QueryParams
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
import datetime as dt

from pydantic.main import BaseConfig


class AccountQuery(QueryParams):
name: Optional[str] = None
Expand All @@ -28,16 +26,13 @@ class UserQuery(QueryParams):

class AccountRequest(BaseModel):
name: str

class Config(BaseConfig):
fields = {
'name': {'description': 'Sample description'},
}
schema_extra = {
model_config = ConfigDict(
json_schema_extra={
'example': {
'name': 'Doroteo Arango',
}
}
)


class AccountResponse(BaseModel):
Expand All @@ -47,10 +42,8 @@ class AccountResponse(BaseModel):
platform_id: str
created_at: dt.datetime
deactivated_at: Optional[dt.datetime] = None

class Config(BaseConfig):
fields = {'name': {'description': 'Sample description'}}
schema_extra = {
model_config = ConfigDict(
json_schema_extra={
'example': {
'id': 'AC-123456',
'name': 'Doroteo Arango',
Expand All @@ -60,17 +53,18 @@ class Config(BaseConfig):
'deactivated_at': None,
}
}
)


class AccountUpdateRequest(BaseModel):
name: str

class Config(BaseConfig):
schema_extra = {
model_config = ConfigDict(
json_schema_extra={
'example': {
'name': 'Pancho Villa',
}
}
)


class FileQuery(QueryParams):
Expand Down
40 changes: 17 additions & 23 deletions fast_agave/blueprints/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from fastapi.responses import JSONResponse as Response
from fastapi.responses import StreamingResponse
from mongoengine import DoesNotExist, Q
from pydantic import ValidationError
from pydantic.main import BaseConfig, BaseModel
from pydantic import BaseModel, Field, ValidationError
from starlette_context import context

from ..exc import NotFoundError, UnprocessableEntity
Expand Down Expand Up @@ -231,7 +230,7 @@ async def retrieve(id: str, request: Request):
file,
media_type=mimetype,
headers={
'Content-Disposition': f'attachment; filename={filename}'
'Content-Disposition': f'attachment; filename={filename}' # noqa: E702
},
)
elif hasattr(cls, 'retrieve'):
Expand All @@ -255,30 +254,25 @@ async def retrieve(id: str, request: Request):
return cls

query_description = (
f'Make queries in resource {cls.__name__} and filter the result using query parameters. \n'
f'The items are paginated, to iterate over them use the `next_page_uri` included in response. \n'
f'If you need only a counter not the data send value `true` in `count` param.'
f"Make queries in resource {cls.__name__} and filter the result using query parameters. \n"
f"The items are paginated, to iterate over them use the 'next_page_uri' included in response. \n"
f"If you need only a counter not the data send value 'true' in 'count' param."
)

# Build dynamically types for query response
class QueryResponse(BaseModel):
items: Optional[List[response_model]] = []
next_page_uri: Optional[str] = None
count: Optional[int] = None

class Config(BaseConfig):
fields = {
'items': {
'description': f'List of {cls.__name__} that match with query filters'
},
'next_page_uri': {
'description': 'URL to fetch the next page of results'
},
'count': {
'description': f'Counter of {cls.__name__} objects that match with query filters. \n'
f'Included in response only if `count` param was `true`'
},
}
items: Optional[List[response_model]] = Field(
[],
description=f'List of {cls.__name__} that match with query filters',
)
next_page_uri: Optional[str] = Field(
None, description='URL to fetch the next page of results'
)
count: Optional[int] = Field(
None,
description=f'Counter of {cls.__name__} objects that match with query filters. \n'
'If you need only a counter not the data send value `true` in `count` param.',
)

QueryResponse.__name__ = f'QueryResponse{cls.__name__}'

Expand Down
4 changes: 2 additions & 2 deletions fast_agave/tasks/sqs_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from aiobotocore.httpsession import HTTPClientError
from aiobotocore.session import get_session
from pydantic import validate_arguments
from pydantic import validate_call

from ..exc import RetryTask

Expand Down Expand Up @@ -106,7 +106,7 @@ async def concurrency_controller(coro: Coroutine) -> None:

session = get_session()

task_with_validators = validate_arguments(task_func)
task_with_validators = validate_call(task_func)

async with session.create_client('sqs', region_name) as sqs:
async for message in message_consumer(
Expand Down
17 changes: 8 additions & 9 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
black==22.3.0
flake8==4.0.*
isort==5.10.*
black==24.10.0
flake8==5.0.4
isort==5.11.5
mock==4.0.3
mongomock==4.1.*
moto[server]==2.2.*
mypy==1.0.1
pytest==7.4.*
pytest-cov==4.1.*
moto[server]==5.0.25
mypy==1.4.1
pytest==7.4.4
pytest-cov==4.1.0
pytest-vcr==1.0.*
pytest-asyncio==0.15.*
requests==2.28.*
boto3==1.20.24
botocore==1.23.24
httpx==0.28.1
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
aiobotocore==2.1.0
cuenca-validations==0.11.19
fastapi==0.68.2
mongoengine-plus==0.0.3
aiobotocore==2.16.1
cuenca-validations==2.0.0.dev9
fastapi==0.115.6
mongoengine-plus==0.2.3.dev1
python-multipart==0.0.5
starlette-context==0.3.3
types-aiobotocore-sqs==2.1.0.post1
types-aiobotocore-sqs==2.5.0
20 changes: 12 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
packages=find_packages(),
include_package_data=True,
package_data=dict(agave=['py.typed']),
python_requires='>=3.8',
python_requires='>=3.9',
install_requires=[
'aiobotocore>=1.0.0,<3.0.0',
'types-aiobotocore-sqs>=2.1.0.post1,<3.0.0',
'cuenca-validations>=0.9.4,<1.0.0',
'fastapi>=0.63.0,<0.69.0',
'mongoengine-plus>=0.0.2,<1.0.0',
'starlette-context>=0.3.2,<0.4.0',
'aiobotocore==2.16.1',
'types-aiobotocore-sqs==2.5.0',
'cuenca-validations==2.0.0.dev9',
'fastapi==0.115.6',
'mongoengine-plus==0.2.3.dev1',
'starlette-context==0.3.3',
],
classifiers=[
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
Expand Down
3 changes: 1 addition & 2 deletions tests/blueprint/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def retrieve(self) -> str:


def test_copy_properties_from() -> None:
def retrieve():
...
def retrieve(): ... # noqa: E704

assert not hasattr(retrieve, 'i_am_test')
retrieve = copy_attributes(TestResource)(retrieve)
Expand Down
15 changes: 11 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime as dt
import functools
import os
import subprocess
from functools import partial
from typing import Callable, Dict, Generator, List

Expand Down Expand Up @@ -185,13 +184,17 @@ def aws_credentials() -> None:
def aws_endpoint_urls(
aws_credentials,
) -> Generator[Dict[str, str], None, None]:
sqs = subprocess.Popen(['moto_server', 'sqs', '-p', '4000'])
from moto.server import ThreadedMotoServer

server = ThreadedMotoServer(port=4000)
server.start()

endpoints = dict(
sqs='http://127.0.0.1:4000/',
)
yield endpoints
sqs.kill()

server.stop()


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -222,7 +225,11 @@ async def sqs_client():
session = aiobotocore.session.get_session()
async with session.create_client('sqs', 'us-east-1') as sqs:
await sqs.create_queue(
QueueName='core.fifo', Attributes={'FifoQueue': 'true'}
QueueName='core.fifo',
Attributes={
'FifoQueue': 'true',
'ContentBasedDeduplication': 'true',
},
)
resp = await sqs.get_queue_url(QueueName='core.fifo')
sqs.send_message = partial(sqs.send_message, QueueUrl=resp['QueueUrl'])
Expand Down
Loading
Loading