|
| 1 | +import json |
| 2 | +import sys |
1 | 3 | from decimal import Decimal |
2 | 4 | from fractions import Fraction |
3 | 5 | from typing import Annotated |
@@ -150,6 +152,64 @@ def test_fraction_validation_error(input: object, error_type: str): |
150 | 152 | ta.validate_python(input) |
151 | 153 |
|
152 | 154 |
|
| 155 | +@pytest.mark.parametrize( |
| 156 | + ['input_value', 'expected'], |
| 157 | + [ |
| 158 | + ('1e4300', Fraction(10**4300)), |
| 159 | + ('-1E+4300', Fraction(-(10**4300))), |
| 160 | + ('1e-4300', Fraction(1, 10**4300)), |
| 161 | + pytest.param( |
| 162 | + '1.5e4_300 ', |
| 163 | + Fraction(15 * 10**4299), |
| 164 | + marks=pytest.mark.skipif(sys.version_info < (3, 11), reason='Fraction supports underscores since 3.11'), |
| 165 | + ), |
| 166 | + ('1e00000000004300', Fraction(10**4300)), |
| 167 | + ('1e٤٣٠٠', Fraction(10**4300)), |
| 168 | + ], |
| 169 | +) |
| 170 | +@pytest.mark.parametrize('mode', ['python', 'json', 'strings']) |
| 171 | +def test_fraction_exponent_within_limit(input_value: str, expected: Fraction, mode: str) -> None: |
| 172 | + ta = TypeAdapter(Fraction) |
| 173 | + |
| 174 | + if mode == 'python': |
| 175 | + assert ta.validate_python(input_value) == expected |
| 176 | + elif mode == 'json': |
| 177 | + assert ta.validate_json(json.dumps(input_value)) == expected |
| 178 | + assert ta.validate_json(json.dumps(input_value), strict=True) == expected |
| 179 | + else: |
| 180 | + assert ta.validate_strings(input_value) == expected |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.parametrize( |
| 184 | + 'input_value', |
| 185 | + [ |
| 186 | + '1e4301', |
| 187 | + '1e-4301', |
| 188 | + '1E+4301', |
| 189 | + '1e4_301', |
| 190 | + '1.5e4301', |
| 191 | + '1e32000000', |
| 192 | + '1e-32000000', |
| 193 | + '1e99999999999999999999999', |
| 194 | + '1e32000000 ', |
| 195 | + '1e32000000\x1c', |
| 196 | + '1e٣٢٠٠٠٠٠٠', # 32000000 in Arabic-Indic digits |
| 197 | + '1e32000000', # 32000000 in fullwidth digits |
| 198 | + ], |
| 199 | +) |
| 200 | +@pytest.mark.parametrize('mode', ['python', 'json', 'strings']) |
| 201 | +def test_fraction_exponent_too_large(input_value: str, mode: str) -> None: |
| 202 | + ta = TypeAdapter(Fraction) |
| 203 | + |
| 204 | + with pytest.raises(ValidationError, check=lambda e: e.errors()[0]['type'] == 'fraction_parsing'): |
| 205 | + if mode == 'python': |
| 206 | + ta.validate_python(input_value) |
| 207 | + elif mode == 'json': |
| 208 | + ta.validate_json(json.dumps(input_value), strict=True) |
| 209 | + else: |
| 210 | + ta.validate_strings(input_value) |
| 211 | + |
| 212 | + |
153 | 213 | @pytest.mark.parametrize( |
154 | 214 | 'input_value,expected', |
155 | 215 | [ |
|
0 commit comments