diff --git a/src/validate_pyproject/plugins/__init__.py b/src/validate_pyproject/plugins/__init__.py index 813ac2a..24890ba 100644 --- a/src/validate_pyproject/plugins/__init__.py +++ b/src/validate_pyproject/plugins/__init__.py @@ -55,9 +55,10 @@ def fragment(self) -> str: class PluginWrapper: - def __init__(self, tool: str, load_fn: "Plugin"): + def __init__(self, tool: str, load_fn: "Plugin", *, fragment: str = ""): self._tool = tool self._load_fn = load_fn + self._fragment = fragment @property def id(self) -> str: @@ -73,7 +74,7 @@ def schema(self) -> "Schema": @property def fragment(self) -> str: - return "" + return self._fragment @property def help_text(self) -> str: @@ -83,7 +84,10 @@ def help_text(self) -> str: return Template(tpl).safe_substitute(tool=self.tool, id=self.id) def __repr__(self) -> str: - return f"{self.__class__.__name__}({self.tool!r}, {self.id})" + args = [repr(self.tool), self.id] + if self.fragment: + args.append(f"fragment={self.fragment!r}") + return f"{self.__class__.__name__}({', '.join(args)})" if typing.TYPE_CHECKING: @@ -118,7 +122,8 @@ def load_from_entry_point(entry_point: EntryPoint) -> PluginWrapper: """Carefully load the plugin, raising a meaningful message in case of errors""" try: fn = entry_point.load() - return PluginWrapper(entry_point.name, fn) + fragment = getattr(fn, "fragment", "") + return PluginWrapper(entry_point.name, fn, fragment=fragment) except Exception as ex: raise ErrorLoadingPlugin(entry_point=entry_point) from ex diff --git a/tests/test_plugins.py b/tests/test_plugins.py index a5e8d01..ee01823 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -6,6 +6,7 @@ import pytest +import validate_pyproject.api from validate_pyproject import plugins from validate_pyproject.plugins import ENTRYPOINT_GROUP, ErrorLoadingPlugin @@ -31,6 +32,20 @@ def test_load_from_entry_point__error(): plugins.load_from_entry_point(fake) +def test_load_from_entry_point_fragment(monkeypatch): + monkeypatch.setattr( + validate_pyproject.api.load_builtin_plugin, + "fragment", + "/properties/tool/properties", + raising=False, + ) + entry = "validate_pyproject.api:load_builtin_plugin" + real = EntryPoint("cibuildwheel", entry, ENTRYPOINT_GROUP) + p = plugins.load_from_entry_point(real) + assert p.fragment == "/properties/tool/properties" + assert p.tool == "cibuildwheel" + + def is_entry_point(ep): return all(hasattr(ep, attr) for attr in ("name", "load"))