Is your feature request related to a problem? Please describe.
When I convert an array like following with datamodel-codegen --input openapi.yaml --output model.py --output-model-type pydantic_v2.BaseModel
,
openapi: 3.0.3
components:
schemas:
MyItem:
type: object
properties:
foo:
type: number
bar:
type: string
MyArray:
type: array
items:
$ref: "#/components/schemas/MyItem"
MyArray
cannot be used like a list because it lacks methods that collections should have.
For example, the following code prints ('root', [MyItem(foo=123.0, bar='text')])
instead of MyItem(foo=123.0, bar='text')
.
from model import MyArray, MyItem
arr = MyArray([MyItem(foo=123, bar="text")])
for item in arr:
print(item)
Describe the solution you'd like
It would be nice to generate the methods that collections should have, e.g., __getitem__
, __iter__
, or __len__
:
class MyItem(BaseModel):
foo: Optional[float] = None
bar: Optional[str] = None
class MyArray(RootModel[List[MyItem]]):
root: List[MyItem]
def __getitem__(self, index):
return self.root[index]
def __iter__(self):
return iter(self.root)
def __len__(self):
return len(self.root)
This would be a common use case so it would be great that some CLI option enables this feature.
Describe alternatives you've considered
Writing a collection methods manually on every generated RootModel
, or pydantic might have some other pretty solutions.
Additional context
Other collection methods like __setitem__
, __delitem__
, __contains__
, or some other methods list-like collections have
(append
, extend
, insert
, remove
, pop
, clear
, or index
) might be better to generated as well.
Sorry if this request was a duplicate.
Thank you very much for the great tool.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too