-
Notifications
You must be signed in to change notification settings - Fork 14
Add ability to resolve any yaml --> dataclass using utility + introduce ${git_hash:} resolver #307
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a23d4cf
add new resolver + load yaml directly
svij-sc 2cd5feb
docs
svij-sc a01d8f1
pr comments
svij-sc ab7de27
pr comments round 2
svij-sc 6b9fab0
fix
svij-sc e953c5b
comment
svij-sc 3b7c3f9
Merge branch 'main' into svij/add-yaml-and-git-hash-resolver
svij-sc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from typing import Type, TypeVar, cast | ||
|
|
||
| from omegaconf import OmegaConf | ||
|
|
||
| from gigl.common import Uri | ||
| from gigl.common.logger import Logger | ||
| from gigl.common.omegaconf_resolvers import register_resolvers | ||
| from gigl.src.common.utils.file_loader import FileLoader | ||
|
|
||
| logger = Logger() | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
| register_resolvers() | ||
|
svij-sc marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def load_resolved_yaml(uri: Uri, type_of_object: Type[T]) -> T: | ||
| with FileLoader().load_to_temp_file(uri) as tf: | ||
| test_spec_data = OmegaConf.load(tf.name) | ||
|
|
||
| # Merge OmegaConf structured config with loaded data for validation | ||
| merged_config = OmegaConf.merge( | ||
| OmegaConf.structured(type_of_object), test_spec_data | ||
| ) | ||
|
|
||
| # Convert to strongly typed T object | ||
| return cast(T, OmegaConf.to_object(merged_config)) | ||
|
svij-sc marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import os | ||
| import tempfile | ||
| import textwrap | ||
| import unittest | ||
| from dataclasses import dataclass, field | ||
| from datetime import datetime | ||
| from typing import List | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from gigl.common import LocalUri | ||
| from gigl.common.utils.yaml_loader import load_resolved_yaml | ||
|
|
||
|
|
||
| @dataclass | ||
| class _SubConfig: | ||
| name: str | ||
| value: int | ||
| enabled: bool = True | ||
| tags: List[str] = field(default_factory=list) | ||
|
svij-sc marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @dataclass | ||
| class _Complex_TestConfig: | ||
| basic_config: _SubConfig | ||
| description: str | ||
|
|
||
|
|
||
| class YamlLoaderTest(unittest.TestCase): | ||
| def setUp(self): | ||
| """Set up test fixtures.""" | ||
| super().setUp() | ||
| self.temp_file = tempfile.NamedTemporaryFile( | ||
| mode="w", suffix=".yaml", delete=False | ||
| ) | ||
|
|
||
| def tearDown(self): | ||
| self.temp_file.close() | ||
| os.remove(self.temp_file.name) | ||
|
svij-sc marked this conversation as resolved.
|
||
| super().tearDown() | ||
|
|
||
| def test_load_resolved_yaml_simple_config(self): | ||
| """Test loading a simple YAML configuration.""" | ||
|
|
||
| contents = textwrap.dedent( | ||
| """ | ||
| basic_config: | ||
| name: "experiment_${now:%Y%m%d}" | ||
| value: 42 | ||
| enabled: true | ||
| tags: | ||
| - "tag_${git_hash:}" | ||
| - "${basic_config.value}" # resolves to 42 | ||
| description: "This is a test description" | ||
| """ | ||
| ) | ||
| with self.temp_file: | ||
| self.temp_file.write(contents) | ||
|
|
||
| patch_commit_hash = "1234567890" | ||
| patch_datetime = datetime(2023, 12, 15, 14, 30, 22) | ||
|
|
||
| expected_result = _Complex_TestConfig( | ||
| basic_config=_SubConfig( | ||
| name=f"experiment_{patch_datetime.strftime('%Y%m%d')}", | ||
| value=42, | ||
| enabled=True, | ||
| tags=[f"tag_{patch_commit_hash}", "42"], | ||
| ), | ||
| description="This is a test description", | ||
| ) | ||
| with patch( | ||
| "gigl.common.omegaconf_resolvers.subprocess.run" | ||
| ) as mock_subprocess_run, patch( | ||
| "gigl.common.omegaconf_resolvers.datetime" | ||
| ) as mock_datetime: | ||
|
kmontemayor2-sc marked this conversation as resolved.
|
||
| mock_result = MagicMock() | ||
| mock_result.stdout = patch_commit_hash | ||
| mock_subprocess_run.return_value = mock_result | ||
| mock_datetime.now.return_value = patch_datetime | ||
|
|
||
| uri = LocalUri(self.temp_file.name) | ||
| result: _Complex_TestConfig = load_resolved_yaml(uri, _Complex_TestConfig) | ||
| self.assertTrue(isinstance(result, _Complex_TestConfig)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. self.assertIsInstance |
||
|
|
||
| self.assertEqual(result, expected_result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.