Skip to content

Conversation

@cjwatson
Copy link
Contributor

@cjwatson cjwatson commented Sep 2, 2025

tests/test_multi_body_errors.py::test_openapi_schema failed with pydantic 2.12.0a1 due to
pydantic/pydantic#11987. Since we're testing the exact contents of the JSON schema, the easiest fix seems to be to add version-dependent handling for this.

@YuriiMotov
Copy link
Member

I think we should try to avoid adding new dependency.

Maybe something with dirty_equals?
Take a look at the code in the details

Details
from dirty_equals import IsOneOf

v1 = {
    "age": {
        "title": "Age",
        "anyOf": [
            {"exclusiveMinimum": 0.0, "type": "number"},
            {"type": "string"},
        ],
    }
}

v2 = {
    "age": {
        "title": "Age",
        "anyOf": [
            {"exclusiveMinimum": 0.0, "type": "number"},
            {"type": "string", "pattern": r"^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$"},
        ],
    }
}


expected = {
    "age": {
        "title": "Age",
        "anyOf": [
            {"exclusiveMinimum": 0.0, "type": "number"},
            IsOneOf(
                {"type": "string"},
                {"type": "string", "pattern": r"^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$"}
            ),
        ],
    }
}

assert v1 == expected
assert v2 == expected

@svlandeg svlandeg changed the title Prepare for pydantic 2.12.0 πŸ‘½οΈ Ensure compatibility with Pydantic 2.12.0 Sep 3, 2025
@svlandeg svlandeg added upgrade and removed internal labels Sep 3, 2025
@cjwatson
Copy link
Contributor Author

cjwatson commented Sep 7, 2025

I think we should try to avoid adding new dependency.

Maybe something with dirty_equals? Take a look at the code in the details

Ah yes, that works too, thanks. Amended.

@YuriiMotov
Copy link
Member

I just tried running this branch with pydantic 2.12.0a1, and I have 4 failed tests

FAILED tests/test_forms_single_model.py::test_send_all_data - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...
FAILED tests/test_forms_single_model.py::test_defaults - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...
FAILED tests/test_forms_single_model.py::test_invalid_data - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...
FAILED tests/test_forms_single_model.py::test_no_data - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...

@cjwatson
Copy link
Contributor Author

I just tried running this branch with pydantic 2.12.0a1, and I have 4 failed tests

FAILED tests/test_forms_single_model.py::test_send_all_data - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...
FAILED tests/test_forms_single_model.py::test_defaults - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...
FAILED tests/test_forms_single_model.py::test_invalid_data - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...
FAILED tests/test_forms_single_model.py::test_no_data - pydantic.warnings.UnsupportedFieldAttributeWarning: The 'alias' attribute with value 'with' was provided to the `Field()` function, which has no effect in the context it was used. 'alias' is field-spec...

These are due to pydantic/pydantic#12028. In that PR, @Viicos wrote:

I've reached out to the FastAPI team regarding the failing tests. They will need to address them in some way.

But I really don't follow what FastAPI is doing here; it seems to be something to do with ModelField.__post_init__ in fastapi/_compat.py? Somebody who's actually an expert in that code would need to weigh in, I think. Maybe @tiangolo?

@github-actions github-actions bot removed the waiting label Sep 18, 2025
@Viicos
Copy link
Contributor

Viicos commented Sep 21, 2025

I discussed internally with the FastAPI team a while ago about this. The fix I proposed was to use the warning.catch_warnings context manager when creating type adapters in FastAPI. Pydantic emits a specific UnsupportedFieldAttributeWarning in this case, so it can safely be filtered while still having other potential warnings raised.

@cjwatson
Copy link
Contributor Author

cjwatson commented Sep 21, 2025

The fix I proposed is to use warning.catch_warnings context manager when creating type adapters in FastAPI.

OK, thanks for the advice. a33cc0a implements that.

@cjwatson cjwatson force-pushed the pydantic-2.12.0a1 branch 5 times, most recently from dc01ba4 to a33cc0a Compare September 22, 2025 09:58
Viicos
Viicos previously requested changes Sep 22, 2025
* run test suite on Python 3.14

