Skip to content

Conversation

@tfliss
Copy link
Contributor

@tfliss tfliss commented Sep 30, 2025

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.

@ialarmedalien
Copy link
Collaborator

Looks like there are conflicts that need to be resolved before the tests will run.

@sujaypatil96
Copy link
Member

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.

@codecov
Copy link

codecov bot commented Oct 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.41%. Comparing base (a1381ca) to head (7438906).
⚠️ Report is 9 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tfliss tfliss requested a review from ialarmedalien October 20, 2025 20:42
@tfliss
Copy link
Contributor Author

tfliss commented Oct 20, 2025

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.

Looks like there are conflicts that need to be resolved before the tests will run.

@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.

Comment on lines 29 to 30
if module_name is None:
module_name = "test"
Copy link
Collaborator

@ialarmedalien ialarmedalien Oct 21, 2025

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"
Copy link
Collaborator

@ialarmedalien ialarmedalien Oct 21, 2025

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)

@ialarmedalien
Copy link
Collaborator

@tfliss I added my suggested changes in a commit.

@sujaypatil96 sujaypatil96 merged commit f71d115 into main Oct 27, 2025
17 checks passed
@sujaypatil96 sujaypatil96 deleted the issue-9203_compile_python_module_name branch October 27, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants