我正在尝试从python数据类生成JSON,这些类包含作为参数的其他数据类的列表。除了生成json模式之外,我并不真正关心pydantic的任何功能,因为我只是使用输出来自动生成web前端。因此,使用其他库的解决方案是可以的。
from typing import List
from dataclasses import dataclass
import pydantic
@dataclass()
class SomeParameters:
a: int = 5
@dataclass()
class SomeMoreParameters:
another: List[SomeParameters]
pydantic_cls = pydantic.dataclasses.dataclass(SomeMoreParameters)
schema = pydantic_cls.__pydantic_model__.schema()下面的堆栈跟踪出现了以下代码错误(您可以使用SKIP_CYTHON=1 pip install --no-cache-dir --no-binary :all: pydantic安装pydantic,以便调试到其中):
Traceback (most recent call last):
File "/home/veith/Projects/job-ui-parametrization/debug.py", line 17, in <module>
schema = pydantic_cls.__pydantic_model__.schema()
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/main.py", line 647, in schema
s = model_schema(cls, by_alias=by_alias, ref_template=ref_template)
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 185, in model_schema
m_schema, m_definitions, nested_models = model_process_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 617, in model_process_schema
m_schema, m_definitions, nested_models = model_type_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 658, in model_type_schema
f_schema, f_definitions, f_nested_models = field_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 258, in field_schema
f_schema, f_definitions, f_nested_models = field_type_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 498, in field_type_schema
items_schema, f_definitions, f_nested_models = field_singleton_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 848, in field_singleton_schema
return field_singleton_sub_fields_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 736, in field_singleton_sub_fields_schema
return field_type_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 563, in field_type_schema
f_schema, f_definitions, f_nested_models = field_singleton_schema(
File "/home/veith/Projects/job-ui-parametrization/venv/lib/python3.9/site-packages/pydantic/schema.py", line 947, in field_singleton_schema
raise ValueError(f'Value not declarable with JSON Schema, field: {field}')
ValueError: Value not declarable with JSON Schema, field: name='_another' type=SomeParameters required=True
Process finished with exit code 1现在,根据JSON模式文档,一个事物数组是有效的。脓性文献使用整数列表,这也适用于我。我希望我也可以在列表中使用自定义类型。文档进一步声明了Nested dataclasses are supported both in dataclasses and normal models.
我做错了什么/没有做我应该做的事情吗?还是我误会了,这是不允许的?如果是这样的话,还有什么可供选择的?
发布于 2022-02-28 19:43:40
这是一个错误引入了脓性1.9。我提供了一个拉请求来解决这个问题:https://github.com/samuelcolvin/pydantic/pull/3819
发布于 2022-02-10 23:44:38
有趣的是,您的代码在Python3.9.1 (Windows)上为我工作。只是好奇,您使用的是什么版本的pydantic?
这是对我有用的代码。注意,您可以使用pydantic插入数据来稍微简化JSON模式的生成。
from typing import List
# from dataclasses import dataclass
from pydantic.dataclasses import dataclass
@dataclass
class SomeParameters:
a: int = 5
@dataclass
class SomeMoreParameters:
another: List[SomeParameters]
# pydantic_cls = pydantic.dataclasses.dataclass(SomeMoreParameters)
schema = SomeMoreParameters.__pydantic_model__.schema()
print(schema)输出:
{'title': 'SomeMoreParameters', 'type': 'object', 'properties': {'another': {'title': 'Another', 'type': 'array', 'items': {'$ref': '#/definitions/SomeParameters'}}}, 'required': ['another'], 'definitions': {'SomeParameters': {'title': 'SomeParameters', 'type': 'object', 'properties': {'a': {'title': 'A', 'default': 5, 'type': 'integer'}}}}}注意:我使用的是pydantic==1.8.2。
https://stackoverflow.com/questions/71048746
复制相似问题