Skip to content

Commit c9b1cfb

Browse files
ruscoderclaude
andcommitted
Fix evaluate() mutating input resource floats to Decimal
visit() in apply_parsed_path wrote get_data() results back into the original dict in place, converting float values to Decimal. Replace the in-place mutation with a new dict comprehension. Closes #75 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 14430fc commit c9b1cfb

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

fhirpathpy/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def visit(node):
8484
return res
8585

8686
if isinstance(data, dict) and not isinstance(data, FP_Type):
87-
for key, value in data.items():
88-
data[key] = visit(value)
87+
return {key: visit(value) for key, value in data.items()}
8988

9089
return data
9190

tests/test_additional.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ def path_functions_test(resource, path):
2323
assert str(e.value) == "to_date called for a collection of length 2"
2424

2525

26+
def evaluate_does_not_mutate_float_values_test():
27+
"""evaluate() must not convert floats to Decimal in the caller's dict."""
28+
qr = {
29+
"resourceType": "QuestionnaireResponse",
30+
"status": "completed",
31+
"item": [
32+
{
33+
"linkId": "temperature",
34+
"answer": [
35+
{
36+
"valueQuantity": {
37+
"value": 37.2,
38+
"unit": "°C",
39+
"system": "http://unitsofmeasure.org",
40+
"code": "Cel",
41+
}
42+
}
43+
],
44+
}
45+
],
46+
}
47+
evaluate(qr, "QuestionnaireResponse.repeat(item).answer")
48+
assert isinstance(qr["item"][0]["answer"][0]["valueQuantity"]["value"], float)
49+
50+
2651
def copy_deepcopy_test():
2752
copy_1 = copy.copy(evaluate({}, "@2018"))
2853
copy_2 = evaluate({}, "@2018").copy()

0 commit comments

Comments
 (0)