matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.82k stars 2.13k forks source link

Add support for pydantic v2 via pydantic.v1 compat module #16332

Closed gotmax23 closed 1 year ago

gotmax23 commented 1 year ago

Add support for pydantic v2 via pydantic.v1 compat module

This adds support for pydantic v2 by using the pydantic.v1 compat module when pydantic v2 is installed and otherwise falling back to the pydantic module.

In order to satisfy mypy, we only import pydantic.v1 during type checking. This allows proper type checking without needing to clutter the code with type: ignores.

Once some time is passed, we can entirely drop compatibility for pydantic v1 and start using the actual pydantic v2 code to reap the additional performance benefits and other improvements of pydantic v2.

Relates: https://github.com/matrix-org/synapse/issues/15858

(I didn't use Fixes:, as we should track enabling full support for pydantic v2)

Pull Request Checklist

clokep commented 1 year ago

See also #16331.

gotmax23 commented 1 year ago

@clokep, do you mind approving the CI run?

clokep commented 1 year ago

It looks like one of the CI jobs is failing too: https://github.com/matrix-org/synapse/actions/runs/6230975624/job/16994483499#step:10:32

gotmax23 commented 1 year ago

It looks like one of the CI jobs is failing too: https://github.com/matrix-org/synapse/actions/runs/6230975624/job/16994483499#step:10:32

Those tests are running with packaging==16.1 from 2016 :fearful:. Can this be updated to at least version 20 from 2019 that has https://github.com/pypa/packaging/commit/f1b2b2222c8832a5a01f328afb46c9f898e67a1e?

gotmax23 commented 1 year ago

I pushed to address the merge conflicts and feedback. @clokep, can you please approve CI again :slightly_smiling_face:?

gotmax23 commented 1 year ago

It looks like one of the CI jobs is failing too: https://github.com/matrix-org/synapse/actions/runs/6230975624/job/16994483499#step:10:32

Those tests are running with packaging==16.1 from 2016 😨. Can this be updated to at least version 20 from 2019 that has pypa/packaging@f1b2b22?

I bumped the minimum packaging version.

6543 commented 1 year ago

Alpine packager: ACK

arkamar commented 1 year ago

Gentoo: ACK, tested with pydantic 1.10.12 and 2.3.0.

clokep commented 1 year ago

Just need CI to churn and this is good to go. 👍

gotmax23 commented 1 year ago

Thanks @clokep!