Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from dymmond/feature/update_pydantic_v2
Browse files Browse the repository at this point in the history
Pydantic 2.0
  • Loading branch information
tarsil authored Jul 4, 2023
2 parents 4f8d787 + 398d942 commit 5e0a291
Show file tree
Hide file tree
Showing 51 changed files with 437 additions and 460 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/1-issue.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ about: Please only raise an issue if you've been advised to do so after discussi

The starting point for issues should usually be a discussion...

https://github.com/dymmond/esmerald/discussions
https://github.com/dymmond/openapi-schemas-pydantic/discussions

Potential bugs may be raised as a "Potential Issue" discussion. The feature requests may be raised as an
"Ideas" discussion.
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
blank_issues_enabled: false
contact_links:
- name: Discussions
url: https://github.com/dymmond/esmerald/discussions
url: https://github.com/dymmond/openapi-schemas-pydantic/discussions
about: >
The "Discussions" forum is where you want to start.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: "actions/checkout@v3"
- uses: "actions/setup-python@v4"
with:
python-version: 3.7
python-version: 3.8
- name: "Install dependencies"
run: "scripts/install"
- name: Install build dependencies
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Test Suite

on:
push:
branches: ["main"]
branches:
- "**"
pull_request:
branches: ["main"]
schedule:
Expand All @@ -16,7 +17,7 @@ jobs:

strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- uses: "actions/checkout@v3"
Expand All @@ -31,8 +32,8 @@ jobs:
- name: "Install dependencies"
if: steps.cache.outputs.cache-hit != 'true'
run: "scripts/install"
# - name: "Run linting checks"
# run: "scripts/lint"
- name: "Run linting checks"
run: "scripts/lint"
# - name: "Build package"
# run: "scripts/build"
- name: "Run tests"
Expand Down
111 changes: 35 additions & 76 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,90 +1,49 @@
# See https://pre-commit.com for more information.
# See https://pre-commit.com/hooks.html for more hooks.
default_language_version:
python: python3.10
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-ast
- id: check-case-conflict
- id: check-merge-conflict
- id: debug-statements
- id: check-added-large-files
- id: check-toml
- id: check-yaml
args:
- --unsafe
- id: end-of-file-fixer
exclude: "\\.idea/(.)*"
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v2.38.0
rev: v2.37.3
hooks:
- id: pyupgrade
args: ["--py37-plus"]
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
args:
- --py3-plus
- --keep-runtime-typing
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.254
hooks:
- id: ruff
args: ["--fix", "--line-length=99"]
- repo: https://github.com/psf/black
rev: 22.8.0
hooks:
- id: black
args: [--config=./pyproject.toml]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.1
hooks:
- id: codespell
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
hooks:
- id: blacken-docs
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.0-alpha.0"
hooks:
- id: prettier
- repo: https://github.com/pycqa/bandit
rev: 1.7.4
hooks:
- id: bandit
exclude: "test_*"
args: ["-iii", "-ll", "-s=B308,B703"]
- repo: https://github.com/hadialqattan/pycln
rev: v2.1.1
hooks:
- id: pycln
args: [--config=pyproject.toml]
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.32.2
hooks:
- id: markdownlint
args: [--disable=MD013, --disable=MD033, --disable=MD050]
- repo: https://github.com/PyCQA/docformatter
rev: v1.5.0
hooks:
- id: docformatter
args: [--in-place]
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies:
[
"flake8-bugbear",
"flake8-comprehensions",
"flake8-mutable",
"flake8-print",
"flake8-simplify",
"flake8-type-checking",
"flake8-pytest-style",
"flake8-implicit-str-concat",
"flake8-noqa",
]
- repo: https://github.com/johnfraney/flake8-markdown
rev: v0.4.0
hooks:
- id: flake8-markdown
- repo: https://github.com/pycqa/pylint
rev: "v2.15.2"
hooks:
- id: pylint
exclude: "test_*|docs"
args: ["--unsafe-load-any-extension=y"]
additional_dependencies: [pydantic, mkdocs_gen_files]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.971"
args: ["--line-length=99"]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: mypy
additional_dependencies: [pydantic, mkdocs_gen_files]
- id: isort
name: isort (python)
args: ["--project=openapi_schemas_pydantic", "--line-length=99"]
- id: isort
name: isort (cython)
types: [cython]
args: ["--project=openapi_schemas_pydantic", "--line-length=99"]
- id: isort
name: isort (pyi)
types: [pyi]
args: ["--project=openapi_schemas_pydantic", "--line-length=99"]
ci:
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate
2 changes: 1 addition & 1 deletion docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ To develop for OpenAPI Schemas, create a fork of the [OpenAPI Schemas Pydantic r
After, clone your fork with the follow command replacing `YOUR-USERNAME` wih your GitHub username:

```shell
$ git clone https://github.com/YOUR-USERNAME/esmerald
$ git clone https://github.com/YOUR-USERNAME/openapi_schemas_pydantic
```

### Install the project dependencies
Expand Down
9 changes: 7 additions & 2 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Release Notes

## 1.0.0
## 2.0.0

### Changed

October 23, 2022.
- This can cause breaking changes. This is an upgrade to pydantic 2.0 internally. When installing it,
please be aware this will require at least pydantic 2.0 in your system.

## 1.0.0

### Added

Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ theme:
- search.highlight
- content.tabs.link

repo_name: dymmond/esmerald
repo_name: dymmond/openapi-schemas-pydantic
repo_url: https://github.com/dymmond/openapi-schemas-pydantic
edit_uri: ""
plugins:
Expand Down
13 changes: 6 additions & 7 deletions openapi_schemas_pydantic/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import deque
from datetime import date, datetime, time, timedelta
from typing import Any, Dict, Pattern, Type, Union
from typing import Any, Callable, Dict, Pattern, Type, Union
from uuid import UUID

from pydantic import (
Expand All @@ -24,11 +24,9 @@
NegativeInt,
NonNegativeInt,
NonPositiveFloat,
PaymentCardNumber,
PositiveFloat,
PositiveInt,
PostgresDsn,
PyObject,
RedisDsn,
SecretBytes,
SecretStr,
Expand All @@ -38,7 +36,7 @@
StrictInt,
StrictStr,
)
from pydantic.fields import (
from pydantic.v1.fields import (
SHAPE_DEFAULTDICT,
SHAPE_DEQUE,
SHAPE_DICT,
Expand All @@ -50,10 +48,13 @@
SHAPE_TUPLE,
SHAPE_TUPLE_ELLIPSIS,
)
from pydantic_extra_types.payment import PaymentCardNumber

from openapi_schemas_pydantic.utils.enums import OpenAPIFormat, OpenAPIType
from openapi_schemas_pydantic.v3_1_0.schema import Schema

PyObject = Callable[..., Any]

PYDANTIC_FIELD_SHAPE_MAP: Dict[int, OpenAPIType] = {
SHAPE_LIST: OpenAPIType.ARRAY,
SHAPE_SET: OpenAPIType.ARRAY,
Expand Down Expand Up @@ -216,9 +217,7 @@
"gt": "exclusiveMinimum",
"max_length": "maxLength",
"min_length": "minLength",
"max_items": "maxItems",
"min_items": "minItems",
"regex": "pattern",
"pattern": "pattern",
"title": "title",
"description": "description",
}
Expand Down
23 changes: 13 additions & 10 deletions openapi_schemas_pydantic/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import TYPE_CHECKING, Any, Set, Type, TypeVar, cast

from pydantic import BaseModel, create_model
from pydantic.schema import schema
from pydantic.json_schema import models_json_schema

from openapi_schemas_pydantic import v3_1_0

Expand All @@ -10,6 +10,7 @@

REF_PREFIX = "#/components/schemas/"
SCHEMA_NAME_ATTRIBUTE = "__schema_name__"
MODE = "validation"

T = TypeVar("T", bound=v3_1_0.OpenAPI)

Expand All @@ -34,7 +35,7 @@ def construct_open_api_with_schema_class(
new OpenAPI object with "#/components/schemas" values updated. If there is no update in
"#/components/schemas" values, the original `open_api` will be returned.
"""
copied_schema = open_api_schema.copy(deep=True)
copied_schema = open_api_schema.model_copy(deep=True)
schema_classes = list(
extract_pydantic_types_to_openapi_components(obj=copied_schema, ref_class=v3_1_0.Reference)
)
Expand All @@ -48,17 +49,19 @@ def construct_open_api_with_schema_class(
copied_schema.components.schemas = cast("Dict[str, Any]", {})

schema_classes = [
cls
if not hasattr(cls, "__schema_name__")
else create_model(getattr(cls, SCHEMA_NAME_ATTRIBUTE), __base__=cls)
(cls, MODE)
if not hasattr(cls, SCHEMA_NAME_ATTRIBUTE)
else (create_model(getattr(cls, SCHEMA_NAME_ATTRIBUTE), __base__=cls), MODE)
for cls in schema_classes
]
schema_classes.sort(key=lambda x: x.__name__)
schema_definitions = schema(schema_classes, ref_prefix=REF_PREFIX)["definitions"]

schema_classes.sort(key=lambda x: x[0].__name__)
_, json_schema = models_json_schema(schema_classes)

copied_schema.components.schemas.update(
{
key: v3_1_0.Schema.parse_obj(schema_dict)
for key, schema_dict in schema_definitions.items()
key: v3_1_0.Schema.model_validate(schema_dict)
for key, schema_dict in json_schema["$defs"].items()
}
)
return copied_schema
Expand All @@ -80,7 +83,7 @@ def extract_pydantic_types_to_openapi_components(
"""
pydantic_schemas: Set[Type[BaseModel]] = set()
if isinstance(obj, BaseModel):
fields = obj.__fields_set__
fields = obj.model_fields_set
for field in fields:
child_obj = getattr(obj, field)
if isinstance(child_obj, OpenAPI310PydanticSchema):
Expand Down
7 changes: 5 additions & 2 deletions openapi_schemas_pydantic/v3_1_0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
from .xml import XML

# resolve forward references
Encoding.update_forward_refs(Header=Header)
Schema.update_forward_refs()
Encoding.model_rebuild()
Schema.model_rebuild()
Header.model_rebuild()
Reference.model_rebuild()
Operation.model_rebuild()

__all__ = [
"Callback",
Expand Down
Loading

0 comments on commit 5e0a291

Please sign in to comment.