Skip to content

Commit ededd5b

Browse files
author
Александр Сергеевич Целуйко
committed
Test Meta length bounds at platform limits
1 parent 7e40645 commit ededd5b

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎tests/unit/test_constraints.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import math
33
import re
4+
import sys
45
from typing import Annotated
56

67
import pytest
@@ -168,21 +169,27 @@ def test_multiple_of_bounds(self, val):
168169
def test_nonnegative_integer_fields(self, field):
169170
Meta(**{field: 0})
170171
Meta(**{field: 10})
172+
Meta(**{field: sys.maxsize})
171173
with pytest.raises(TypeError, match=f"`{field}` must be an int, got float"):
172174
Meta(**{field: 1.5})
173175

174176
with pytest.raises(ValueError, match=f"`{field}` must be >= 0, got -1"):
175177
Meta(**{field: -1})
178+
with pytest.raises(ValueError, match=f"`{field}` must be >= 0, got -10"):
179+
Meta(**{field: -10})
180+
with pytest.raises(
181+
ValueError, match=f"`{field}` must be >= 0, got {-sys.maxsize - 1}"
182+
):
183+
Meta(**{field: -sys.maxsize - 1})
176184

177185
@pytest.mark.parametrize("field", ["min_length", "max_length"])
178-
@pytest.mark.parametrize("val", [2**63, 10**400, -(10**400)])
186+
@pytest.mark.parametrize(
187+
"val", [sys.maxsize + 1, -sys.maxsize - 2, 10**400, -(10**400)]
188+
)
179189
def test_nonnegative_integer_fields_out_of_range(self, field, val):
180-
# Doesn't fit in a Py_ssize_t. The overflow must not be reported as
181-
# the value being negative, which it isn't.
190+
# Values outside Py_ssize_t need a range error in either direction.
182191
with pytest.raises(ValueError, match=f"`{field}` is out of range"):
183192
Meta(**{field: val})
184-
with pytest.raises(ValueError, match=f"{field}` must be >= 0, got -10"):
185-
Meta(**{field: -10})
186193

187194
@pytest.mark.parametrize("field", ["pattern", "title", "description"])
188195
def test_string_fields(self, field):

0 commit comments

Comments
 (0)