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

`exclude_unset` missing from `FHIRAbstractModel.dict` #89

Closed johnjameswhitman closed 2 years ago

johnjameswhitman commented 2 years ago

Description

We're currently on v6.1 and are testing an upgrade to v6.2 but our tests for a method that works with both fhir.resources and regular pydantic models fails with this message:

> value.dict(by_alias=True, exclude_unset=True)
TypeError: dict() got an unexpected keyword argument 'exclude_unset'

We rely on exclude_unset, which determines whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary (pydantic docs).

Then I noticed this from the 6.2.0b1 release notes:

Breaking

  • FHIRAbstractModel.json() and FHIRAbstractModel.dict() parameters signatures are more FHIR specific and additional parameters are removed (pydantic specific).

I see this is the commit that tweaks the signature of fhir.resources.fhirabstractmodel.FHIRAbstractModel.dict().

What I Did

It seems like the underlying BaseModel keeps track of a __fields_set__ property (pydantic docs) we might be able to use to shoehorn exclude_unset back into the new dict() implementation; however, I'm curious if it was removed for a specific reason?

nazrulworld commented 2 years ago

Sorry to hear about your problem. I think that breaking changes for not one particular reason. If you look at our overridden FHIRAbstractModel.dict is internally using two custom methods (_fhir_iter() & _fhir_get_value()) (have to) & way of calculating allowed fields are different than pydantic.

//We rely on exclude_unset, which determines whether fields which were not explicitly set when creating the model should// Can you please describe with an example of your use case? about explicitly set.

We will try to add that feature, but it could be really good if we found this issue before the stable release [during beta release].

nazrulworld commented 2 years ago

The new release v6.2.1 should prevent the breaking situation.

johnjameswhitman commented 2 years ago

Thank you @nazrulworld!