Skip to content

It should be easier to extend commands provided by spin #242

Closed

Description

@ngoldbaum

NumPy defines a spin build command, which has fallen out of sync from the upstream meson.build command it uses. Reading the click docs, it seems like the pattern NumPy is using with ctx.forward is frowned upon.

I think it would be better if spin provided factories for click commands to make it easier for projects to extend upstream commands without having to effectively re-implement their API.

See e.g. https://stackoverflow.com/questions/55584012/python-click-dependent-options-on-another-option.

Activity

  1. stefanv commented on Nov 8, 2024

    @stefanv
    Member

    @ngoldbaum Could you expand on the suggestion here? I read the S/O post, but I'm not sure what you mean to highlight there.

    We've modified all spin commands to use keyword-only arguments, so you should be "forward compatible" with newer versions of commands.

  2. ngoldbaum commented on Nov 12, 2024

    @ngoldbaum
    ContributorAuthor

    NumPy would just use the upstream spin.cmds.meson.build command provided by spin, but it needs to add one keyword argument to build, with_scipy_openblas. So it defines a new command with the signature

    def build(ctx, meson_args, with_scipy_openblas, jobs=None, clean=False, verbose=False, quiet=False, *args, **kwargs)
    

    This is missing both build_dir and gcov as arguments that are in the upstream command. You might think that because NumPy's spin command accepts **kwargs, if someone passed in gcov, then click or spin would just forward the command, but that's not how it works:

    goldbaum at Nathans-MBP in ~/Documents/numpy on fix-generic-fancy-index-cast!
    ± spin build --clean --gcov
    Usage: spin build [OPTIONS] [MESON_ARGS]...
    Try 'spin build --help' for help.
    
    Error: No such option: --gcov
    

    If you look at the docstring for numpy's build command, you'll also see that it's missing a bunch of stuff that's in spin's spin.cmds.meson.build command.

    So to update NumPy's spin build command to match the features provided by upstream, someone is going to need to manually update the build function and docstring in NumPy. Or more likely forget to update it, as has happened, particularly to the docstring.

    I also found that I needed to patch NumPy's spin config in order for NumPy to benefit from #241, because without an explicit --prefix override in NumPy, click doesn't pass --prefix to spin's command.

    Ultimately, as I understand it, this is because we use ctx.forward, which the spin docs advise against ("this pattern is generally discouraged"): https://click.palletsprojects.com/en/stable/advanced/#invoking-other-commands

    IMO it would be better if spin itself could provide a command factory that accepts some sort of structure that defines a keyword argument and associated docstring, and then spin builds the command function and corresponding docstring as is needed. That would also allow better customization, for example if a built-in argument like --gcov doesn't make sense for some project: a command factory would allow a project to ask the factory to not include --gcov.

  3. changed the title [-]It should be easier to subclass commands provided by spin[/-] [+]It should be easier to extend commands provided by spin[/+] on Nov 12, 2024
  4. stefanv commented on Nov 12, 2024

    @stefanv
    Member

    Since you may want to do pre and postprocessing, before running the existing command, you will likely still need ctx.forward. The docs say you should be careful when using it, but we use it in the correct setting.

    So, perhaps the need here is to be able to forward flags, without knowing about them at the time of authorin? I think that can be accomplished by something like:

    @spin.extend_cmd(spin.cmds.meson.build)
    @click.option(
        "-n", "--numpy",
        help="NumPy-specific command",
        type=int
    )
    def build(*args, numpy=None, **kwargs):
       ... pre-processing ...
       ... may use kwargs and args as necessary; kwargs can also be made explicit for use,
       ... e.g. def build(*args, numpy=None, jobs=None, **kwargs)
    
       ctx.forward(spin.cmds.meson.build, *args, **kwargs)
    
       ... post-processing

    Would that cover your use-cases? This way, you can still swallow the gcov flag if you want.

  5. ngoldbaum commented on Nov 12, 2024

    @ngoldbaum
    ContributorAuthor

    Yeah I think that would be perfect! I'd love to delete the docstring in numpy's build command and I think this way we'd get updates "for free" whenever spin updates.

  6. added 2 commits that reference this issue on Nov 13, 2024
    9aac713
    6fdacd3
  7. added a commit that references this issue on Nov 17, 2024
    5e91741
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions