Skip to content

[Issue #185] Support mapping-based transformations #193

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

widal001
Copy link
Collaborator

@widal001 widal001 commented May 19, 2025

Summary

Adds support for mapping based transformations to the Python SDK.

Changes proposed

What was added, updated, or removed in this PR.

  • Adds transform_from_mapping() function that transforms arbitrary JSON from one format to another using a mapping format that serves as a kind of lightweight DSL.
  • Adds a validate_with_mapping() to the CommonGrantsBaseModel to deserialize a dictionary after transforming it with a mapping.
  • Adds a dump_with_mapping() to CommonGrantsBaseModel to serialize and transform a model to a python dictionary.
  • Removes the extra push trigger from the ci-python-sdk.yaml to avoid duplicate runs of the CI checks on each PR.

Context for reviewers

Testing instructions, background context, more in-depth details of the implementation, and anything else you'd like to call out or ask reviewers. Explain how the changes were verified.

  1. Checkout the PR
  2. Change directory into python-sdk: cd lib/python-sdk/

Additional information

Screenshots, GIF demos, code examples or output to help show the changes working as expected.

Comment on lines -4 to -11
push:
paths:
- 'lib/python-sdk/**'
- '.github/workflows/ci-python-sdk.yml'
pull_request:
paths:
- 'lib/python-sdk/**'
- '.github/workflows/ci-python-sdk.yml'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes the push trigger because it was causing duplicate runs of the CI checks on each PR. Not a big deal, but HHS admins recently asked us to try to reduce GitHub action usage wherever we can.

Comment on lines +1 to +3
{
"python.analysis.typeCheckingMode": "basic"
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enables Python type checking when using VSCode

@classmethod
def from_json(cls, json_str: str) -> "CommonGrantsBaseModel":
def from_json(cls, json_str: str) -> Self:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This improves the intellisense output, so that the class method displays the name of the sub-class being instantiated (if it inherits from this base model) instead of always displaying "CommonGrantsBaseModel".

Comment on lines 82 to 86
DEFAULT_HANDLERS: dict[str, handle_func] = {
"field": handle_field,
"const": handle_const,
"match": handle_match,
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now we just define a base set of reserved words and their transformation functions, but this pattern makes it really easy to extend the mapping with additional key words.

mapping: dict,
depth: int = 0,
max_depth: int = 500,
handlers: dict[str, handle_func] = DEFAULT_HANDLERS,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows library users to provide their own custom handlers if they'd like to extend the base set.

@widal001 widal001 marked this pull request as ready for review May 20, 2025 16:54
Copy link
Collaborator

@DavidDudas-Intuitial DavidDudas-Intuitial left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. I left a small number of suggestions and questions.

Consider adding a README to explain usage of transform_from_mapping().

Returns:
A new dictionary containing the transformed data according to the mapping
"""
if depth > max_depth:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of enforcing a max depth? I agree 500 is an absurd depth, but curious what the intent is. Fear of runaway recursion?

Copy link
Collaborator Author

@widal001 widal001 May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a note explaining max depth here: docs(py-sdk): Add note explaining max_depth

It's mainly a check to avoid exceeding python's max recursion limit (1000) since this transformation function might be running on third-party (untrusted) mapping inputs.

In a future iteration I might consider refactoring this so we're using a loop instead of recursion and stress testing how depth is incremented.

for k in node:
if k in handlers:
return handlers[k](data, node)
return {k: transform_node(v, depth + 1) for k, v in node.items()}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot going on in these 4 lines! Maybe add a brief comment to explain what's happening.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I added some comments and example to show what happens at each step in this commit: refactor(py-sdk): Adds docs to transformation functions

widal001 added 5 commits May 23, 2025 11:33
- Uses more descriptive names for each handler function
- Changes how the known keys for handler functions are fetched
- Adds an example to the docstrings
- Adds more code comments to the recursive transform_node() step
The transformation function already handles literal values so an extra
reserved word just adds confusion and makes the mapping more verbose
Copy link
Collaborator

@DavidDudas-Intuitial DavidDudas-Intuitial left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Nice work!

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

Successfully merging this pull request may close these issues.

[Py SDK] Support for mapping-based transformations
2 participants