You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the jinja template, in the config section we iterate throught each key value of the config griffe object and we highlight the value. However the highlight method take a string as input and it will fail if another kind of value is took as input.
Now comes the issue, if you are using pydantic > 2.0 you will probably use the new way to define the config with model_dict being a ConfigDict. When you switch from the old config definition (with class Config) to the new here is the change in the data.extra.griffe_pydantic:
data.extra["griffe_pydantic"] ={
'fields': functools.partial(<function_model_fieldsat0x7400568c1bd0>, Class('TermObject', 6, 44)),
'validators': functools.partial(<function_model_validatorsat0x7400568c1c60>, Class('TermObject', 6, 44)),
'config': {
'json_schema_extra': ExprDict(
keys=["'example'"],
values=[ExprDict(keys=["'usage'", "'limitations'", "'billing'", "'notice_period'"],
values=["'Data can be used for reports, analytics and machine learning use cases.'", "'Not suitable for real-time use cases. Data may not be used to identify individual customers.'", "'5000 USD per month'", "'P3M'"]
)
]
)
}
}
In the first case you can see that there is no section about config, but in the second one there is a section about config. So in the first case the {% if class.extra.griffe_pydantic.config %} condition is not met so no issue, but with newer pydantic version the config now exist and looks like this:
{'json_schema_extra': ExprDict(keys=["'example'"], values=[ExprDict(keys=["'usage'", "'limitations'", "'billing'", "'notice_period'"], values=["'Data can be used for reports, analytics and machine learning use cases.'", "'Not suitable for real-time use cases. Data may not be used to identify individual customers.'", "'5000 USD per month'", "'P3M'"])])}
So here we see that it mapped strings to ExprDict, and highlight method does not like to get ExprDict as input which finally raise our error:
TypeError: expectedstringorbytes-likeobject
To Reproduce
Inorder to reproduce you can try to display this pydantic model:
fromtypingimportOptionalfrompydanticimportBaseModel, ConfigDict, FieldclassTermObject(BaseModel):
""" Represents the terms and conditions of a data contract. This class defines the structure for the 'terms' section of a data contract, including details such as usage terms, limitations, billing information, and notice period. """usage: Optional[str] =Field(
None,
description="The way the data is expected to be used. Can contain business and technical information.",
example="Data can be used for reports, analytics and machine learning use cases.",
)
limitations: Optional[str] =Field(
None,
description="Restrictions on how the data can be used, including technical limitations or usage restrictions.",
example="Not suitable for real-time use cases. Data may not be used to identify individual customers.",
)
billing: Optional[str] =Field(
None,
description="The pricing model for using the data (e.g., free, monthly fee, or metered pay-per-use).",
example="5000 USD per month",
)
notice_period: Optional[str] =Field(
None,
description="Period of time required to terminate or modify the data usage agreement. Uses ISO-8601 format.",
example="P3M",
)
model_config=ConfigDict(
json_schema_extra={
"example": {
"usage": "Data can be used for reports, analytics and machine learning use cases.",
"limitations": "Not suitable for real-time use cases. Data may not be used to identify individual customers.",
"billing": "5000 USD per month",
"notice_period": "P3M",
}
}
)
#class Config:# schema_extra = {# "example": {# "usage": "Data can be used for reports, analytics and machine learning use cases.",# "limitations": "Not suitable for real-time use cases. Data may not be used to identify individual customers.",# "billing": "5000 USD per month",# "notice_period": "P3M",# }# }
The expected behavior is to not fail even if some values of the config are not dict and to convert it before in string. Moreover the model_config field should not be displayed since it's now a special field dedicated to the config that will already be displayed.
Here is the suggestion:
<li><code>{{ name }}</code>: {{ value|string|highlight(language="python", inline=True) }}</li>
Description of the bug
In the jinja template, in the config section we iterate throught each key value of the config griffe object and we highlight the value. However the
highlight
method take a string as input and it will fail if another kind of value is took as input.Here is the targeted peace of code:
Now comes the issue, if you are using
pydantic
> 2.0 you will probably use the new way to define the config withmodel_dict
being aConfigDict
. When you switch from the old config definition (withclass Config
) to the new here is the change in thedata.extra.griffe_pydantic
:With
class Config
definition:With
ConfigDict
definition:In the first case you can see that there is no section about config, but in the second one there is a section about config. So in the first case the
{% if class.extra.griffe_pydantic.config %}
condition is not met so no issue, but with newer pydantic version the config now exist and looks like this:So here we see that it mapped strings to
ExprDict
, andhighlight
method does not like to getExprDict
as input which finally raise our error:To Reproduce
Inorder to reproduce you can try to display this pydantic model:
Full traceback
Expected behavior
The expected behavior is to not fail even if some values of the config are not dict and to convert it before in string. Moreover the
model_config
field should not be displayed since it's now a special field dedicated to the config that will already be displayed.Here is the suggestion:
Environment information
python: 3.10.12
Libs:
Tell me what you think about, I can do a PR to implement the changes if needed
The text was updated successfully, but these errors were encountered: