Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 134 additions & 26 deletions src/datamodel_code_generator/parser/base.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tests/data/expected/main/builtin_import_cache_retention.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class TestObject(BaseModel):
test_string: Optional[str] = None


class Test(BaseModel):
TestObject_1: Optional[TestObject] = Field(None, alias='TestObject', title='TestObject')
2 changes: 2 additions & 0 deletions tests/data/expected/main/builtin_import_cache_retention.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TestObject:retained
Test:invalidated
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from typing import Any, Dict, Optional, Set

from pydantic import BaseModel


class Item(BaseModel):
values: Optional[Set[Dict[str, Any]]] = [{'a': 1}]


class Left(BaseModel):
item: Optional[Item] = None


class Model(BaseModel):
left: Optional[Left] = None
right: Optional[Left] = None
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Item:invalidated
Left:retained
Model:retained
12 changes: 12 additions & 0 deletions tests/data/expected/main/custom_import_cache_alias_invalidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

from datetime import date as date_aliased
from typing import Optional

from pydantic import AwareDatetime, BaseModel


class TypeDate(BaseModel):
cache_clear_history = ['empty', 'cached', 'cached']
date: Optional[date_aliased]
datetime: Optional[AwareDatetime] = None
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from msgspec import Struct


class Type1(Struct, tag_field='type_', tag='a'):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from typing import Union

from msgspec import Struct

from .. import type_4
from ..subfolder import type_5
from . import type_2
from .artificial_folder import type_1


class Type3(Struct, tag_field='type_', tag='c'):
pass


class Response(Struct):
inner: Union[type_1.Type1, type_2.Type2, Type3, type_4.Type4, type_5.Type5]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

from typing import Union

from msgspec import UNSET, Struct, UnsetType

from .artificial_folder import type_1


class Type2(Struct, tag_field='type_', tag='b'):
ref_type: Union[type_1.Type1, UnsetType] = UNSET
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from msgspec import Struct


class Type5(Struct, tag_field='type_', tag='e'):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from msgspec import Struct


class Type4(Struct, tag_field='type_', tag='d'):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
?:empty
Type1:cached
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
inner_folder/__init__.py
inner_folder/artificial_folder/__init__.py
subfolder/__init__.py
__init__.py
inner_folder/artificial_folder/type_1.py
subfolder/type_5.py
inner_folder/type_2.py
inner_folder/schema.py
type_4.py
43 changes: 43 additions & 0 deletions tests/data/expected/main/custom_import_cache_generic_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

from typing import Optional

from pydantic import BaseModel as _BaseModel
from pydantic import ConfigDict


class BaseModel(_BaseModel):
model_config = ConfigDict(
extra='forbid',
)


class Foo(BaseModel):
model_config = ConfigDict(
extra='allow',
)
cache_clear_history = ['empty', 'cached', 'empty']
x: Optional[int] = None


class Bar(BaseModel):
model_config = ConfigDict(
extra='forbid',
)
cache_clear_history = ['empty', 'cached', 'empty']
y: Optional[int] = None


class Baz(BaseModel):
cache_clear_history = ['empty', 'cached', 'empty']
z: Optional[int] = None


class Test(BaseModel):
model_config = ConfigDict(
extra='forbid',
)
cache_clear_history = ['empty', 'cached', 'empty']
foo: Foo
bar: Optional[Bar] = None
baz: Optional[Baz] = None
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from enum import Enum


class NestedState1(Enum):
field_1 = '1'
field_2 = '2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from enum import Enum


class NestedState2(Enum):
field_1 = '1'
field_2 = '2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from __future__ import annotations

from pydantic import BaseModel

from . import nested_state1


class Result1(BaseModel):
state: nested_state1.NestedState1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from __future__ import annotations

from pydantic import BaseModel

from . import nested_state2


class Result2(BaseModel):
state: nested_state2.NestedState2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from enum import Enum


class State(Enum):
field_1 = '1'
field_2 = '2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
?:empty
State:cached
?:empty
NestedState2:empty
?:empty
NestedState1:empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__init__.py
state.py
result2.py
result1.py
nested_state2.py
nested_state1.py
14 changes: 14 additions & 0 deletions tests/data/expected/main/custom_import_cache_invalidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from typing import Any, List, Optional

from pydantic import BaseModel, Field, conint


class Person(BaseModel):
cache_clear_history = ['empty', 'cached']
firstName: Optional[str] = Field(None, description="The person's first name.")
lastName: Optional[str] = Field(None, description="The person's last name.")
age: Optional[conint(ge=0)] = Field(None, description='Age in years which must be equal to or greater than zero.')
friends: Optional[List[Any]] = None
comment: None = Field(None)
24 changes: 24 additions & 0 deletions tests/data/expected/main/custom_import_cache_unique_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from enum import StrEnum
from typing import Optional, Set

from pydantic import BaseModel


class Status(StrEnum):
active = 'active'
inactive = 'inactive'
pending = 'pending'


class Item(BaseModel):
cache_clear_history = ['empty', 'cached', 'cached']
__hash__ = object.__hash__
name: Optional[str] = None


class Container(BaseModel):
cache_clear_history = ['empty', 'cached', 'empty', 'empty', 'empty', 'empty', 'cached']
statuses: Optional[Set[Status]] = None
items: Optional[Set[Item]] = None
39 changes: 39 additions & 0 deletions tests/data/jsonschema/unique_items_unhashable_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Model",
"type": "object",
"properties": {
"left": {
"type": "object",
"properties": {
"item": {
"type": "object",
"properties": {
"values": {
"type": ["array", "null"],
"items": {"type": "object"},
"uniqueItems": true,
"default": [{"a": 1}]
}
}
}
}
},
"right": {
"type": "object",
"properties": {
"item": {
"type": "object",
"properties": {
"values": {
"type": ["array", "null"],
"items": {"type": "object"},
"uniqueItems": true,
"default": [{"a": 1}]
}
}
}
}
}
}
}
Loading
Loading