Skip to main content

Stay

Simple Typed Argparse, Yes

ruff-action Tests

Add typing to your argparse CLIs without having to modify your current parsers.

Example:

from stay import Stayspace, StayParser

class ArgSpace(Stayspace):
    foo: int
    bar: str

parser = StayParser(namespace_cls=ArgSpace)

parser.add_argument("--foo", type=int)
parser.add_argument("--bar", type=float)

args = parser.parse_args()

reveal_type(args.foo) # int
reveal_type(args.bar) # float
reveal_type(args.invalid) # Type Error

Install

To install from PyPi:

pip install -U py-stay

To install the most recent dev version

git clone https://github.com/drkspace/stay
cd stay
pip install .

Why not other typed argparse libraries?

There are two other typed argparse libraries, tap and typed-argparse. Their main deficiencies are that they can not replicate all of argparse's features, like argument groups and mutually exclusive groups. Stay, on the other hand, will always support 100% of argparse's features as the parsing class, StayParser, is a subclass of argparse.ArgumentParser.

However, Stay cannot generate a parser from a Namespace or other data structure. If you require that functionality, please use tap or typed-argparse. No Hard feelings.

Examples

Here are some examples. See examples/ for the full set.

Simple Example

#!/usr/bin/env python
# ruff: noqa: T201
from stay import StayParser, Stayspace


class CLIInput(Stayspace):
    name: str
    age: int

def main() -> None:

    parser = StayParser(namespace_cls=CLIInput)

    parser.add_argument("--name", type=str)
    parser.add_argument("--age", type=int)

    args = parser.parse_args()

    print(f"Hello {args.name}. You are {args.age} years old")

if __name__ == "__main__":
    main()

Mutually exclusive groups

Unfortunately, python's typing system is not robust enough to have a nice way to have multiple/an arbitrary number types as the namespace_cls input. You have to fake it with combining your Stayspaces into one class and using that as input to namespace_cls. You are still able to type the generic as the union of your types and then you can use a TypeIs or TypeGuard (depending on your python version) to pick out the type.

#!/usr/bin/env python
# ruff: noqa: T201

from typing_extensions import TypeIs

from stay import StayParser, Stayspace


class CLIInput1(Stayspace):
    foo: int

class CLIInput2(Stayspace):
    bar: str

class CLICombined(CLIInput1, CLIInput2):
    ...

def is_inp_1(inp: CLIInput1|CLIInput2) -> TypeIs[CLIInput1]:
    return "foo" in inp

def main() -> None:

    parser = StayParser[CLIInput1|CLIInput2](namespace_cls=CLICombined)

    meg = parser.add_mutually_exclusive_group(required=True)
    meg.add_argument("--foo", type=int)
    meg.add_argument("--bar", type=str)

    args = parser.parse_args()

    if is_inp_1(args):
        print(f"You inputted foo with a value of {args.foo}")
    else:
        print(f"You inputted bar with a value of {args.bar}")

if __name__ == "__main__":
    main()

Subparser

Subparsers act just like (mutually exclusive) groups. In order to match with how argparse behaves, the results of a subparser will be "flat". You can use TypeIs/TypeGuard to narrow a Stayspace to the one for a specific subparser.

Metadata

Release files for py-stay 0.2.1

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

Source distribution (sdist)

Source distribution for py-stay 0.2.1
File Size Uploaded Provenance Actions
py_stay-0.2.1.tar.gz 5.9 kB With provenance Details for py_stay-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-stay 0.2.1
File Interpreter ABI Platform Actions
py_stay-0.2.1-py3-none-any.whl Python 3 none any Details for py_stay-0.2.1-py3-none-any.whl

Total release size: 11.3 kB

Release files / py_stay-0.2.1.tar.gz

Download URL py_stay-0.2.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dac8a856d5c157a84f2632001de56055e055225932bb4484dbab6b6060a8eec0
BLAKE2b-256 checksum
How to use checksums
bdb4b6bc20cab4178e65cd04a6a42457b0dbb755ed20e8fa0e0dd33a721497db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 19, 2025.

Transparency log

Release files / py_stay-0.2.1-py3-none-any.whl

Download URL py_stay-0.2.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
adcae8973ead5faf8b750cf4e7fab09510c2a4272568710d1946745e84e3521c
BLAKE2b-256 checksum
How to use checksums
390a6fc0384b61833dac9c0d73357f2acafd54bf8d6fd003f792a956ca4ab516
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 19, 2025.

Transparency log

Security for py-stay 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for py_stay-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 19, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files Full provenance

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