Fix Field deprecation warning resulting from building models with formatted strings #50

Merged
fredsonnenwald merged 1 commits from string_format into main 2025-09-15 22:18:20 +00:00
fredsonnenwald commented 2025-09-15 16:06:26 +00:00 (Migrated from github.com)

When building a data model using SchemaConverter.build() from a JSON schema with a string field having a format this warning is generated:

PydanticDeprecatedSince20: Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead. (Extra keys: 'format'). Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.11/migration/

This is a result of the "format" keyword being passed to Pydantic's Field(). This is a deprecated string constraint and no longer shown as possible in Pydantic's documentation.

To resolve this issue, the format property can instead be passed using the json_schema_extra keyword - part of Pydantic's support for customising the JSON schema.

The warning can be generated by either running the tests or using the following example code:

import warnings
from datetime import datetime
from jambo import SchemaConverter
from pydantic import BaseModel
from typing import Optional

warnings.simplefilter('always')


class TestModel(BaseModel):
    a_datetime: Optional[datetime] | None = None


schema = TestModel.model_json_schema()
print("Got schema")

rebuilt_model = SchemaConverter.build(schema)
print("Built model")

After this is PR is applied no other behaviour should change other than the warning is no longer generated.

This PR is a partial revert of fbbff0b.

When building a data model using `SchemaConverter.build()` from a JSON schema with a string field having a format this warning is generated: ``` PydanticDeprecatedSince20: Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead. (Extra keys: 'format'). Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.11/migration/ ``` This is a result of the "format" keyword being passed to Pydantic's `Field()`. This is a deprecated [string constraint](https://docs.pydantic.dev/latest/concepts/fields/#string-constraints) and no longer shown as possible in Pydantic's documentation. To resolve this issue, the format property can instead be passed using the `json_schema_extra` keyword - part of Pydantic's support for [customising the JSON schema](https://docs.pydantic.dev/latest/concepts/json_schema/#field-level-customization). The warning can be generated by either running the tests or using the following example code: ```python import warnings from datetime import datetime from jambo import SchemaConverter from pydantic import BaseModel from typing import Optional warnings.simplefilter('always') class TestModel(BaseModel): a_datetime: Optional[datetime] | None = None schema = TestModel.model_json_schema() print("Got schema") rebuilt_model = SchemaConverter.build(schema) print("Built model") ``` After this is PR is applied no other behaviour should change other than the warning is no longer generated. This PR is a partial revert of fbbff0b.
codecov[bot] commented 2025-09-15 16:06:57 +00:00 (Migrated from github.com)

Codecov Report

All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

## [Codecov](https://app.codecov.io/gh/HideyoshiNakazone/jambo/pull/50?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Vitor+Hideyoshi) Report :white_check_mark: All modified and coverable lines are covered by tests. :loudspeaker: Thoughts on this report? [Let us know!](https://github.com/codecov/feedback/issues/255)
HideyoshiNakazone commented 2025-09-15 22:13:12 +00:00 (Migrated from github.com)

Thanks @fredsonnenwald, i swore that i solved that issue, i'll pull this branch locally to test it but this simple fix seems solid to merge!

Thanks @fredsonnenwald, i swore that i solved that issue, i'll pull this branch locally to test it but this simple fix seems solid to merge!
HideyoshiNakazone commented 2025-09-15 22:18:16 +00:00 (Migrated from github.com)

Yeah... You were right i reverted the code by mistake, merging and releasing a post release for this

Yeah... You were right i reverted the code by mistake, merging and releasing a post release for this
Sign in to join this conversation.