Skip to content
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

Enh: allow apps to be referenced by scope to avoid namespace collisions #1789

Open
khiron opened this issue Mar 21, 2024 · 1 comment
Open
Assignees

Comments

@khiron
Copy link
Collaborator

khiron commented Mar 21, 2024

_get_app_matching_name (and get_app) will take a full scope but only use the app name when requesting app attributes to construct the app instance so get_app will return the first app that matches the name without reference to scope.

Modify get_app(name) and available_apps(name) and app_help(name) to support scoped names, and get the following test to pass

# add a new to_json app that should collide with the internal to_json app
@define_app
class to_json:
    def main(self, data: c3types.SerialisableType) -> str:
        """Convert primitive python types to json string."""
        return json.dumps(data)
    @classmethod
    def _requires_imports(cls):
        return ["from cogent3.app import typing as c3types"]   

from stevedore.extension import ExtensionManager

@pytest.mark.parametrize("install_temp_app", [to_json], indirect=True)
def test_app_namespace_collision(install_temp_app):
    em = ExtensionManager(namespace="cogent3.app", invoke_on_load=False)
    to_json_extensions = [ext for ext in em.extensions if ext.name == "to_json"]
    assert len(to_json_extensions) == 2

    with pytest.raises(NameError):  # should raise an error if name is not scoped
        to_json = get_app("to_json")

    assert available_apps("to_json").shape[0] == 2

    # get apps by scope 
    internal_to_json = get_app("cogent3.app.io.to_json")
    added_to_json = get_app("temp_app_to_json.to_json")

    # test the new app and internal app are equivalent
    data = {"a": 1, "b": 2}
    assert internal_to_json(data) == added_to_json(data) == json.dumps(data)
@khiron khiron self-assigned this Mar 21, 2024
@khiron
Copy link
Collaborator Author

khiron commented Mar 24, 2024

Create a new test_plugin.py for testing plugin apps, that includes the

def install_temp_app(request: pytest.FixtureRequest):

fixture for dynamically loading and unloading test apps. Move tests into this file from test_composable.py that specifically test registration of apps and remove the xfail decoration from these tests.

Test help documentation using a test app and _make_apphelp_docstring(app)

khiron added a commit that referenced this issue Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant