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

Support Pydantic models without the need to serialize #43

Open
axelv opened this issue Dec 16, 2024 · 2 comments
Open

Support Pydantic models without the need to serialize #43

axelv opened this issue Dec 16, 2024 · 2 comments

Comments

@axelv
Copy link

axelv commented Dec 16, 2024

Is it technically possible to support Pydantic models like the ones generated by https://github.com/beda-software/fhir-py-types directly in fhirpath (without needing to serialize)?

What I had in mind was making my Pydantic models compatible with Mapping. Then, the library will be generalized from dict and list types to mapping and sequence types.

I am happy to help implement this one in a PR if it is viable 🙌

@ruscoder
Copy link
Member

Hi,

If I got you right, currently it's impossible to pass Pydantic model into fhirpathpy.evaluate without explicitly invoking model_dump() on the instance first.

I guess it's not easy to adjust the code to support pydantic models, because in that case all conditions like isinstance(data, dict) should be supplemented with isinstance(data, BaseModel) that will require pydantic as a dependency.

What concerns do you have with writing evaluate(model.model_dump(), "expression")?

@axelv
Copy link
Author

axelv commented Dec 16, 2024

Correct!

My concerns with evaluate(model.model_dump(), "expression") :

  • I'm using a custom BaseModel for my FHIR Pydantic models with custom methods (eg. __str__(), __liquid__()). When running model_dump() all those methods are lost.
  • To a lesser extent, performance

Some ideas I'm playing with

  1. First solution I have in mind is generalizing the type checks for dict
    If I find a way to implement __getitem__ on my Pydantic model. Replacing all isinstance(data, dict) with isinstance(data, Mapping) would be enough I think.

  2. Second solution, but this is more a workaround:
    Maybe there is a way to extract the model 'path' of the result items from evaluate. Base on the path I can try to parse result nodes back to Pydantic. 🤔

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

No branches or pull requests

2 participants