Skip to content

Commit df3cc08

Browse files
committed
feat: update code generation
- set datamodel-code-generator>=0.51.0 - set python >= 3.10 - use union operator instead of Optional
1 parent a221e11 commit df3cc08

15 files changed

Lines changed: 239 additions & 66 deletions

File tree

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ package_dir =
4141
=src
4242

4343
# Require a min/specific Python version (comma-separated conditions)
44-
# python_requires = >=3.8
44+
python_requires = >=3.10
4545

4646
# Add here dependencies of your project (line-separated), e.g. requests>=2.2,<3.0.
4747
# Version specifiers like >=2.2,<3.0 avoid problems due to API changes in
@@ -50,7 +50,7 @@ package_dir =
5050
install_requires =
5151
importlib-metadata; python_version<"3.8"
5252
pydantic>=2.0.0
53-
datamodel-code-generator>=0.43.1
53+
datamodel-code-generator>=0.51.0
5454
typing_extensions
5555
pyld
5656
rdflib

src/oold/generator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ def generate(
115115
field_include_all_keys=True,
116116
base_class=base_class,
117117
# use_default = True,
118-
enum_field_as_literal="all",
118+
allof_class_hierarchy=(
119+
datamodel_code_generator.AllOfClassHierarchy.Always,
120+
),
121+
enum_field_as_literal=datamodel_code_generator.LiteralType.Off,
119122
use_title_as_name=True,
120123
use_schema_description=True,
121124
use_field_description=True,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Generated by oold.generator
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# generated by datamodel-codegen:
2+
# filename: Entity.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic.v1 import Field
7+
8+
from oold.model.v1 import LinkedBaseModel
9+
10+
11+
class SimpleSubSchema(LinkedBaseModel):
12+
class Config:
13+
schema_extra = {"title": "SimpleSubSchema"}
14+
15+
some_property: str | None = None
16+
17+
18+
class NestedSubSchema(LinkedBaseModel):
19+
"""
20+
An example subschema
21+
"""
22+
23+
class Config:
24+
schema_extra = {
25+
"title": "NestedSubSchema",
26+
"description": "An example subschema",
27+
}
28+
29+
subschema: SimpleSubSchema | None = Field(None, title="SimpleSubSchema")
30+
31+
32+
class Entity(LinkedBaseModel):
33+
class Config:
34+
schema_extra = {"title": "Entity"}
35+
36+
nested: NestedSubSchema | None = Field(
37+
None, title="EntityNestedSubSchema", x_custom_annotation="custom value"
38+
)
39+
"""
40+
An example using a subschema
41+
"""
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# generated by datamodel-codegen:
2+
# filename: Entity.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import ConfigDict, Field
7+
8+
from oold.model import LinkedBaseModel
9+
10+
11+
class SimpleSubSchema(LinkedBaseModel):
12+
model_config = ConfigDict(
13+
json_schema_extra={"title": "SimpleSubSchema"},
14+
)
15+
some_property: str | None = None
16+
17+
18+
class NestedSubSchema(LinkedBaseModel):
19+
"""
20+
An example subschema
21+
"""
22+
23+
model_config = ConfigDict(
24+
json_schema_extra={
25+
"title": "NestedSubSchema",
26+
"description": "An example subschema",
27+
},
28+
)
29+
subschema: SimpleSubSchema | None = Field(None, title="SimpleSubSchema")
30+
31+
32+
class Entity(LinkedBaseModel):
33+
model_config = ConfigDict(
34+
json_schema_extra={"title": "Entity"},
35+
)
36+
nested: NestedSubSchema | None = Field(
37+
None,
38+
json_schema_extra={"x-custom-annotation": "custom value"},
39+
title="EntityNestedSubSchema",
40+
)
41+
"""
42+
An example using a subschema
43+
"""

tests/data/enum_docstrings/model_v1.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from __future__ import annotations
55

66
from enum import Enum
7-
from typing import Optional
87

98
from pydantic.v1 import Field
109

@@ -34,8 +33,8 @@ class Example(LinkedBaseModel):
3433
class Config:
3534
schema_extra = {"title": "Example"}
3635

37-
type: Optional[str] = ["example"]
38-
hobby: Optional[Hobby] = Field(
36+
type: str | None = ["example"]
37+
hobby: Hobby | None = Field(
3938
None,
4039
options={
4140
"enum_titles": [

tests/data/enum_docstrings/model_v2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from __future__ import annotations
55

66
from enum import Enum
7-
from typing import Optional
87

98
from pydantic import ConfigDict, Field
109

@@ -34,8 +33,8 @@ class Example(LinkedBaseModel):
3433
model_config = ConfigDict(
3534
json_schema_extra={"title": "Example"},
3635
)
37-
type: Optional[str] = ["example"]
38-
hobby: Optional[Hobby] = Field(
36+
type: str | None = ["example"]
37+
hobby: Hobby | None = Field(
3938
None,
4039
json_schema_extra={
4140
"x-enum-varnames": ["SPORTS", "MUSIC", "ART"],

tests/data/oneof_subschema/model_v1.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
from __future__ import annotations
55

6-
from typing import Optional, Union
7-
86
from pydantic.v1 import Field
97

108
from oold.model.v1 import LinkedBaseModel
@@ -14,24 +12,22 @@ class Subschema1(LinkedBaseModel):
1412
class Config:
1513
schema_extra = {"custom_key": "custom_value", "title": "Subschema1"}
1614

17-
subprop0: Optional[str] = Field(None, custom_key="custom_value_0")
18-
subprop1: Optional[str] = Field(None, custom_key="custom_value_1")
15+
subprop0: str | None = Field(None, custom_key="custom_value_0")
16+
subprop1: str | None = Field(None, custom_key="custom_value_1")
1917

2018

2119
class Subschema2(LinkedBaseModel):
2220
class Config:
2321
schema_extra = {"custom_key": "custom_value", "title": "Subschema2"}
2422

25-
subprop0: Optional[str] = Field(None, custom_key="custom_value_0")
26-
subprop2: Optional[str] = Field(None, custom_key="custom_value_2")
23+
subprop0: str | None = Field(None, custom_key="custom_value_0")
24+
subprop2: str | None = Field(None, custom_key="custom_value_2")
2725

2826

2927
class Example(LinkedBaseModel):
3028
class Config:
3129
schema_extra = {"title": "Example"}
3230

33-
type: Optional[str] = ["example"]
34-
prop1: Optional[str] = Field(None, custom_key="custom_value")
35-
prop2: Optional[Union[Subschema1, Subschema2]] = Field(
36-
None, custom_key="custom_value"
37-
)
31+
type: str | None = ["example"]
32+
prop1: str | None = Field(None, custom_key="custom_value")
33+
prop2: Subschema1 | Subschema2 | None = Field(None, custom_key="custom_value")

tests/data/oneof_subschema/model_v2.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
from __future__ import annotations
55

6-
from typing import Optional, Union
7-
86
from pydantic import ConfigDict, Field
97

108
from oold.model import LinkedBaseModel
@@ -14,10 +12,10 @@ class Subschema1(LinkedBaseModel):
1412
model_config = ConfigDict(
1513
json_schema_extra={"custom_key": "custom_value", "title": "Subschema1"},
1614
)
17-
subprop0: Optional[str] = Field(
15+
subprop0: str | None = Field(
1816
None, json_schema_extra={"custom_key": "custom_value_0"}
1917
)
20-
subprop1: Optional[str] = Field(
18+
subprop1: str | None = Field(
2119
None, json_schema_extra={"custom_key": "custom_value_1"}
2220
)
2321

@@ -26,10 +24,10 @@ class Subschema2(LinkedBaseModel):
2624
model_config = ConfigDict(
2725
json_schema_extra={"custom_key": "custom_value", "title": "Subschema2"},
2826
)
29-
subprop0: Optional[str] = Field(
27+
subprop0: str | None = Field(
3028
None, json_schema_extra={"custom_key": "custom_value_0"}
3129
)
32-
subprop2: Optional[str] = Field(
30+
subprop2: str | None = Field(
3331
None, json_schema_extra={"custom_key": "custom_value_2"}
3432
)
3533

@@ -38,8 +36,8 @@ class Example(LinkedBaseModel):
3836
model_config = ConfigDict(
3937
json_schema_extra={"title": "Example"},
4038
)
41-
type: Optional[str] = ["example"]
42-
prop1: Optional[str] = Field(None, json_schema_extra={"custom_key": "custom_value"})
43-
prop2: Optional[Union[Subschema1, Subschema2]] = Field(
39+
type: str | None = ["example"]
40+
prop1: str | None = Field(None, json_schema_extra={"custom_key": "custom_value"})
41+
prop2: Subschema1 | Subschema2 | None = Field(
4442
None, json_schema_extra={"custom_key": "custom_value"}
4543
)

tests/data/quantities/model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from __future__ import annotations
55

66
from enum import Enum
7-
from typing import List, Optional
7+
from typing import Optional
88

99
from pydantic import ConfigDict, Field
1010

@@ -36,7 +36,7 @@ class Bar2(LinkedBaseModel):
3636
},
3737
)
3838
id: Optional[str] = None
39-
type: Optional[List[str]] = ["Bar2"]
39+
type: Optional[list[str]] = ["Bar2"]
4040
prop1: Optional[str] = None
4141
unit: Optional[LengthUnit] = Field(
4242
None,
@@ -57,7 +57,7 @@ class Bar(Bar2):
5757
model_config = ConfigDict(
5858
json_schema_extra={"@context": ["./bar2/Bar2.json"], "title": "Bar"},
5959
)
60-
type: Optional[List[str]] = ["Bar"]
60+
type: Optional[list[str]] = ["Bar"]
6161
prop2: Optional[str] = None
6262
unit: Optional[str] = Field(
6363
None,
@@ -71,7 +71,7 @@ class Foo(LinkedBaseModel):
7171
json_schema_extra={"title": "Foo"},
7272
)
7373
id: Optional[str] = None
74-
type: Optional[List[str]] = ["Foo"]
74+
type: Optional[list[str]] = ["Foo"]
7575
literal: Optional[str] = None
7676
b: Optional[Bar] = Field(None, json_schema_extra={"range": "Bar.json"})
77-
b2: Optional[List[Bar]] = Field(None, json_schema_extra={"range": "Bar.json"})
77+
b2: Optional[list[Bar]] = Field(None, json_schema_extra={"range": "Bar.json"})

0 commit comments

Comments
 (0)