Skip to main content

Typarse

This is a small project born out of my frustration with simple argument parsing in Python.

Not only do I have to instantiate some object whose name I can never remember, then I get way too many function parameters to get them right... It's a mess. And I don't even need half the features.

So this is an attempt at streamlining this process while simultaneously promoting some better type safety, by using the magic of Python type hints!

Really all the magic here is happening in the BaseParser class. You just need to subclass it, add a few typed parameters, perhaps with some extra information in dictionaries... and you're done! For examples, just look at, well, examples.

Supported types

First of all, all the basic types supported by argparse are also supported here. Things like: str, int, float. bools are automatically interpreted as flags, False by default. Each type can be wrapped in a List to support reading them like --list 1 2 3 4. Each type can also be made Optional which makes it, well, optinal.

Simple illustrative example

from typarse import BaseParser
from typing import List


class Parser(BaseParser):
    nums: List[int]
    square: bool
    default: int = 0

    _abbrev = {
        "nums": "n",
        "square": "s",
        "default": "d"
    }

    _help = {
        "nums": "List of numbers to sum",
        "square": "Whether the result should be squared",
        "default": "Initial value, added to the sum"
    }



args = Parser()

print(args.default + sum(args.nums) ** (1+args.square))

Functionality 2: Config

In the spirit of the library, I added a config management component. Similarly to parsing, you can define a config using type annotations on a class -- also in a nested manner.

Example:

from typarse import BaseConfig
from typing import List 

class Config(BaseConfig):
    rate: float = 0.1
    amount: float = 0.01
    limit: float = 1.
    class PolicyConfig(BaseConfig):
        layers: List[int] = [32, 32, 32]
        activation: str = "relu"

config = {
    "rate": 0.5,
    "PolicyConfig": {
        "layers": [64, 64]
    }
}

Installation

It's on PyPi so install it with pip install typarse, poetry add typarse or whatever you want. If you want to install it from the source, it's pure Python so I'm sure you can figure it out.

Metadata

Release files for typarse 4.2.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 typarse 4.2.0
File Size Uploaded Provenance Actions
typarse-4.2.0.tar.gz 19.4 kB Without provenance Details for typarse-4.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typarse 4.2.0
File Interpreter ABI Platform Actions
typarse-4.2.0-py3-none-any.whl Python 3 none any Details for typarse-4.2.0-py3-none-any.whl

Total release size: 36.2 kB

Release files / typarse-4.2.0.tar.gz

Download URL typarse-4.2.0.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c59040c1fce8d82263fa8dfef5ce726fbf7e87a87978d11faf1db97bc2cbbcf7
BLAKE2b-256 checksum
How to use checksums
bfe7d40f6eee8c5a6f8a7302fdd292b6548b3e4f1bf52a922ed1436b7acb7904
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.2

Release files / typarse-4.2.0-py3-none-any.whl

Download URL typarse-4.2.0-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e451201c52605b96e50cd9f9a0f8c2056f12d6e0b4c6b72cfd60ba3c38574b7e
BLAKE2b-256 checksum
How to use checksums
e3613a4cd06467ecff7152a1d3c6488d9917e98fe31348aeef8f0030571ee85c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.2

Security for typarse 4.2.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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