Skip to content

Commit 40adf78

Browse files
koxudaxiclaude
andauthored
Fix empty array items to generate List[Any] instead of bare List (#2584)
* Fix handling of empty items in lists to use List[Any] for mypy compliance * Update remaining expected test files for List[Any] change 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 930fa74 commit 40adf78

18 files changed

Lines changed: 77 additions & 31 deletions

File tree

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,15 +1364,20 @@ def parse_array_fields(
13641364
else:
13651365
items = []
13661366

1367+
if items:
1368+
item_data_types = self.parse_list_item(
1369+
name,
1370+
items,
1371+
path,
1372+
obj,
1373+
singular_name=singular_name,
1374+
)
1375+
else:
1376+
item_data_types = [self.data_type_manager.get_data_type(Types.any)]
1377+
13671378
data_types: list[DataType] = [
13681379
self.data_type(
1369-
data_types=self.parse_list_item(
1370-
name,
1371-
items,
1372-
path,
1373-
obj,
1374-
singular_name=singular_name,
1375-
),
1380+
data_types=item_data_types,
13761381
is_list=True,
13771382
)
13781383
]

tests/data/expected/main/jsonschema/autodetect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from typing import List, Optional
7+
from typing import Any, List, Optional
88

99
from pydantic import BaseModel, Field, conint
1010

@@ -15,5 +15,5 @@ class Person(BaseModel):
1515
age: Optional[conint(ge=0)] = Field(
1616
None, description='Age in years which must be equal to or greater than zero.'
1717
)
18-
friends: Optional[List] = None
18+
friends: Optional[List[Any]] = None
1919
comment: None = None

tests/data/expected/main/jsonschema/custom_formatters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99
from __future__ import annotations
1010

11-
from typing import List, Optional
11+
from typing import Any, List, Optional
1212

1313
from pydantic import BaseModel, Field, conint
1414

@@ -19,5 +19,5 @@ class Person(BaseModel):
1919
age: Optional[conint(ge=0)] = Field(
2020
None, description='Age in years which must be equal to or greater than zero.'
2121
)
22-
friends: Optional[List] = None
22+
friends: Optional[List[Any]] = None
2323
comment: None = None

tests/data/expected/main/jsonschema/forwarding_reference/forwarding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class Model(BaseModel):
1414

1515

1616
class ForwardingArray(BaseModel):
17-
__root__: List
17+
__root__: List[Any]

tests/data/expected/main/jsonschema/general.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from typing import List, Optional
7+
from typing import Any, List, Optional
88

99
from pydantic import BaseModel, Field, conint
1010

@@ -15,5 +15,5 @@ class Person(BaseModel):
1515
age: Optional[conint(ge=0)] = Field(
1616
None, description='Age in years which must be equal to or greater than zero.'
1717
)
18-
friends: Optional[List] = None
18+
friends: Optional[List[Any]] = None
1919
comment: None = None

tests/data/expected/main/jsonschema/general_dataclass_frozen_kw_only.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from __future__ import annotations
66

77
from dataclasses import dataclass
8-
from typing import List, Optional
8+
from typing import Any, List, Optional
99

1010

1111
@dataclass(frozen=True, kw_only=True)
1212
class Person:
1313
firstName: Optional[str] = None
1414
lastName: Optional[str] = None
1515
age: Optional[int] = None
16-
friends: Optional[List] = None
16+
friends: Optional[List[Any]] = None
1717
comment: None = None

tests/data/expected/main/jsonschema/invalid_import_name/array_commons_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ class Commons(RootModel[Any]):
1313
root: Any = Field(..., description='Commons objects', title='Commons')
1414

1515

16-
class DefaultArray(RootModel[List]):
17-
root: List = Field(..., max_length=100, min_length=1)
16+
class DefaultArray(RootModel[List[Any]]):
17+
root: List[Any] = Field(..., max_length=100, min_length=1)

tests/data/expected/main/jsonschema/invalid_model_name.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from typing import List, Optional
7+
from typing import Any, List, Optional
88

99
from pydantic import BaseModel, Field, conint
1010

@@ -15,5 +15,5 @@ class ValidModelName(BaseModel):
1515
age: Optional[conint(ge=0)] = Field(
1616
None, description='Age in years which must be equal to or greater than zero.'
1717
)
18-
friends: Optional[List] = None
18+
friends: Optional[List[Any]] = None
1919
comment: None = None

tests/data/expected/main/jsonschema/items_boolean.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from __future__ import annotations
66

7-
from typing import List, Optional
7+
from typing import Any, List, Optional
88

99
from pydantic import BaseModel
1010

1111

1212
class Model(BaseModel):
13-
example: Optional[List] = None
13+
example: Optional[List[Any]] = None
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# generated by datamodel-codegen:
2+
# filename: empty_items_array.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from typing import Any, Dict, List, Optional
8+
9+
from pydantic import BaseModel
10+
11+
12+
class Model(BaseModel):
13+
datum_kwargs: Optional[Dict[str, List[Any]]] = None
14+
simple_list: Optional[List[Any]] = None

0 commit comments

Comments
 (0)