Skip to main content

Type-Hinted, Type-Enforced classes for cleo 2.x

Project description

Typehinted Classes for Cleo 2.x

The authors of the excellent Cleo library have declared a plan to overhaul Typing in Cleo 3.x, amongst the many other improvements listed in their Writeup.

In the meantime, some of the projects that I work on make use of Cleo 2.x, whose Command.option() and Command.argument() methods are annotated with t.Any and don't enforce returned types.

This library adds type-hinted, type-enforced wrappers for these methods as a holdover until Cleo 3.x is ready for use.

If Cleo 3.x is out by the time you're reading this: You should probably be using that instead!

PyPi

https://pypi.org/project/typehints-cleo2/

Installation

With Poetry:

poetry add typehints_cleo2

With pip:

pip install typehints_cleo2

Example Usage

Modified example from Cleo's README.md

from cleo.helpers import argument, option

# Instead of `from cleo.commands.command import Command`
from typehints_cleo2 import TypeHintedCommand


# use TypeHintedCommand as a drop-in replacement for cleo's Command class
class GreetCommand(TypeHintedCommand):

    ...

    def handle(self):
        name: str = self.argument_str("name")  # Type-Hinted argument_str guarantees a string return
        ...

    ...

Optional Arguments & Options

class TestCommand(TypeHintedCommand):

    name = "test"
    description = "Test Command"
    arguments = [
        argument("test_argument", description="A test argument", optional=False),
        argument("test_argument2", description="A test argument", optional=True),
    ]
    options = [option("test_option", description="A test option")]

    ...

    def handle(self):
        arg1: str = self.argument_str("test_argument")
        arg2: str | None = self.argument_str_optional("test_argument2")
        opt1: str | bool = self.option_str("test_option")
        self.line(f"Test Command! -- Arg1: '{arg1}' -- Arg2: '{arg2}' -- Opt1: '{opt1}'")

    ...
poetry run python -m my_application test a_test_val

Output: Test Command! -- Arg1: 'a_test_val' -- Arg2: 'None' -- Opt1: 'False'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typehints_cleo2-1.0.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

typehints_cleo2-1.0.0-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page