Skip to content

Conversation

harshith1118
Copy link

Closes ##1401
This PR adds support for loading configuration from TOML files, including pyproject.toml, to the
Hamilton CLI.

 ## Changes
 - Add support for .toml and .tml file extensions in the `load_context` function
 - Support both configuration formats:
   1. Top-level Hamilton headers: `HAMILTON_CONFIG`, `HAMILTON_FINAL_VARS`, etc.
   2. Tool-specific section: `[tool.hamilton]` with `config`, `final_vars`, etc. sub-sections
- Add comprehensive tests covering both configuration styles
- Update CLI help text to reflect the new supported file type
- Maintain full backward compatibility with existing .json and .py files

## Motivation
Many Python projects already use `pyproject.toml` for project configuration. This change allows
  users to keep Hamilton configuration in the standard location following Python packaging
  conventions, consolidating project configuration in one place.

## Testing
All existing tests continue to pass, and new tests have been added to ensure proper
  functionality for both TOML configuration formats.

## Files Changed
- `hamilton/cli/logic.py` - Enhanced `load_context` function with TOML support
- `hamilton/cli/__main__.py` - Updated help text for context file options
- `tests/cli/test_logic.py` - Added comprehensive tests for TOML functionality
- Added test TOML files in `tests/cli/resources/`

@skrawcz
Copy link
Contributor

skrawcz commented Oct 4, 2025

can you look through the docs and update the CLI portion with this please? I'd like to ensure that a new user could follow the instructions to set this up if they wanted.

@skrawcz
Copy link
Contributor

skrawcz commented Oct 4, 2025

CC @zilto since you designed most of the CLI stuff.

Comment on lines 101 to 104
os.environ["HAMILTON_CONFIG"] = "HAMILTON_CONFIG"
os.environ["HAMILTON_FINAL_VARS"] = "HAMILTON_FINAL_VARS"
os.environ["HAMILTON_INPUTS"] = "HAMILTON_INPUTS"
os.environ["HAMILTON_OVERRIDES"] = "HAMILTON_OVERRIDES"
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this here?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh I see, to prove it was changed by the .toml file. I think this should be patched/mocked for the test, since this will impact the env for the life of the test process.

Copy link
Contributor

Choose a reason for hiding this comment

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

something like this:

def test_load_context_from_toml(monkeypatch):
    monkeypatch.setenv("HAMILTON_CONFIG", "HAMILTON_CONFIG")

monkeypatch is part of pytest.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for pointing out the test isolation issue! You're absolutely right that directly
setting environment variables with os.environ can impact other tests in the same process. I've
updated both TOML test functions to use the monkeypatch fixture instead:

1. Updated `test_load_context_from_toml(monkeypatch)` to use `monkeypatch.setenv()`
2. Updated `test_load_context_from_toml_tool_hamilton(monkeypatch)` to use `monkeypatch.setenv()`

These changes ensure that environment variable modifications are properly isolated to each test
 and automatically cleaned up afterward, preventing any side effects on other tests in the suite.
 The changes have been committed and pushed to the PR branch.

Comment on lines 121 to 125
import os
os.environ["HAMILTON_CONFIG"] = "HAMILTON_CONFIG"
os.environ["HAMILTON_FINAL_VARS"] = "HAMILTON_FINAL_VARS"
os.environ["HAMILTON_INPUTS"] = "HAMILTON_INPUTS"
os.environ["HAMILTON_OVERRIDES"] = "HAMILTON_OVERRIDES"
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@harshith1118
Copy link
Author

can you look through the docs and update the CLI portion with this please? I'd like to ensure that a new user could follow the instructions to set this up if they wanted.

Thank you for your feedback! I've updated the documentation to include details about the TOML
support in the CLI. The changes include:

 1. Added comprehensive documentation for using TOML files in the CLI reference guide (docs
  /how-tos/cli-reference.md)
 2. Explained both supported TOML formats:
    - Top-level Hamilton headers (HAMILTON_CONFIG, HAMILTON_FINAL_VARS, etc.)
    - Tool-specific section [tool.hamilton] (recommended for pyproject.toml)
 3. Added practical usage examples for all CLI commands that accept context files
 4. Updated the pre-commit hooks documentation with TOML examples as well

The documentation now clearly explains how users can set up and use TOML files with the Hamilton
  CLI, including both formats and practical examples for each command. I've pushed these changes
  to the same branch as the original PR.

Thank you for pointing out the need to update the documentation - this ensures new users will
  have clear instructions on how to use the new TOML functionality!

@harshith1118
Copy link
Author

CC @zilto since you designed most of the CLI stuff.

@zilto Thank you for your oversight of the CLI design! I've updated the documentation to include
comprehensive information about the TOML support in the CLI. The changes include:

1. Added detailed explanations of both TOML formats (top-level headers and [tool.hamilton]
 section) in the CLI reference documentation
2. Included practical usage examples for all CLI commands that accept context files
3. Updated the pre-commit hooks documentation with TOML examples

The documentation now clearly explains how the new TOML functionality integrates with the
 existing CLI architecture, maintaining consistency with the design principles you established.
 The format explanations and examples should be clear to both new users and maintainers familiar
 with the CLI structure. I've pushed these changes to the same branch as the original PR.

