-
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
Conversation
|
Looks like there are conflicts that need to be resolved before the tests will run. |
|
Would you be able to help us resolve the merge conflict so we can review and merge this PR @tfliss? apologies for the delay in reviewing it. |
…b.com/linkml/linkml-runtime into issue-9203_compile_python_module_name
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #457 +/- ##
==========================================
+ Coverage 78.24% 78.41% +0.16%
==========================================
Files 52 52
Lines 4533 4536 +3
Branches 988 989 +1
==========================================
+ Hits 3547 3557 +10
+ Misses 768 764 -4
+ Partials 218 215 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@sujaypatil96, @ialarmedalien Much appreciation for your reviews and for pointing out the conflict. I believe the conflicts are resolved now so the checks can run. I hope this didn't delay you. |
| if module_name is None: | ||
| module_name = "test" |
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.
Suggest using if not module_name instead of None -- otherwise, it's possible to create a module with an empty string as the name. I'd also set the default to "test", rather than None, as checking that the module_name is not falsy will catch "" and set it to "test" instead.
|
|
||
| def test_default_module_name(module_1): | ||
| m = compile_python(module_1) | ||
| assert m.__name__ == "test" |
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.
In no way because I was procrastinating on sorting out my own PRs, but possible reworking of these tests:
from __future__ import annotations
from types import ModuleType
import pytest
from linkml_runtime.utils.compile_python import compile_python
@pytest.fixture(scope="module")
def module_1() -> str:
return """
x: int = 2
def fun(value: int):
return f'known value {value}'
"""
@pytest.fixture(scope="module")
def module_import() -> 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"), ("module_1", "module_1")])
def test_compile_python_module_name(module_1: str, name_arg: str | None, module_name: str) -> None:
"""Test the compilation of python code to create a module."""
m = compile_python(module_1, module_name=name_arg)
check_generated_module(m, module_name)
@pytest.mark.parametrize(("name_arg", "module_name"), [(None, "test"), ("", "test"), ("module_1", "module_1")])
def test_compile_python_module_import_local_module(
module_1: str,
module_import: 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."""
# compile the first module and check functionality
m = compile_python(module_1, module_name=name_arg)
check_generated_module(m, module_name)
# switch in the appropriate module name for module 1
module_import = module_import.replace("MODULE_NAME", module_name)
m2 = compile_python(module_import, module_name="module_2", package_path=".")
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)|
@tfliss I added my suggested changes in a commit. |
This change adds a module_name keyword to the compile_python utility method to override the default 'test'.
This makes it possible to compile multiple modules and import one from the other.
An issue test is also included that verifies new behavior and the existing default behavior.
Please excuse the switched numbers in the branch name, let me know I can cut a new pr with a different branch.