Skip to content

Using typed-settings with multiple click commands and pluggy.

Hi Stefan,

typed-settings looks fantastic and I am wondering whether it fits to my package. I have two questions.

  1. The package has a cli based on click with multiple commands where each command has its own set of options which are extending the options of the click.group.

    I saw that it is possible to pass settings from the group to the commands (https://gitlab.com/sscherfke/typed-settings/-/blob/main/tests/test_click.py#L698). But, is it possible to define settings for the group and the command which will be merged or at least passed as two objects to the command?

    Here is a very minimal example which I used for toying around.

    import click
    import typed_settings as ts
    
    
    @ts.settings
    class OverallSettings:
        verbose: int = ts.option(default=0, help="verbosity")
    
    
    @click.group()
    @ts.click_options(OverallSettings, "myapp")
    def main(settings):
        print(settings)
    
    
    @ts.settings
    class FirstSettings:
        string: str
    
    
    @main.command()
    @ts.pass_settings
    @ts.click_options(FirstSettings, "myapp")
    def first_command(settings):
        print(settings)
    
    
    if __name__ == '__main__':
        main()

    Executing the following does not work

    $ python .\click_.py first-command --string hello
    OverallSettings(verbose=0)
    Traceback (most recent call last):
      File "C:\Users\TobiasR\git\pytask\click_.py", line 29, in <module>
        main()
      ...
      File "C:\tools\miniconda3\envs\pytask\lib\site-packages\typed_settings\click_utils.py", line 354, in cb
        _set_path(settings, path, value)
      File "C:\tools\miniconda3\envs\pytask\lib\site-packages\typed_settings\_dict_utils.py", line 79, in _set_path
        dct[key] = val
    TypeError: 'OverallSettings' object does not support item assignment
  2. The package uses pluggy for loading plugins which are allowed to extend the cli by adding commands and options to commands. I saw the stub for an example with pytest so you might have thought about how to dynamically generate a settings class. My simple take on it would be to gather dictionaries of options for the group and the commands and call Settings = ts.settings()(attrs.make_class("Settings", options)).

Thanks for your help in advance!