* add 3.14 classifier

* allow Python pre-release for now

* exclude pydantic v1 on Python 3.14

* install pydantic 1.12.0 alpha1 when testing on python 3.14

* preinstall pydantic 1.12.0 alpha1 before anything else
@svlandeg svlandeg changed the base branch from master to feature/py314 September 24, 2025 15:58
@svlandeg
Copy link
Member

svlandeg commented Sep 24, 2025

I changed the base branch of this PR to feature/py314 where we've got a CI run for Python 3.14 and Pydantic 2.12 set up - this way we can actually see the failing tests πŸ˜‰

@svlandeg svlandeg marked this pull request as draft September 24, 2025 16:03
@cjwatson
Copy link
Contributor Author

@svlandeg That seems to have resulted in a pile of errors that have nothing to do with this branch. Or am I missing something?

cjwatson and others added 4 commits September 25, 2025 12:55
`tests/test_multi_body_errors.py::test_openapi_schema` failed with
pydantic 2.12.0a1 due to
pydantic/pydantic#11987.  Since we're testing
the exact contents of the JSON schema, the easiest fix seems to be to
add version-dependent handling for this.
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
@cjwatson cjwatson marked this pull request as ready for review September 25, 2025 11:57
@svlandeg
Copy link
Member

svlandeg commented Sep 25, 2025

@svlandeg That seems to have resulted in a pile of errors that have nothing to do with this branch. Or am I missing something?

I think these are exactly the errors that need resolving - the test suite now actually runs Pydantic 2.12.0a1 on Python 3.14 (rc)?

(they're really all mostly versions of the same error, from a quick glance)

@svlandeg svlandeg marked this pull request as draft September 25, 2025 17:58
@svlandeg
Copy link
Member

PS: @cjwatson: it's fine if you don't want to continue working on those other errors - just let us know - and we'll take over this branch because they need fixing anyway πŸ˜‰

@cjwatson
Copy link
Contributor Author

Those errors don't appear to happen in my local setup with pydantic 2.12.0a1 on Python 3.13 (which was what I was originally trying to fix with this PR). I'd appreciate it if somebody else could take over the rest of those fixes, yes.

@svlandeg
Copy link
Member

svlandeg commented Sep 26, 2025

Those errors don't appear to happen in my local setup with pydantic 2.12.0a1 on Python 3.13 (which was what I was originally trying to fix with this PR)

I think you're right - those additional errors are new in Python 3.14.

I'd appreciate it if somebody else could take over the rest of those fixes, yes.

Sure thing - I'm going to look into them now. I'll probably merge this PR into our feature branch feature/py314 and continue from there.

@svlandeg svlandeg marked this pull request as ready for review September 26, 2025 11:27
@svlandeg svlandeg changed the base branch from feature/py314 to master October 7, 2025 09:43
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just to recap and apologies for the earlier confusion!

This PR ensures compatibility with Pydantic 2.12.0, which will be released soon.

In particular, pydantic 2.12.0a1 causes 2 types of failures in our test suite:

  • test_openapi_schema fails due to pydantic/pydantic#11987 as explained by OP. The test is adjusted to work on both new and older versions of Pydantic.
  • warnings about using the alias attribute with value with in the Field() function, cf PR pydantic/pydantic#12028. This PR suppresses those warnings when creating type adapters in FastAPI, which seems like a reasonable solution to me.

I had intended to merge these fixes as part of a larger PR to get ready for Python 3.14, but have reverted that and suggest we merge & release this separately to ensure compatibility with the upcoming Pydantic 2.12.0.

Thanks again for your work on this, @cjwatson !

@svlandeg svlandeg removed their assignment Oct 7, 2025
Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you @cjwatson! πŸš€

And thanks a lot for all the help here @YuriiMotov, @Viicos, @svlandeg, @patrick91 πŸ™Œ

So cool to come and see all the conversations already done, the code simplified to the minimum, all the context easy to get quickly. Great job, thank you all! 🍰

I'll release this in the next hours/minutes in FastAPI 0.118.1 πŸŽ‰

@tiangolo tiangolo merged commit c970d8a into fastapi:master Oct 8, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants