Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When are Falsy values evaluated as None? #74

Open
Healthedata1 opened this issue May 26, 2021 · 4 comments
Open

When are Falsy values evaluated as None? #74

Healthedata1 opened this issue May 26, 2021 · 4 comments
Labels
question Further information is requested

Comments

@Healthedata1
Copy link

Healthedata1 commented May 26, 2021

  • fhir.resources version: R4
  • Python version: 3.7.3
  • Operating System: Mac OS

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 nazrulworld added the question Further information is requested label May 28, 2021
@nazrulworld
Copy link
Owner

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

@Healthedata1
Copy link
Author

Healthedata1 commented May 29, 2021

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
Copy link
Owner

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
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants