Skip to content

Commit 08a88e7

Browse files
authored
[Python] Fix missing support for floats (#2883)
## Changes Add support for floats to Python code. Previously, attempting to deserialize a float resulted in an error. There is only one example of a float. It's in `spot_bid_max_price` in `compute.AzureAttributes`, which was previously unsupported. Implement support for floats with all implications of precision loss. ## Why Fixes #2875 ## Tests Unit tests and manually
1 parent 25cc7d9 commit 08a88e7

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
* Fix handling of Unicode characters in Python support ([#2873](https://github.com/databricks/cli/pull/2873))
1919
* Add support for secret scopes in DABs ([#2744](https://github.com/databricks/cli/pull/2744))
2020
* Make `artifacts.*.type` optional in bundle JSON schema ([#2881](https://github.com/databricks/cli/pull/2881))
21+
* Fix support for `spot_bid_max_price` field in Python support ([#2883](https://github.com/databricks/cli/pull/2883))
2122

2223
### API Changes

experimental/python/databricks/bundles/core/_transform.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def _transform(cls: Type[_T], value: Any) -> _T:
195195
return str(value) # type:ignore
196196
elif cls is int:
197197
return int(value) # type:ignore
198+
elif cls is float:
199+
return float(value) # type:ignore
198200
elif cls is bool:
199201
if isinstance(value, bool):
200202
return value # type:ignore

experimental/python/databricks_tests/core/test_transform.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from dataclasses import dataclass
23
from enum import Enum
34
from typing import Optional
@@ -332,3 +333,15 @@ class A:
332333
out = _transform(A, {"field": {"color": "red"}})
333334

334335
assert out == A(field=MyDataclass(color=Color.RED))
336+
337+
338+
def test_transform_float():
339+
value = float(math.pi)
340+
341+
@dataclass
342+
class Fake:
343+
field: Optional[float] = None
344+
345+
out = _transform(Fake, {"field": value})
346+
347+
assert out == Fake(field=value)

0 commit comments

Comments
 (0)