Skip to content

Commit e887d99

Browse files
committed
feat: clarify FhirData setter behavior with explicit replace parameter
1 parent 2f114b2 commit e887d99

File tree

2 files changed

+86
-24
lines changed

2 files changed

+86
-24
lines changed

healthchain/io/containers/document.py

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,36 +270,102 @@ def problem_list(self) -> List[Condition]:
270270
return self.get_resources("Condition")
271271

272272
@problem_list.setter
273-
def problem_list(self, conditions: List[Condition]) -> None:
274-
# TODO: should make this behaviour more explicit whether it's adding or replacing
275-
"""Set problem list in the bundle."""
276-
self.add_resources(conditions, "Condition")
273+
def problem_list(self, value: Union[List[Condition], Dict[str, Any]]) -> None:
274+
"""
275+
Set problem list in the bundle.
276+
277+
By default, this adds the provided conditions to any existing conditions in the bundle.
278+
To replace existing conditions instead, pass a dictionary with 'resources' and 'replace' keys.
279+
280+
Args:
281+
value: Either a list of Condition resources (adds by default) or a dict with:
282+
- 'resources': List of Condition resources
283+
- 'replace': bool, whether to replace existing resources (default: False)
284+
285+
Examples:
286+
>>> # Add to existing conditions (default behavior)
287+
>>> fhir.problem_list = [condition1, condition2]
288+
>>> # Replace existing conditions
289+
>>> fhir.problem_list = {'resources': [condition1], 'replace': True}
290+
"""
291+
if isinstance(value, dict):
292+
resources = value.get("resources", [])
293+
replace = value.get("replace", False)
294+
else:
295+
resources = value
296+
replace = False
297+
298+
self.add_resources(resources, "Condition", replace=replace)
277299

278300
@property
279301
def medication_list(self) -> List[MedicationStatement]:
280302
"""Get medication list from the bundle."""
281303
return self.get_resources("MedicationStatement")
282304

283305
@medication_list.setter
284-
def medication_list(self, medications: List[MedicationStatement]) -> None:
285-
"""Set medication list in the bundle.
286-
Medication statements are stored as MedicationStatement resources in the bundle.
287-
See: https://www.hl7.org/fhir/medicationstatement.html
306+
def medication_list(
307+
self, value: Union[List[MedicationStatement], Dict[str, Any]]
308+
) -> None:
288309
"""
289-
self.add_resources(medications, "MedicationStatement")
310+
Set medication list in the bundle.
311+
312+
By default, this adds the provided medications to any existing medications in the bundle.
313+
To replace existing medications instead, pass a dictionary with 'resources' and 'replace' keys.
314+
315+
Args:
316+
value: Either a list of MedicationStatement resources (adds by default) or a dict with:
317+
- 'resources': List of MedicationStatement resources
318+
- 'replace': bool, whether to replace existing resources (default: False)
319+
320+
Examples:
321+
>>> # Add to existing medications (default behavior)
322+
>>> fhir.medication_list = [medication1, medication2]
323+
>>> # Replace existing medications
324+
>>> fhir.medication_list = {'resources': [medication1], 'replace': True}
325+
"""
326+
if isinstance(value, dict):
327+
resources = value.get("resources", [])
328+
replace = value.get("replace", False)
329+
else:
330+
resources = value
331+
replace = False
332+
333+
self.add_resources(resources, "MedicationStatement", replace=replace)
290334

291335
@property
292336
def allergy_list(self) -> List[AllergyIntolerance]:
293337
"""Get allergy list from the bundle."""
294338
return self.get_resources("AllergyIntolerance")
295339

296340
@allergy_list.setter
297-
def allergy_list(self, allergies: List[AllergyIntolerance]) -> None:
298-
"""Set allergy list in the bundle.
299-
Allergy intolerances are stored as AllergyIntolerance resources in the bundle.
300-
See: https://www.hl7.org/fhir/allergyintolerance.html
341+
def allergy_list(
342+
self, value: Union[List[AllergyIntolerance], Dict[str, Any]]
343+
) -> None:
301344
"""
302-
self.add_resources(allergies, "AllergyIntolerance")
345+
Set allergy list in the bundle.
346+
347+
By default, this adds the provided allergies to any existing allergies in the bundle.
348+
To replace existing allergies instead, pass a dictionary with 'resources' and 'replace' keys.
349+
350+
Args:
351+
value: Either a list of AllergyIntolerance resources (adds by default) or a dict with:
352+
- 'resources': List of AllergyIntolerance resources
353+
- 'replace': bool, whether to replace existing resources (default: False)
354+
355+
Examples:
356+
>>> # Add to existing allergies (default behavior)
357+
>>> fhir.allergy_list = [allergy1, allergy2]
358+
>>> # Replace existing allergies
359+
>>> fhir.allergy_list = {'resources': [allergy1], 'replace': True}
360+
"""
361+
if isinstance(value, dict):
362+
resources = value.get("resources", [])
363+
replace = value.get("replace", False)
364+
else:
365+
resources = value
366+
replace = False
367+
368+
self.add_resources(resources, "AllergyIntolerance", replace=replace)
303369

304370
def get_prefetch_resources(self, key: str) -> List[Any]:
305371
"""Get resources of a specific type from the prefetch bundle."""

pyproject.toml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
[tool.poetry]
1+
[project]
22
name = "healthchain"
33
version = "0.0.0"
44
description = "Open source framework for productionizing healthcare AI"
5-
authors = ["Jennifer Jiang-Kells <jenniferjiangkells@gmail.com>", "Adam Kells <adamjkells93@gmail.com>"]
6-
license = "Apache-2.0"
5+
authors = [
6+
{name = "Jennifer Jiang-Kells", email = "jenniferjiangkells@gmail.com"},
7+
{name = "Adam Kells", email = "adamjkells93@gmail.com"}
8+
]
9+
license = {text = "Apache-2.0"}
710
readme = "README.md"
8-
documentation = "https://dotimplement.github.io/HealthChain/"
911
keywords = ["nlp", "ai", "llm", "healthcare", "ehr", "mlops", "fhir"]
1012
classifiers = [
1113
"Development Status :: 3 - Alpha",
@@ -16,12 +18,6 @@ classifiers = [
1618
"Programming Language :: Python :: 3",
1719
"Topic :: Scientific/Engineering :: Artificial Intelligence",
1820
]
19-
include = [
20-
"healthchain/templates/*",
21-
"healthchain/configs/**/*.yaml",
22-
"healthchain/configs/**/*.liquid"
23-
]
24-
2521
[project.urls]
2622
"Homepage" = "https://dotimplement.github.io/HealthChain/"
2723
"Repository" = "https://github.com/dotimplement/HealthChain"

0 commit comments

Comments
 (0)