Skip to main content

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'

Release files for typehints-cleo2 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typehints-cleo2 1.0.0
File Size Uploaded
typehints_cleo2-1.0.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typehints-cleo2 1.0.0
File Interpreter ABI Platform
typehints_cleo2-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size:8.6 kB

Release files / typehints_cleo2-1.0.0.tar.gz

Download URL typehints_cleo2-1.0.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b6e9c4b0acd9b67facf545c4c5cf665b896a6c4c5a968cbdcce753113f35fabf
BLAKE2b-256 checksum
How to use checksums
fb2c56dc0d9c0d64b2c83ea88ea6d757e34a2d7a4dfbc765c44b1b454b47d962
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.4

Release files / typehints_cleo2-1.0.0-py3-none-any.whl

Download URL typehints_cleo2-1.0.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
467d5190ac63226c54347dff710c8f72b71760a3e34bd698b8545d89fef66b03
BLAKE2b-256 checksum
How to use checksums
f27fe9bfbb0a47917b3c29fb7bae46fb05102891758237b7e688ef1f916d761e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page