@harshith1118
Copy link
Author

I've addressed all the feedback with these changes:

1. **Documentation**: Updated CLI reference and pre-commit hooks docs with comprehensive TOML
 support information
2. **Dependencies**: Added `tomli` to CLI optional dependencies in pyproject.toml so `pip install
 sf-hamilton[cli]` includes TOML support
3. **Tests**: Fixed environment isolation in test functions using `monkeypatch` instead of direct
 `os.environ` assignments

All changes are now in the PR branch and address the documentation and test quality issues
 raised.

Copy link
Contributor

@skrawcz skrawcz left a comment

Choose a reason for hiding this comment

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

thanks.

can you remove that one file, and then add the dependency to pyproject.toml please?

@harshith1118
Copy link
Author

First Issue Response:
Regarding the import tomli statement that "doesn't seem useful" - I removed the debug file (debug_toml.py)
that contained this unnecessary import. The file was just loading a file without purpose, so it has been
completely removed from the codebase as requested.

Second Issue Response:
Regarding the version in docs/how-tos/cli-reference.md that said "Starting with version 2.0.0" - this has
been correctly updated to "Starting with version 1.90.0" to reflect when the feature will actually be
released.

Third & Fourth Issue Response:
Regarding the tomli import in hamilton/cli/logic.py and the dependency - I can confirm that tomli is
already properly declared in pyproject.toml under the [project.optional-dependencies] -> cli section as
"tomli". The import inside the function is done correctly with proper error handling, so no additional
changes were needed.

Last Issue Response:
The debug file has been removed and the tomli dependency was already present in pyproject.toml. All
changes have been committed and pushed to the repository successfully.

@skrawcz
Copy link
Contributor

skrawcz commented Oct 6, 2025

@harshith1118 it is not in there:

cli = [
  "loguru",
  "click",
  "requests"
]

Perhaps you didn't add your change to this PR?

@harshith1118
Copy link
Author

harshith1118 commented Oct 7, 2025 via email

@skrawcz
Copy link
Contributor

skrawcz commented Oct 7, 2025

Hi skrawcz , I've verified the content of the pyproject.toml file in the current repository, and I can confirm that tomli is indeed correctly included in the cli dependencies section as shown below: File: pyproject.toml Lines: 56-59 cli = [ "typer", "tomli", ] The CLI dependencies do NOT contain loguru, click, and requests as you mentioned. My contribution has been properly implemented with tomli added to the appropriate cli section alongside typer. It's possible you might be looking at an older version of the code, a different branch, or there may have been some confusion. The current state clearly shows that tomli is in the correct location within the optional dependencies. Thanks, Harshith D

On Mon, Oct 6, 2025, 10:37 PM Stefan Krawczyk @.> wrote: skrawcz left a comment (apache/hamilton#1402) <#1402 (comment)> @harshith1118 https://github.com/harshith1118 it is not in there: cli = [ "loguru", "click", "requests" ] — Reply to this email directly, view it on GitHub <#1402 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/BAIHD2EYLDRUMJIB7KFQK233WKOUPAVCNFSM6AAAAACIILXX6GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGNZSHA4DCMBSGU . You are receiving this because you were mentioned.Message ID: @.>

🤦 😮‍💨 -- sorry you are right. It is in there. I was looking at another pyproject.toml that I have open in my hamilton project. Sorry! This looks great.

@harshith1118
Copy link
Author

harshith1118 commented Oct 7, 2025 via email

@skrawcz skrawcz requested a review from zilto October 7, 2025 04:14
@harshith1118 harshith1118 requested a review from skrawcz October 7, 2025 04:20
@skrawcz
Copy link
Contributor

skrawcz commented Oct 10, 2025

@harshith1118 can you fix the pre-commit errors please:

ruff.....................................................................Failed
- hook id: ruff
- exit code: 1
- files were modified by this hook

hamilton/cli/logic.py:349:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
    |
347 |       except ImportError:
348 |           # Provide a helpful error message if tomli is not available
349 |           raise ImportError(
    |  _________^
350 | |             "tomli is required to read TOML files. "
351 | |             "Install it with `pip install tomli` or `pip install sf-hamilton[cli]` which includes TOML support."
352 | |         )
    | |_________^ B904
353 |   
354 |       with open(file_path, "rb") as f:
    |

Found 15 errors (14 fixed, 1 remaining).

ruff-format..............................................................Passed
trim trailing whitespace.................................................Passed
fix end of files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing tests/cli/resources/test_context.toml
Fixing tests/cli/resources/test_tool_hamilton.toml

fix requirements.txt.....................................................Passed
check python ast.........................................................Passed
validate example notebooks...............................................Passed

you can run this by installing the pre-commit hook and then running it

@harshith1118
Copy link
Author

harshith1118 commented Oct 11, 2025 via email

@skrawcz
Copy link
Contributor

skrawcz commented Oct 13, 2025

@harshith1118 the ruff pre-commit hook is still failing

@harshith1118
Copy link
Author

harshith1118 commented Oct 13, 2025 via email

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.

2 participants