Skip to content

Commit

Permalink
Disallow extra keys in pydantic where it makes sense (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Sep 12, 2023
1 parent 182f51e commit 818e363
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install .
python -m pip install . pytest
- uses: jakebailey/pyright-action@v1

tests:
name: basic import test
name: unit tests
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -52,5 +52,5 @@ jobs:
allow-prereleases: true
cache: pip
cache-dependency-path: pyproject.toml
- run: pip install -e .[dev]
- run: python3 -c 'import fastapi_poe'
- run: pip install -e .[dev] pytest
- run: pytest tests/
8 changes: 7 additions & 1 deletion src/fastapi_poe/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field
from typing_extensions import Literal, TypeAlias

Identifier: TypeAlias = str
Expand Down Expand Up @@ -77,6 +77,8 @@ class ReportErrorRequest(BaseRequest):


class SettingsResponse(BaseModel):
model_config = ConfigDict(extra="forbid")

context_clear_window_secs: Optional[int] = None # deprecated
allow_user_context_clear: bool = True # deprecated
server_bot_dependencies: Dict[str, int] = Field(default_factory=dict)
Expand All @@ -87,6 +89,10 @@ class SettingsResponse(BaseModel):
class PartialResponse(BaseModel):
"""Representation of a (possibly partial) response from a bot."""

# These objects are usually instantiated in user code, so we
# disallow extra fields to prevent mistakes.
model_config = ConfigDict(extra="forbid")

text: str
"""Partial response text.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pydantic
import pytest

from fastapi_poe.types import PartialResponse


def test_extra_attrs():
with pytest.raises(pydantic.ValidationError):
PartialResponse(text="hi", replaceResponse=True) # type: ignore

resp = PartialResponse(text="a capybara", is_replace_response=True)
assert resp.is_replace_response is True
assert resp.text == "a capybara"

0 comments on commit 818e363

Please sign in to comment.