Skip to content

Commit

Permalink
Update Workflows + Optional Type Support (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Apr 17, 2024
1 parent 6cfffdc commit 0c33188
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Deploy SDK Docs
on:
push:
branches:
- master
- main
workflow_dispatch:

permissions:
Expand Down
51 changes: 51 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Unit Tests

on:
push:
branches: [main]
pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: install deps
run: |
pip install poetry poethepoet
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.12
cache: poetry
cache-dependency-path: "poetry.lock"
- name: Poetry install
run: |
poetry install --with dev,test,email
- name: Run tests
run: |
poe test
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: install deps
run: |
pip install poetry poethepoet
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.12
cache: poetry
cache-dependency-path: "poetry.lock"
- name: Poetry install
run: |
poetry install --with dev
- name: Lint code
run: |
poe lint
2 changes: 2 additions & 0 deletions dydantic/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ def _json_schema_to_pydantic_field(
field_kwargs["min_length"] = json_schema["minLength"]
if "maxLength" in json_schema:
field_kwargs["max_length"] = json_schema["maxLength"]
if not is_required:
type_ = Optional[type_]
return (type_, Field(default, json_schema_extra=field_kwargs))


Expand Down
42 changes: 40 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dydantic"
version = "0.0.5"
version = "0.0.6"
description = "Dynamically generate pydantic models from JSON schema."
authors = ["William Fu-Hinthorn <[email protected]>"]
license = "MIT"
Expand All @@ -9,6 +9,10 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.9"
pydantic = "^2"
email-validator = { version = "^2.1", optional = true }

[tool.poetry.extras]
email = ["email-validator"]

[tool.poetry.group.dev.dependencies]
ruff = "^0.3.5"
Expand All @@ -20,6 +24,9 @@ mypy = "^1.9.0"
poethepoet = "^0.25.0"
pytest = "^8.1.1"

[tool.poetry.group.email.dependencies]
email-validator = "^2.1.0"


[tool.poetry.group.docs.dependencies]
mkdocs = "^1.5.3"
Expand All @@ -43,24 +50,13 @@ build-backend = "poetry.core.masonry.api"
ruff = "ruff"
black = "black"
mypy = "mypy"
format = [
"ruff check --fix .",
"black .",
"ruff format ."
]
lint = [
"ruff check .",
"mypy ."
]
format = ["ruff check --fix .", "black .", "ruff format ."]
lint = ["ruff check .", "mypy ."]
pytest = "poetry run pytest"
test = "poetry run pytest tests"
doctest = "pytest --doctest-modules dydantic"

mkdocs = "poetry run mkdocs"
build-docs ="mkdocs build --clean -f docs/mkdocs.yml"

serve-docs = [
"build-docs",
"mkdocs serve -f docs/mkdocs.yml",
]
build-docs = "mkdocs build --clean -f docs/mkdocs.yml"

serve-docs = ["build-docs", "mkdocs serve -f docs/mkdocs.yml"]
3 changes: 2 additions & 1 deletion tests/test_big_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ def test_create_model_from_dino_pika_schema(input, should_error):
with pytest.raises(ValidationError):
model.model_validate(input)
else:
model.model_validate(input)
result = model.model_validate(input)
model.model_validate(result.model_dump())
5 changes: 3 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,15 @@ class UuidModel(BaseModel):
def test_create_model_from_schema_formats(model: Type[BaseModel], inputs: dict):
dynamic_model = create_model_from_schema(model.model_json_schema())
dynamic_model.schema_json() # test it is serializable
dynamic_model.model_json_schema() # test it is serializable
dynamic_model.model_json_schema() # test it is serializable using the v2 schema
error = None
try:
model.model_validate(inputs)
except Exception as e:
error = str(e)
if not error:
dynamic_model.model_validate(inputs)
result = dynamic_model.model_validate(inputs)
dynamic_model.model_validate(result.model_dump())
else:
with pytest.raises(Exception):
dynamic_model.model_validate(inputs)

0 comments on commit 0c33188

Please sign in to comment.