From: Colin Watson <cjwatson@debian.org>
Date: Tue, 2 Sep 2025 11:04:36 +0100
Subject: Fix tests with pydantic >= 2.12.0a1

Last-Update: 2025-09-02
---
 pyproject.toml            |  1 +
 tests/test_coordinate.py  | 18 ++++++++++++------
 tests/test_json_schema.py | 18 ++++++++++++------
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index bdf485a..da405b0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -69,6 +69,7 @@ dev = [
     "coverage[toml]>=7.6.1",
     "pytest-pretty>=1.2.0",
     "dirty-equals>=0.7.1",
+    "packaging",
     "pytest>=8.3.2",
 ]
 lint = [
diff --git a/tests/test_coordinate.py b/tests/test_coordinate.py
index eb9e1c0..ea4df92 100644
--- a/tests/test_coordinate.py
+++ b/tests/test_coordinate.py
@@ -1,8 +1,10 @@
+import importlib.metadata
 from decimal import Decimal
 from re import Pattern
 from typing import Any, Optional, Union
 
 import pytest
+from packaging.version import Version
 from pydantic import BaseModel, ValidationError
 from pydantic_core._pydantic_core import ArgsKwargs
 
@@ -215,14 +217,18 @@ def test_json_schema():
     class Model(BaseModel):
         value: Coordinate
 
+    decimal_type = {'type': 'string'}
+    if Version(importlib.metadata.version('pydantic')) >= Version('2.12.0a1'):
+        decimal_type['pattern'] = '^(?!^[-+.]*$)[+-]?0*\\d*\\.?\\d*$'
+
     assert Model.model_json_schema(mode='validation')['$defs']['Coordinate'] == {
         'properties': {
             'latitude': {
-                'anyOf': [{'maximum': 90.0, 'minimum': -90.0, 'type': 'number'}, {'type': 'string'}],
+                'anyOf': [{'maximum': 90.0, 'minimum': -90.0, 'type': 'number'}, decimal_type],
                 'title': 'Latitude',
             },
             'longitude': {
-                'anyOf': [{'maximum': 180.0, 'minimum': -180.0, 'type': 'number'}, {'type': 'string'}],
+                'anyOf': [{'maximum': 180.0, 'minimum': -180.0, 'type': 'number'}, decimal_type],
                 'title': 'Longitude',
             },
         },
@@ -237,8 +243,8 @@ def test_json_schema():
                 'maxItems': 2,
                 'minItems': 2,
                 'prefixItems': [
-                    {'anyOf': [{'type': 'number'}, {'type': 'string'}]},
-                    {'anyOf': [{'type': 'number'}, {'type': 'string'}]},
+                    {'anyOf': [{'type': 'number'}, decimal_type]},
+                    {'anyOf': [{'type': 'number'}, decimal_type]},
                 ],
                 'type': 'array',
             },
@@ -251,11 +257,11 @@ def test_json_schema():
             'Coordinate': {
                 'properties': {
                     'latitude': {
-                        'anyOf': [{'maximum': 90.0, 'minimum': -90.0, 'type': 'number'}, {'type': 'string'}],
+                        'anyOf': [{'maximum': 90.0, 'minimum': -90.0, 'type': 'number'}, decimal_type],
                         'title': 'Latitude',
                     },
                     'longitude': {
-                        'anyOf': [{'maximum': 180.0, 'minimum': -180.0, 'type': 'number'}, {'type': 'string'}],
+                        'anyOf': [{'maximum': 180.0, 'minimum': -180.0, 'type': 'number'}, decimal_type],
                         'title': 'Longitude',
                     },
                 },
diff --git a/tests/test_json_schema.py b/tests/test_json_schema.py
index 815ac53..d5d60e1 100644
--- a/tests/test_json_schema.py
+++ b/tests/test_json_schema.py
@@ -1,7 +1,9 @@
+import importlib.metadata
 from typing import Any, Dict, Union
 
 import pycountry
 import pytest
+from packaging.version import Version
 from pydantic import BaseModel
 from typing_extensions import Annotated
 
@@ -55,6 +57,10 @@ USNumberE164 = Annotated[
     ),
 ]
 
+decimal_type = {'type': 'string'}
+if Version(importlib.metadata.version('pydantic')) >= Version('2.12.0a1'):
+    decimal_type['pattern'] = '^(?!^[-+.]*$)[+-]?0*\\d*\\.?\\d*$'
+
 
 @pytest.mark.parametrize(
     'cls,expected',
@@ -141,7 +147,7 @@ USNumberE164 = Annotated[
                     'x': {
                         'anyOf': [
                             {'maximum': 90.0, 'minimum': -90.0, 'type': 'number'},
-                            {'type': 'string'},
+                            decimal_type,
                         ],
                         'title': 'X',
                     }
@@ -158,7 +164,7 @@ USNumberE164 = Annotated[
                     'x': {
                         'anyOf': [
                             {'maximum': 180.0, 'minimum': -180.0, 'type': 'number'},
-                            {'type': 'string'},
+                            decimal_type,
                         ],
                         'title': 'X',
                     }
@@ -177,14 +183,14 @@ USNumberE164 = Annotated[
                             'latitude': {
                                 'anyOf': [
                                     {'maximum': 90.0, 'minimum': -90.0, 'type': 'number'},
-                                    {'type': 'string'},
+                                    decimal_type,
                                 ],
                                 'title': 'Latitude',
                             },
                             'longitude': {
                                 'anyOf': [
                                     {'maximum': 180.0, 'minimum': -180.0, 'type': 'number'},
-                                    {'type': 'string'},
+                                    decimal_type,
                                 ],
                                 'title': 'Longitude',
                             },
@@ -202,8 +208,8 @@ USNumberE164 = Annotated[
                                 'maxItems': 2,
                                 'minItems': 2,
                                 'prefixItems': [
-                                    {'anyOf': [{'type': 'number'}, {'type': 'string'}]},
-                                    {'anyOf': [{'type': 'number'}, {'type': 'string'}]},
+                                    {'anyOf': [{'type': 'number'}, decimal_type]},
+                                    {'anyOf': [{'type': 'number'}, decimal_type]},
                                 ],
                                 'type': 'array',
                             },
