Skip to content

Commit b103e7a

Browse files
authored
Merge pull request #18 from OpenPaymentNetwork/main
Fix typing compatibility for Python 3.9
2 parents 77b2c23 + ac6acc9 commit b103e7a

File tree

7 files changed

+40
-40
lines changed

7 files changed

+40
-40
lines changed

openapi_pydantic/compat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Compatibility layer to make this package usable with Pydantic 1 or 2"""
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, Optional
44

55
from pydantic.version import VERSION as PYDANTIC_VERSION
66

@@ -30,7 +30,7 @@
3030

3131
def ConfigDict(
3232
extra: Literal["allow", "ignore", "forbid"] = "allow",
33-
json_schema_extra: dict[str, Any] | None = None,
33+
json_schema_extra: Optional[dict[str, Any]] = None,
3434
populate_by_name: bool = True,
3535
) -> PydanticConfigDict:
3636
"""Stub for pydantic.ConfigDict in Pydantic 2"""
@@ -53,7 +53,7 @@ def models_json_schema(
5353
*,
5454
by_alias: bool = True,
5555
ref_template: str = "#/$defs/{model}",
56-
schema_generator: type | None = None,
56+
schema_generator: Optional[type] = None,
5757
) -> tuple[dict, dict[str, Any]]:
5858
"""Stub for pydantic.json_schema.models_json_schema in Pydantic 2"""
5959
...

openapi_pydantic/v3/v3_0_3/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ class Config:
602602
def schema_validate(
603603
obj: Any,
604604
*,
605-
strict: bool | None = None,
606-
from_attributes: bool | None = None,
607-
context: dict[str, Any] | None = None
605+
strict: Optional[bool] = None,
606+
from_attributes: Optional[bool] = None,
607+
context: Optional[dict[str, Any]] = None
608608
) -> Schema:
609609
...
610610

openapi_pydantic/v3/v3_0_3/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import logging
2-
from typing import TYPE_CHECKING, Any, Generic, List, Optional, Set, Type, TypeVar, cast
2+
from typing import (
3+
TYPE_CHECKING,
4+
Any,
5+
Generic,
6+
List,
7+
Optional,
8+
Set,
9+
Type,
10+
TypeVar,
11+
Union,
12+
cast,
13+
)
314

415
from pydantic import BaseModel
516

@@ -143,7 +154,7 @@ def construct_open_api_with_schema_class(
143154

144155
def _validate_schemas(
145156
schema_definitions: dict[str, Any]
146-
) -> dict[str, Reference | Schema]:
157+
) -> dict[str, Union[Reference, Schema]]:
147158
"""Convert JSON Schema definitions to parsed OpenAPI objects"""
148159
# Note: if an error occurs in schema_validate(), it may indicate that
149160
# the generated JSON schemas are not compatible with the version

openapi_pydantic/v3/v3_1_0/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,9 @@ class Config:
959959
def schema_validate(
960960
obj: Any,
961961
*,
962-
strict: bool | None = None,
963-
from_attributes: bool | None = None,
964-
context: dict[str, Any] | None = None
962+
strict: Optional[bool] = None,
963+
from_attributes: Optional[bool] = None,
964+
context: Optional[dict[str, Any]] = None
965965
) -> Schema:
966966
...
967967

tests/util/test_optional_and_computed.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import pytest
24

35
from openapi_pydantic import (
@@ -69,7 +71,7 @@ class ConfigDictExt(ConfigDict, total=False):
6971

7072
class SampleModel(BaseModel):
7173
req: bool
72-
opt: bool | None = None
74+
opt: Optional[bool] = None
7375

7476
@computed_field # type: ignore
7577
@property

tests/v3_0_3/test_optional_and_computed.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import pytest
24

35
from openapi_pydantic.compat import PYDANTIC_V2, JsonSchemaMode
@@ -72,7 +74,7 @@ class ConfigDictExt(ConfigDict, total=False):
7274

7375
class SampleModel(BaseModel):
7476
req: bool
75-
opt: bool | None = None
77+
opt: Optional[bool] = None
7678

7779
@computed_field # type: ignore
7880
@property

tox.ini

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,27 @@
11
[tox]
22
min_version = 4.0
3-
env_list = format, test_pydantic1, type_pydantic1, test_pydantic2, type_pydantic2, lint, py{3.9,3.10,3.11}
3+
env_list = format, lint, py{39,310,311}-pydantic{1,2}-{test,type}
44

55
[gh-actions]
66
python =
77
3.9: py39
88
3.10: py310
9-
3.11: py311, test_pydantic1, type_pydantic1, test_pydantic2, type_pydantic2, format, lint
9+
3.11: format, lint, py311
1010

11-
[testenv:test_pydantic1]
11+
[testenv]
1212
labels = core
1313
allowlist_externals = poetry
14+
# The "pydanticX:", "test:", and "type:" prefixes are Tox factor-conditional settings.
15+
# https://tox.wiki/en/3.4.0/config.html?highlight=conditional#factors-and-factor-conditional-settings
16+
# Note that "poetry add" changes pyproject.toml, but at least we
17+
# change it back when the tests finish.
1418
commands_pre =
15-
poetry install --only main --only test --no-root --all-extras
16-
poetry add pydantic^1
17-
commands = poetry run pytest -vv tests
18-
19-
[testenv:type_pydantic1]
20-
allowlist_externals = poetry
21-
commands_pre =
22-
poetry install --no-root --all-extras
23-
poetry add pydantic^1
24-
commands = poetry run mypy openapi_pydantic tests
25-
26-
[testenv:test_pydantic2]
27-
labels = core
28-
allowlist_externals = poetry
29-
commands_pre =
30-
poetry install --only main --only test --no-root --all-extras
31-
poetry add 'pydantic>=1.8'
32-
commands = poetry run pytest -vv tests
33-
34-
[testenv:type_pydantic2]
35-
allowlist_externals = poetry
36-
commands_pre =
19+
pydantic1: poetry add --lock pydantic<2
20+
pydantic2: poetry add --lock pydantic>=1.8
3721
poetry install --no-root --all-extras
38-
poetry add 'pydantic>=1.8'
39-
commands = poetry run mypy openapi_pydantic tests
22+
commands =
23+
test: poetry run pytest -vv tests
24+
type: poetry run mypy openapi_pydantic tests
4025

4126
[testenv:format]
4227
allowlist_externals = poetry

0 commit comments

Comments
 (0)