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

Improve typing #70

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
42 changes: 35 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,56 @@ name: Tests
on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
tool:
- flake8
- mypy
- black --diff --check

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.7
uses: actions/setup-python@v4
with:
python-version: "3.7"
cache: pip
cache-dependency-path: |
requirements/base.pip
requirements/test.pip
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements/test.pip
- name: Run ${{ matrix.tool }} static check
run: |
python3 -m ${{ matrix.tool }} flask_pydantic/ tests/

build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, macOS-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: |
requirements/base.pip
requirements/test.pip
- name: Install dependencies
if: steps.cache-pip.outputs.cache-hit != 'true'
run: |
python -m pip install --upgrade pip
pip install -r requirements/test.pip
- name: Test with pytest
run: |
python3 -m pytest
- name: Lint with flake8
run: |
flake8 .
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,44 @@ def my_route():
return MyModel(...)
```

### Generic model type

It's possible to use a validator on a generic model type. It can be useful,
e.g., when making a convenience API passthrough (example taken from
[headscale-webui](https://github.com/iFargle/headscale-webui/blob/cc67cf0e9cf0b33fd66d766ce74d86e0c9cda114/server.py#L186)
project):

```python
RequestT = TypeVar("RequestT", bound=Message)
ResponseT = TypeVar("ResponseT", bound=Message)

def api_passthrough(
route: str,
request_type: Type[RequestT],
api_method: Callable[[RequestT], ResponseT | str],
):
def api_passthrough_page(body: RequestT) -> ResponseT | str:
return api_method(body)

api_passthrough_page.__name__ = route.replace("/", "_")
api_passthrough_page.__annotations__ = {"body": request_type}

return app.route(route, methods=["POST"])(
validate()(api_passthrough_page)
)

api_passthrough(
"/api/endpoint1",
schema.GetMachineRequest,
backend_api_endpoint1,
)
api_passthrough(
"/api/endpoint2",
schema.DeleteMachineRequest,
backend_api_endpoint2,
)
```

### Example app

For more complete examples see [example application](https://github.com/bauerji/flask_pydantic/tree/master/example_app).
Expand Down
8 changes: 4 additions & 4 deletions flask_pydantic/converters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Type
from typing import Dict, List, Type, Union

from pydantic import BaseModel
from werkzeug.datastructures import ImmutableMultiDict
from werkzeug.datastructures import MultiDict


def convert_query_params(
query_params: ImmutableMultiDict, model: Type[BaseModel]
) -> dict:
query_params: "MultiDict[str, str]", model: Type[BaseModel]
) -> Dict[str, Union[str, List[str]]]:
"""
group query parameters into lists if model defines them

Expand Down
Loading