import click class MyCLI(click.MultiCommand): def list_commands(self, ctx): print('A') assert ctx.obj is not None, 'A' def get_command(self, ctx, name): print('B') assert ctx.obj is not None, 'B' @click.command(cls=MyCLI) @click.pass_context def cli(ctx): ctx.obj = 123 if __name__ == '__main__': cli() cmd = self.get_command(ctx, cmd_name) File "cli4.py", line 11, in get_command assert ctx.obj is not None, 'B' AssertionError: B