Skip to main content

Types for FHIR Resources

Project description

Python Typings For FHIR

The FHIR Types package exports types declared using the typing library. This provides type checking support when creating dictionaries of FHIR objects in Python.

Installation

Installing from Pypi (recommended)

We recommend you first use a virtual environment and activate it in your shell.

pip install fhir-types

Installing from source

  1. Clone this repository
  2. Change to the package root directory
  3. Run the pipenv install --dev --deploy command to create and install in a virtual environment
% pipenv install --dev --deploy
Creating a virtualenv for this project...
Pipfile: /fhir-types/Pipfile
Using /usr/local/bin/python3 (3.9.7) to create virtualenv...
. . .

If you want to install globally run pip install ., outside of a virtual environment.

Verify the installation

Verify the installation with pipenv run almake test.

% pipenv run almake test
python -m black --check .
All done!  🍰 702 files would be left unchanged.
python -m isort --check --df .
. . .

Or, if you have make installed:

% pipenv run make test
<same output as shown above>

Usage

This project was developed using mypy for type checking.

Example

Consider these two FHIR JSON dictionaries.

# myfile.py

from fhir_types import FHIR_Patient

# This is a valid patient dictionary
patient: FHIR_Patient = {
    "resourceType": "Patient",
    "gender": "female",
    "name": [
        {
            "use": "official",
            "family": "Johnson",
            "given": ["Jennifer"],
            "prefix": ["Mrs."],
        }
    ]
}

# This is an invalid patient dictionary
invalid_patient: FHIR_Patient = {
    "resourceType": "PPPPatient", # error 1
    "gender": "female123",  # error 2
    "name": [
        {
            "123use": "official", # error 3
            "family": "Johnson",
            "given": ["Jennifer"],
            "prefix": ["Mrs."],
        }
    ]
}

After installing mypy with pip, running mypy myfile.py will display the errors in the second (invalid) structure.

It is good practice to set up your IDE (such as VS Code) to use mypy for linting as you work.

Issues

The Types located in the fhir_types directory are generated from the official fhir.schema.json provided by FHIR.

This data stucture is quite large with and contains cyclical relationships. Unfortunately, mypy does not handle cyclical relationships and has an open issue here. We added a workaround that reduces the type checking capabilities for certain properties in some classes.

All properties that point to any of the following classes are given an Any type because these lead to circular references. These were found by repeatedly running the mypy command.

CIRCULAR_REFERENCES = {
    "Extension",
    "Identifier",
    "ResourceList",
    "QuestionnaireResponse_Answer",
    "GraphDefinition_Link",
    "ExampleScenario_Process",
    "ExampleScenario_Alternative",
}

The other case is when a property points back to a parent class.

from fhir_types import FHIR_Questionnaire_Item

FHIR_Questionnaire_Item = TypedDict("FHIR_Questionnaire_Item", {
    "id": "123",
    item: List[Any] # This used to be FHIR_Questionnaire_Item but was changed to Any
})

Here, full type checking can be accomplished by creating two separate dictionaries and then combining them. For example:

from fhir_types import FHIR_Extension, FHIR_Patient

extension: FHIR_Extension = {
    "valueAddress": {
        "city": "Springfield",
        "state": "Massachusetts",
        "country": "US"
    }
}

patient: FHIR_Patient = {
    "gender": "male",
    "extension": [extension]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fhir-types-0.2.4.tar.gz (657.1 kB view details)

Uploaded Source

Built Distribution

fhir_types-0.2.4-py3-none-any.whl (988.5 kB view details)

Uploaded Python 3

File details

Details for the file fhir-types-0.2.4.tar.gz.

File metadata

  • Download URL: fhir-types-0.2.4.tar.gz
  • Upload date:
  • Size: 657.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for fhir-types-0.2.4.tar.gz
Algorithm Hash digest
SHA256 afb3be60266e63e8cb83665e3992ef5617ef5dd59893452ca555af1c4e8b5dc3
MD5 d62f30a36dd560763dc092ef9d48df86
BLAKE2b-256 6466487ea893c14161a224f8098da081402ba93b394556edf19124306ba527d0

See more details on using hashes here.

File details

Details for the file fhir_types-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: fhir_types-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 988.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for fhir_types-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e853d91a22b3928991c07255d52072be1bf30c543525dd7f4b174d46b36df47c
MD5 ab9e8d32ff0874cb6c104dce891e9f20
BLAKE2b-256 38e4603309ad6e0b8447cc79bccad41f4cbc5a98f4875528c1f7b506f893276b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page