pypa / pyproject-hooks

A low-level library for calling build-backends in `pyproject.toml`-based project
https://pyproject-hooks.readthedocs.io/
MIT License
122 stars 49 forks source link

Tidy-up API a bit #161

Closed KOLANICH closed 1 year ago

KOLANICH commented 1 year ago

Since this package got renamed, it may be a good time to tidy-up the API a bit into a more convenient one. I know everyone can do it himself, but anyway shipping tidy API inside the main package may worth.


class Builder:
    __slots__ = ("caller", )

    def __init__(self, caller: BuildBackendHookCaller):
        self.caller = caller

    def build(self, *args, **kwargs):
        raise NotImplementedError
    def get_req(self, *args, **kwargs):
        raise NotImplementedError
    def prepare_metadata(self, *args, **kwargs):
        raise NotImplementedError

class Editable(Builder):
    __slots__ = ()

    def build(self, *args, **kwargs):
        return self.caller.build_editable(*args, **kwargs)
    def get_req(self, *args, **kwargs):
        return self.caller.get_requires_for_build_editable(*args, **kwargs)
    def prepare_metadata(self, *args, **kwargs):
        return self.caller.prepare_metadata_for_build_editable(*args, **kwargs)

class Wheel(Builder):
    __slots__ = ()

    def build(self, *args, **kwargs):
        return self.caller.build_wheel(*args, **kwargs)
    def get_req(self, *args, **kwargs):
        return self.caller.get_requires_for_build_wheel(*args, **kwargs)
    def prepare_metadata(self, *args, **kwargs):
        return self.caller.prepare_metadata_for_build_wheel(*args, **kwargs)

class SDist(Builder):
    __slots__ = ()

    def build(self, *args, **kwargs):
        return self.caller.build_sdist(*args, **kwargs)
    def get_req(self, *args, **kwargs):
        return self.caller.get_requires_for_build_sdist(*args, **kwargs)
    def prepare_metadata(self, *args, **kwargs):
        return self.caller.prepare_metadata_for_build_sdist(*args, **kwargs)
takluyver commented 1 year ago

Personally, I like the simplicity of directly matching the methods in the spec. I don't think adding another API on top of that actually makes anything particularly clearer - and in fact having two different APIs to do the same things is less clear overall.

Also, the spec doesn't include prepare_metadata_for_build_sdist, unless that's been added more recently, which spoils the symmetry a bit.

So I'm inclined to say it's not worth it, sorry.

pradyunsg commented 1 year ago

Personally, I like the simplicity of directly matching the methods in the spec.

Same. I think higher level tools like build are the right place for such a "tidy API" (build even has that: https://pypa-build.readthedocs.io/en/stable/api.html#build.ProjectBuilder). Besides, I expect that the most prominent users of this API will be pip and build, both of which want to have such low-level control. :)