Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydantic 2 deprecation warnings #82

Open
ValeriyWorld opened this issue Dec 6, 2024 · 0 comments
Open

Pydantic 2 deprecation warnings #82

ValeriyWorld opened this issue Dec 6, 2024 · 0 comments

Comments

@ValeriyWorld
Copy link
Contributor

ValeriyWorld commented Dec 6, 2024

Using the old specification parallel to Pydantic v2 produces warnings using models package.

Warning content:
.../.../python3.9/site-packages/pydantic/fields.py:804: PydanticDeprecatedSince20: Using extra keyword arguments on Fieldis deprecated and will be removed. Usejson_schema_extra instead. (Extra keys: 'example'). Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.7/migration/

Roots of warning - models package, for example:

class BaseObject(BaseModel):
    oddrn: str = Field(..., example='//aws/glue/{account_id}/{database}/{tablename}')
    name: str
    version: Optional[str] = None
    description: Optional[str] = None
    owner: Optional[str] = Field(None, example='//aws/iam/{account_id}/user/name')
    metadata: Optional[List[MetadataExtension]] = None
    tags: Optional[List[Tag]] = None

according to pedantic v2 Field should not have example attribute. With Pydantic v3 it will be considered as error.

As our models are generated based on specification with use of datamodel-code-generator we are somewhat dependent on it.
Right now there is a way to modify the specification in the way Pydantic v2 will not throw errors, but with this approach OpenAPI (Swagger) used for documentation will not correctly understand the examples.

  • Old specification (Pydantic throws errors, documentation is OK):
    BaseObject:
      type: object
      properties:
        oddrn:
          type: string
          example: "//aws/glue/{account_id}/{database}/{tablename}"
  • Version where Pydantic will not throw errors, but will be problems displaying examples in documentation:
    BaseObject:
      type: object
      properties:
        oddrn:
          type: string
          json_schema_extra:
            examples:
              - "//aws/glue/{account_id}/{database}/{tablename}"

Invocation command for datamodel-code-generator:

datamodel-codegen --input ./opendatadiscovery-specification/specification/entities.yaml --output odd_models/models/models.py --input-file-type openapi --output-model-type pydantic_v2.BaseModel --field-extra-keys json_schema_extra

Maybe with later versions of datamodel-code-generator this will be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant