-
Notifications
You must be signed in to change notification settings - Fork 174
Fix is_submodule substring match causing spurious DAG nodes #1485
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
spock-yh
wants to merge
1
commit into
apache:main
Choose a base branch
from
spock-yh:fix/is-submodule-substring-match
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,40 @@ | |
| import tests.resources.typing_vs_not_typing | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("child_name", "parent_name", "expected"), | ||
| [ | ||
| ("foo", "foo", True), # same module | ||
| ("foo.bar", "foo", True), # direct child | ||
| ("foo.bar.baz", "foo", True), # nested child | ||
| ("foo.bar.baz", "foo.bar", True), # nested child of subpackage | ||
| ("foobar", "foo", False), # not a submodule, just a prefix without dot separator | ||
| ("hamilton.function_modifiers", "modifiers", False), # substring match, not a submodule | ||
| ("hamilton.function_modifiers.dependencies", "modifiers", False), # substring deeper | ||
| ("x.foo.y", "foo", False), # parent name in the middle, not a prefix | ||
| ("bar", "foo", False), # completely unrelated | ||
| ], | ||
| ids=[ | ||
| "same_module", | ||
| "direct_child", | ||
| "nested_child", | ||
| "nested_child_of_subpackage", | ||
| "prefix_without_dot", | ||
| "substring_not_submodule", | ||
| "substring_deeper", | ||
| "parent_in_middle", | ||
| "unrelated", | ||
| ], | ||
| ) | ||
| def test_is_submodule(child_name, parent_name, expected): | ||
| """Tests that is_submodule correctly checks module hierarchy using prefix matching.""" | ||
| from types import ModuleType | ||
|
|
||
| child = ModuleType(child_name) | ||
| parent = ModuleType(parent_name) | ||
| assert hamilton.graph_utils.is_submodule(child, parent) == expected | ||
|
|
||
|
|
||
| def test_find_functions(): | ||
| """Tests that we filter out _ functions when passed a module and don't pull in anything from the imports.""" | ||
| expected = [ | ||
|
|
@@ -64,6 +98,44 @@ def test_find_functions(): | |
| assert actual == expected | ||
|
|
||
|
|
||
| def test_find_functions_excludes_imports_with_substring_module_name(): | ||
| """Regression test: imported functions should not be included when the user module's | ||
| name is a substring of the imported function's module path. | ||
|
|
||
| Previously, is_submodule used `parent.__name__ in child.__name__` (substring match), | ||
| which caused e.g. a module named 'modifiers' to pull in functions from | ||
| 'hamilton.function_modifiers'. | ||
| """ | ||
| import sys | ||
| from types import ModuleType | ||
|
|
||
| from hamilton.function_modifiers import source, value | ||
|
Comment on lines
+109
to
+112
Contributor
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. top level imports please |
||
|
|
||
| # Create a fake module named "modifiers" with one real function and two imports | ||
| mod = ModuleType("modifiers") | ||
|
|
||
| def my_func(x: int) -> int: | ||
| return x * 2 | ||
|
|
||
| # Assign the function to the module so inspect.getmodule can resolve it | ||
| my_func.__module__ = "modifiers" | ||
| mod.my_func = my_func | ||
| mod.source = source | ||
| mod.value = value | ||
|
|
||
| # Register in sys.modules so inspect.getmodule can find it | ||
| sys.modules["modifiers"] = mod | ||
| try: | ||
| actual = hamilton.graph_utils.find_functions(mod) | ||
| actual_names = [name for name, _ in actual] | ||
| assert actual_names == ["my_func"], ( | ||
| f"Expected only ['my_func'] but got {actual_names}. " | ||
| "Imported functions from hamilton.function_modifiers should not be included." | ||
| ) | ||
| finally: | ||
| del sys.modules["modifiers"] | ||
|
|
||
|
|
||
| def test_find_functions_from_temporary_function_module(): | ||
| """Tests that we handle the TemporaryFunctionModule object correctly.""" | ||
| expected = [ | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
top level import please.