nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
372 stars 104 forks source link

When are Falsy values evaluated as None? #74

Open Healthedata1 opened 3 years ago

Healthedata1 commented 3 years ago

Description

I am creating a FHIR object using the models and a spreadsheet as a source When there is repeating element and their is no data in the source, my code creates an empty list. This empty list is preserved unlike when the element is None. I think when an element is evaluated to these Falsy types listed below I think it should behave the same ... namely the element value should be None. Note that in FHIR "when present, elements cannot be empty - they SHALL have a value attribute, child elements, or extensions".

  1. None
  2. [] - an empty list
  3. {} - an empty dict
  4. () - an empty tuple
  5. '' - an empty str
  6. b'' - an empty bytes
  7. set() - an empty set.

What I Did

cs = construct_fhir_element('CapabilityStatement',dict(
id = meta.id,
url = f'{canon}CapabilityStatement/{meta.id}',
version = meta.version,
# more elements
instantiates = [i for i in meta.instantiates.split(',') if i], 
imports = [i for i in meta.imports.split(',') if i],   #<<<< this evaluates to '[]'  instead of None 
))

print(cs.json(indent=2))

...
  "kind": "requirements",
  "instantiates": [
    "foo"
  ],
  "imports": [],  <<<<<<<<<<<<<  the empty list as value!
  "fhirVersion": "4.0.1",
....

As a convenience, I would like to see these falsy values be treated like None instead of persisting.

nazrulworld commented 3 years ago

I fully agree with you, that an empty list should be ignored just a null value.

Healthedata1 commented 3 years ago

Although I can see the argument for persisting an empty list when building an instance. You might want to append items in a subsequent step. So it would not always be desirable behavior. Maybe the class could preserve the list, but the serialization methods would "scrub" them or not based on a parameter?

nazrulworld commented 3 years ago

All are default behaviors from the Pydnatic but It is possible to change that empty list, dict to None during serialization. I will work on it.

Healthedata1 commented 3 years ago

Thanks!