1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
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 ecc1de0..adc3c69 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -71,6 +71,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 d3a06cc..a741add 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 27e72a2..3dd43f3 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
@@ -56,6 +58,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',
@@ -156,7 +162,7 @@ USNumberE164 = Annotated[
'x': {
'anyOf': [
{'maximum': 90.0, 'minimum': -90.0, 'type': 'number'},
- {'type': 'string'},
+ decimal_type,
],
'title': 'X',
}
@@ -173,7 +179,7 @@ USNumberE164 = Annotated[
'x': {
'anyOf': [
{'maximum': 180.0, 'minimum': -180.0, 'type': 'number'},
- {'type': 'string'},
+ decimal_type,
],
'title': 'X',
}
@@ -192,14 +198,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',
},
@@ -217,8 +223,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',
},
|