typer icon indicating copy to clipboard operation
typer copied to clipboard

Typer.command.cls swallows Typer.cls.command_class

Open boonhapus opened this issue 2 years ago • 0 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

class MyCustomCommand(click.Command):
    def get_help(self, ctx) -> str:
        return 'Nope!'

class MyCustomGroup(click.Group):
    command_class = MyCustomCommand
    group_class = type


app = typer.Typer(name='app', cls=MyCustomGroup)

@app.command()
def foo(help='just some filler text'):
    pass

if __name__ == '__main__':
    app()
    # Expected: app foo -h  ==> 'Nope!'
    # Actual: app foo -h ==> full help text

Description

Since click>=8.0, click.Group may define a class member to designate which Command to default to.

class click.Group(name=None, commands=None, attrs) ... command_class: Optional[Type[click.core.Command]] = None If set, this is used by the group’s command() decorator as the default Command class. This is useful to make all subcommands use a custom command class.

New in version 8.0.

It appears that this gets swallowed by the current version of Typer.

Operating System

Windows

Operating System Details

No response

Typer Version

0.4.0

Python Version

Python 3.8.5

Additional Context

https://github.com/tiangolo/typer/blob/d5bc56177edcb14f159743e5eaf52cd448e622dc/typer/main.py#L145-L146

should likely be...

        if cls is None:
            cls = getattr(self.info.cls, 'command_class', TyperCommand)

boonhapus avatar Apr 03 '22 04:04 boonhapus