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

Example 5 and Encounter validation is failing #39

Closed ytitov closed 3 years ago

ytitov commented 3 years ago

Greetings, I am investigating this library to use in a ETL step for a data integration and looking to create FHIR data structures. I've never used pydantic before so please forgive my ignorance :) Could I get some help with why this is failing?

Description

I am trying to create an encounter:

from fhir.resources.encounter import Encounter
e: Encounter = Encounter(**{"class": 'inpatient', "status": "planned"})

What I Did

Results in a confusing error. Especially since it is talking about "jsondecode" error.

Traceback (most recent call last):
  File "test.py", line 62, in <module>
    fetchsome(cur, handleRow, some=100)
  File "test.py", line 39, in fetchsome
    rowFunc(row)
  File "test.py", line 52, in handleRow
    e: Encounter = Encounter(**{"class": 'inpatient', "status": "planned"})
  File "...\lib\site-packages\fhir.resources-6.0.0b7.dev0-py3.7.egg\fhir\resources\fhirabstractmodel.py", line 126, in __init__
    BaseModel.__init__(__pydantic_self__, **data)
  File "pydantic\main.py", line 362, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Encounter
class -> __root__
  expected value at line 1 column 1: line 1 column 1 (char 0) (type=value_error.jsondecode; msg=expected value at line 1 column 1; doc=; pos=0; lineno=1; colno=1)

In addition to above Example 5 doesn't seem to work either:

from fhir.resources.organization import Organization
org = Organization.construct()

results in

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    org = Organization.construct()
  File "...\lib\site-packages\fhir.resources-6.0.0b7.dev0-py3.7.egg\fhir\resources\fhirabstractmodel.py", line 386, in construct
    m, "__dict__", {**deepcopy(cls.__field_defaults__), **values}
AttributeError: type object 'Organization' has no attribute '__field_defaults__'
nazrulworld commented 3 years ago

@ytitov first of all thanks a lot for reporting this issue.

  1. Encounter(**{"class": 'inpatient', "status": "planned"}) this obisouly validation error as class should be Coding type dict instead of string, for example Encounter(**{"class": {"code": 'inpatient'}, "status": "planned"}). But I am fully agree with you about confusing error message, I will work on it.

  2. I cannot reproduce this error, in my local (Mac + Pycharm environment), would be nice if can share a gist file.

ytitov commented 3 years ago

@nazrulworld First, thanks for your responses.

  1. You're right, my object was invalid, but I really wish the error message made sense. Unfortunately I am not a python expert, just a user :)

  2. To replicate:

    • create a new environment (I used python 3.7.9 because that is the maximum stated supported version)
    • enter the environment, git clone master, and I tried two methods of installation (individually):
    • pip install -e .[all]
    • ./setup.py install
    • run the code as in example 5
      from fhir.resources.organization import Organization
      org = Organization.construct()

      The resulting error:

      $ python demo_fail.py
      Traceback (most recent call last):
      File "demo_fail.py", line 2, in <module>
      org = Organization.construct()
      File "C:\Users\ytitov\workspace\fhir\kn-udm-to-fhir\fhir.resources\fhir\resources\fhirabstractmodel.py", line 386, in construct
      m, "__dict__", {**deepcopy(cls.__field_defaults__), **values}
      AttributeError: type object 'Organization' has no attribute '__field_defaults__'
      (venv_py37)

EDIT: Just for good measure I did the same thing on my other PC which is running Arch Linux with python 3.8.3. Used code from master and installed that, still same error. If you still can't reproduce I can see if I can create a Dockerfile for you.

nazrulworld commented 3 years ago

@ytitov I can confirm that problem to be reproducible in my PC and I know why this is happing https://pypi.org/project/pydantic/1.7/ so new pydantic has some breaking changes. use earlier version than 1.7 would solve this problem. But I am looking forward to support pydantic 1.7

ytitov commented 3 years ago

@nazrulworld looks like downgrading to pydantic 1.6.1 fixes my issue