-
Couldn't load subscription status.
- Fork 35
module_name parameter for compile_python resolves linkml/linkml#2903 #457
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
+78
−9
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bbfcd0f
module_name parameter for compile_python
tfliss 541bc2d
Merge branch 'main' into issue-9203_compile_python_module_name
sierra-moxon f7be37e
move compile_python test; fix docstrings
tfliss 0c68734
Merge branch 'main' into issue-9203_compile_python_module_name
tfliss 51e611f
Merge branch 'issue-9203_compile_python_module_name' of https://githu…
tfliss e2ae846
Merge branch 'main' into issue-9203_compile_python_module_name
ialarmedalien 53c7ad0
Implementing suggestions made in PR comments
ialarmedalien 7438906
Merge branch 'main' into issue-9203_compile_python_module_name
amc-corey-cox 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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from types import ModuleType | ||
|
|
||
| import pytest | ||
|
|
||
| from linkml_runtime.utils.compile_python import compile_python | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def base_module() -> str: | ||
| return """ | ||
| x: int = 2 | ||
|
|
||
| def fun(value: int): | ||
| return f'known value {value}' | ||
| """ | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def importing_module() -> str: | ||
| return """ | ||
| import MODULE_NAME as m | ||
|
|
||
| def more_fun(message: str): | ||
| return f'got "{message}"' | ||
| """ | ||
|
|
||
|
|
||
| def check_generated_module(module: ModuleType, module_name: str) -> None: | ||
| assert isinstance(module, ModuleType) | ||
| assert module.__name__ == module_name | ||
| assert module.x == 2 | ||
| assert module.fun(3) == "known value 3" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("name_arg", "module_name"), [(None, "test"), ("", "test"), ("base_module", "base_module")]) | ||
| def test_compile_python_module_name(base_module: str, name_arg: str | None, module_name: str) -> None: | ||
| """Test the compilation of python code to create a module.""" | ||
| m = compile_python(base_module, module_name=name_arg) | ||
| check_generated_module(m, module_name) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("name_arg", "module_name"), [(None, "test"), ("", "test"), ("base_module", "base_module")]) | ||
| def test_compile_python_importing_module_local_module( | ||
| base_module: str, | ||
| importing_module: str, | ||
| name_arg: str | None, | ||
| module_name: str, | ||
| ) -> None: | ||
| """Test the compilation of python code to create a local module and then compile a second module that imports the first.""" | ||
| m = compile_python(base_module, module_name=name_arg) | ||
| check_generated_module(m, module_name) | ||
|
|
||
| # switch in the appropriate module name | ||
| importing_module_text = importing_module.replace("MODULE_NAME", module_name) | ||
| m2 = compile_python(importing_module_text, package_path=".", module_name="module_2") | ||
| assert isinstance(m2, ModuleType) | ||
| assert m2.__name__ == "module_2" | ||
| assert m2.more_fun("hello") == 'got "hello"' | ||
|
|
||
| # check the imported module, m2.m, has the correct type, name, etc. | ||
| check_generated_module(m2.m, module_name) | ||
Oops, something went wrong.
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.