Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import math
import uuid

from end_to_end_tests.functional_tests.helpers import (
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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"),
Expand Down
12 changes: 10 additions & 2 deletions openapi_python_client/parser/properties/float.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import math
from typing import Any, ClassVar

from attr import define, field
Expand Down Expand Up @@ -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))
3 changes: 3 additions & 0 deletions openapi_python_client/parser/properties/int.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import math
from typing import Any, ClassVar

from attr import define, field
Expand Down Expand Up @@ -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
Expand Down