-
Notifications
You must be signed in to change notification settings - Fork 233
refactor: implement registries as mutable mappings #1237
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
GlowLED
wants to merge
2
commits into
ModelTC:main
Choose a base branch
from
GlowLED:refactor/register-dict-inheritance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,147 @@ | ||
| import importlib.util | ||
| import sys | ||
| import types | ||
| import unittest | ||
| from collections.abc import MutableMapping | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||
|
|
||
|
|
||
| def load_module(module_name, path): | ||
| spec = importlib.util.spec_from_file_location(module_name, path) | ||
| if spec is None or spec.loader is None: | ||
| raise ImportError(f"Cannot load module {module_name} from {path}") | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def first_target(): | ||
| return "first" | ||
|
|
||
|
|
||
| def second_target(): | ||
| return "second" | ||
|
|
||
|
|
||
| platform_registry = load_module("_platform_registry_for_test", REPO_ROOT / "lightx2v_platform" / "registry_factory.py") | ||
|
|
||
| platform_package = types.ModuleType("lightx2v_platform") | ||
| platform_package.registry_factory = platform_registry | ||
| with patch.dict( | ||
| sys.modules, | ||
| { | ||
| "lightx2v_platform": platform_package, | ||
| "lightx2v_platform.registry_factory": platform_registry, | ||
| }, | ||
| ): | ||
| runtime_registry = load_module("_runtime_registry_for_test", REPO_ROOT / "lightx2v" / "utils" / "registry_factory.py") | ||
|
|
||
| training_registry = load_module( | ||
| "_training_registry_for_test", | ||
| REPO_ROOT / "lightx2v_train" / "lightx2v_train" / "utils" / "registry.py", | ||
| ) | ||
|
|
||
|
|
||
| REGISTER_CLASSES = { | ||
| "runtime": runtime_registry.Register, | ||
| "platform": platform_registry.Register, | ||
| "training": training_registry.Register, | ||
| } | ||
|
|
||
|
|
||
| class RegisterTest(unittest.TestCase): | ||
| def test_implements_mutable_mapping(self): | ||
| for name, register_cls in REGISTER_CLASSES.items(): | ||
| with self.subTest(register=name): | ||
| self.assertTrue(issubclass(register_cls, MutableMapping)) | ||
| self.assertFalse(issubclass(register_cls, dict)) | ||
|
|
||
| def test_initial_mapping_uses_registry_storage(self): | ||
| def initial_target(): | ||
| return "initial" | ||
|
|
||
| def keyword_target(): | ||
| return "keyword" | ||
|
|
||
| for name, register_cls in REGISTER_CLASSES.items(): | ||
| with self.subTest(register=name): | ||
| registry = register_cls({"initial": initial_target}, keyword=keyword_target) | ||
|
|
||
| self.assertEqual(len(registry), 2) | ||
| self.assertEqual(list(registry), ["initial", "keyword"]) | ||
| self.assertEqual(dict(registry), {"initial": initial_target, "keyword": keyword_target}) | ||
| self.assertEqual(str(registry), str({"initial": initial_target, "keyword": keyword_target})) | ||
|
|
||
| def test_registers_named_and_unnamed_targets(self): | ||
| for name, register_cls in REGISTER_CLASSES.items(): | ||
| with self.subTest(register=name): | ||
| registry = register_cls() | ||
|
|
||
| @registry("named") | ||
| def named_target(): | ||
| return "named" | ||
|
|
||
| @registry | ||
| def automatic_target(): | ||
| return "automatic" | ||
|
|
||
| self.assertIs(registry["named"], named_target) | ||
| self.assertIs(registry.get("automatic_target"), automatic_target) | ||
| self.assertEqual(set(registry.keys()), {"named", "automatic_target"}) | ||
| self.assertEqual(set(registry.values()), {named_target, automatic_target}) | ||
| self.assertEqual(set(registry.items()), {("named", named_target), ("automatic_target", automatic_target)}) | ||
|
|
||
| def test_rejects_invalid_and_duplicate_targets(self): | ||
| for name, register_cls in REGISTER_CLASSES.items(): | ||
| with self.subTest(register=name): | ||
| registry = register_cls() | ||
|
|
||
| def original_target(): | ||
| return "original" | ||
|
|
||
| with self.assertRaisesRegex(Exception, "must be callable"): | ||
| registry.register("not callable") | ||
|
|
||
| registry("target")(original_target) | ||
| with self.assertRaisesRegex(Exception, "already exists"): | ||
| registry("target")(lambda: None) | ||
| self.assertIs(registry["target"], original_target) | ||
|
|
||
| def test_supports_mutable_mapping_operations(self): | ||
| for name, register_cls in REGISTER_CLASSES.items(): | ||
| with self.subTest(register=name): | ||
| registry = register_cls() | ||
|
|
||
| registry.update({"first": first_target}) | ||
| registry.setdefault("second", second_target) | ||
| self.assertEqual(len(registry), 2) | ||
| self.assertIn("first", registry) | ||
|
|
||
| self.assertIs(registry.pop("first"), first_target) | ||
| del registry["second"] | ||
| self.assertFalse(registry) | ||
|
|
||
| registry["first"] = first_target | ||
| registry["second"] = second_target | ||
| registry.clear() | ||
| self.assertEqual(dict(registry), {}) | ||
|
|
||
| def test_merges_registries_and_rejects_conflicts(self): | ||
| for name, register_cls in REGISTER_CLASSES.items(): | ||
| with self.subTest(register=name): | ||
| registry = register_cls({"first": first_target}) | ||
| other = register_cls({"second": second_target}) | ||
|
|
||
| registry.merge(other) | ||
| self.assertEqual(dict(registry), {"first": first_target, "second": second_target}) | ||
|
|
||
| with self.assertRaisesRegex(Exception, "already exists"): | ||
| registry.merge(register_cls({"first": first_target})) | ||
| self.assertIs(registry["first"], first_target) | ||
|
|
||
|
|
||
| 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.