From fc34b884ec202d22d89a59ec39b07dff7da3204d Mon Sep 17 00:00:00 2001 From: takinanton <191816639+takinanton@users.noreply.github.com> Date: Sat, 5 Dec 2026 03:30:52 +0000 Subject: [PATCH] fix: handle inf and nan number defaults --- .../generated_code_execution/test_defaults.py | 21 +++++++++++++++++++ .../test_invalid_defaults.py | 4 ++++ .../parser/properties/float.py | 12 +++++++++-- .../parser/properties/int.py | 3 +++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py b/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py index ae32cb31c..e06dfe274 100644 --- a/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py +++ b/end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py @@ -1,4 +1,5 @@ import datetime +import math import uuid from end_to_end_tests.functional_tests.helpers import ( @@ -113,3 +114,23 @@ def test_enum_default(self, MyEnum, MyModel): class TestLiteralEnumDefaults: def test_default_value(self, MyModel): assert MyModel().enum_prop == "A" + + +@with_generated_client_fixture( + """ +components: + schemas: + MyModel: + properties: + upper: {"type": "number", "default": .inf} + lower: {"type": "number", "default": "-Infinity"} + missing: {"type": "number", "default": .nan} +""" +) +@with_generated_code_imports(".models.MyModel") +class TestNonFiniteFloatDefaults: + def test_non_finite_defaults(self, MyModel): + instance = MyModel() + assert instance.upper == math.inf + assert instance.lower == -math.inf + assert math.isnan(instance.missing) diff --git a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py index 7499f655c..849a24734 100644 --- a/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py +++ b/end_to_end_tests/functional_tests/generator_failure_cases/test_invalid_defaults.py @@ -16,6 +16,9 @@ WithBadIntAsOther: properties: badInt: {"type": "integer", "default": true} + WithInfiniteInt: + properties: + badInt: {"type": "integer", "default": .inf} WithBadFloatAsString: properties: badInt: {"type": "number", "default": "not a number"} @@ -71,6 +74,7 @@ class TestInvalidDefaultValues: ("WithBadBoolean", "Invalid boolean value"), ("WithBadIntAsString", "Invalid int value"), ("WithBadIntAsOther", "Invalid int value"), + ("WithInfiniteInt", "Invalid int value"), ("WithBadFloatAsString", "Invalid float value"), ("WithBadFloatAsOther", "Cannot convert True to a float"), ("WithBadDateAsString", "Invalid date"), diff --git a/openapi_python_client/parser/properties/float.py b/openapi_python_client/parser/properties/float.py index ed9a49683..dfee1f82c 100644 --- a/openapi_python_client/parser/properties/float.py +++ b/openapi_python_client/parser/properties/float.py @@ -1,5 +1,6 @@ from __future__ import annotations +import math from typing import Any, ClassVar from attr import define, field @@ -62,11 +63,18 @@ def convert_value(cls, value: Any) -> Value | PropertyError | None: if isinstance(value, str): try: parsed = float(value) - return Value(python_code=PythonCode(str(parsed)), raw_value=value) except ValueError: return PropertyError(f"Invalid float value: {value}") + return Value(python_code=_float_code(parsed), raw_value=value) if isinstance(value, float): - return Value(python_code=PythonCode(str(value)), raw_value=value) + return Value(python_code=_float_code(value), raw_value=value) if isinstance(value, int) and not isinstance(value, bool): return Value(python_code=PythonCode(str(float(value))), raw_value=value) return PropertyError(f"Cannot convert {value} to a float") + + +def _float_code(value: float) -> PythonCode: + # str() gives "inf" or "nan" for these, which are not valid Python names + if not math.isfinite(value): + return PythonCode(f'float("{value}")') + return PythonCode(str(value)) diff --git a/openapi_python_client/parser/properties/int.py b/openapi_python_client/parser/properties/int.py index 6ee23fcf9..f2e29766a 100644 --- a/openapi_python_client/parser/properties/int.py +++ b/openapi_python_client/parser/properties/int.py @@ -1,5 +1,6 @@ from __future__ import annotations +import math from typing import Any, ClassVar from attr import define, field @@ -66,6 +67,8 @@ def convert_value(cls, value: Any) -> Value | PropertyError | None: except ValueError: return PropertyError(f"Invalid int value: {converted}") if isinstance(converted, float): + if not math.isfinite(converted): + return PropertyError(f"Invalid int value: {value}") as_int = int(converted) if converted == as_int: converted = as_int