Skip to content

Add a @finalize decorator for functions to collect/operate on subroutine results. #140

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
bckohan opened this issue Nov 15, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@bckohan
Copy link
Member

bckohan commented Nov 15, 2024

For symmetry with @initialize

This behavior is mostly currently achievable with result_callback that you can pass to Typer.callback or Typer.group. There are two deficiencies:

  1. The command must be compound. There's no way to finalize a singular command. This makes sense, but it'd be nice if the interface uniformly worked.
  2. result_callback is not class aware, so you have to jump through extra hoops to get access to self - namely you need to return it from your subcommands.
@bckohan bckohan added the enhancement New feature or request label Nov 15, 2024
@bckohan bckohan self-assigned this Nov 15, 2024
@bckohan
Copy link
Member Author

bckohan commented Nov 15, 2024

You can currently do something like this, but its very awkward:

class Command(TyperCommand):

    def finalize(self, **kwargs):
        pass

    @initialize(result_callback=finalize)
    def init(self):
        pass

    @command():
    def cmd(self):
        return self

Or this for chainable commands:

class Command(TyperCommand, chain=True):

    @staticmethod
    def finalize(result, **kwargs):
        self = result[0]

    @initialize(result_callback=finalize)
    def init(self):
        pass

    @command():
    def cmd1(self):
        return self

    @command():
    def cmd2(self):
        return self

Would prefer to be able to do something like:

class Command(TyperCommand, chain=True|False):

    @finalize()
    def finalize(self, results):
        pass

    @initialize()
    def init(self):
        pass

    @command():
    def cmd(self):
        return self

bckohan added a commit that referenced this issue Nov 19, 2024
bckohan added a commit that referenced this issue Nov 19, 2024
@bckohan bckohan added this to the Version 3.0 milestone Nov 20, 2024
@bckohan bckohan changed the title Add a @finalize decorator for functions to be run on the result of all subroutines. Add a @finalize decorator for functions to collect/operate on subroutine results. Nov 20, 2024
bckohan added a commit that referenced this issue Nov 21, 2024
bckohan added a commit that referenced this issue Nov 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant