diff --git a/src/pyproject_api/_backend.py b/src/pyproject_api/_backend.py index 331e326..8fcf92d 100644 --- a/src/pyproject_api/_backend.py +++ b/src/pyproject_api/_backend.py @@ -23,7 +23,8 @@ def __init__(self, backend_module, backend_obj): self.backend_object = backend_obj backend = importlib.import_module(self.backend_module) if self.backend_object: - backend = getattr(backend, self.backend_object) + for name in self.backend_object.split("."): + backend = getattr(backend, name) self.backend = backend def __call__(self, name, *args, **kwargs): diff --git a/tests/test_frontend.py b/tests/test_frontend.py index e3313bb..f6172d2 100644 --- a/tests/test_frontend.py +++ b/tests/test_frontend.py @@ -232,19 +232,31 @@ def test_create_args_from_folder_rejects_bad_build_system_type(tmp_path: Path, t SubprocessFrontend.create_args_from_folder(tmp_path) -def test_backend_obj(tmp_path: Path) -> None: - toml = """ +@pytest.mark.parametrize( + "backend_obj", + [ + pytest.param("backend", id="attribute"), + pytest.param("backend:", id="trailing-colon"), + pytest.param("holder.backend", id="nested"), + pytest.param("holder.nested.backend", id="deeply-nested"), + ], +) +def test_backend_obj(tmp_path: Path, backend_obj: str) -> None: + toml = f""" [build-system] requires=[] - build-backend = "build.api:backend:" + build-backend = "build.api:{backend_obj}" backend-path=["."] """ api = """ + from types import SimpleNamespace + class A: def get_requires_for_build_sdist(self, config_settings=None): return ["a"] backend = A() + holder = SimpleNamespace(backend=backend, nested=SimpleNamespace(backend=backend)) """ (tmp_path / "pyproject.toml").write_text(dedent(toml)) build = tmp_path / "build" @@ -253,9 +265,7 @@ def get_requires_for_build_sdist(self, config_settings=None): (build / "api.py").write_text(dedent(api)) frontend = SubprocessFrontend(*SubprocessFrontend.create_args_from_folder(tmp_path)[:-1]) result = frontend.get_requires_for_build_sdist() - for left, right in zip(result.requires, (Requirement("a"),), strict=False): - assert isinstance(left, Requirement) - assert str(left) == str(right) + assert result.requires == (Requirement("a"),) @pytest.mark.parametrize("of_type", ["wheel", "sdist"])