Skip to main content

Yet another dataclass argparse

Project description

Yada

PyPI Python GitHub Issues Contributions welcome License

Yada (Yet Another Dataclass Argument Parser!) is a library to automatically generate argparse.ArgumentParser given data classes. Compared to some available options such as: Huggingface's HfArgumentParser, argparse_dataclass, and tap, it offers the following benefits:

  1. Static Type Checking
  2. Nested data classes and complex types
  3. Easy to extend and customize the parser
  4. Generate command line arguments given the data classes.

Installation

Install via PyPI (requires Python 3.8+):

pip install t2-yada

How to use

Yada's parser can be constructed from data classes. It relies on fieds' annotated types to construct correct argument parsers.

import yada
from dataclasses import dataclass
from typing import *

@dataclass
class CityArgs:
    city: Literal["LA", "NY"]


@dataclass
class NestedArgs:
    name: str
    nested: CityArgs

parser = yada.YadaParser(NestedArgs)
args = parser.parse_args()  # or use parser.parse_known_args() -- the two functions are similar to argparse.parse_args or argparse.parse_known_args

Note: YadaParser is annotated as a generic type: YadaParser[C, R] where C denotes the classes, and R denotes the instance of the classes created from the arguments. Therefore, in the above example, C is inferred as NestedArgs, but R is unknown, hence the type of args variable is unknown. To overcome this typing limitation, Yada provides several options for up to 10 data classes (yada.Parser1, yada.Parser2, ...). Below is two examples:

parser = yada.Parser1(NestedArgs)
args = parser.parse_args()  # <-- args now has type NestedArgs
parser = yada.Parser2((NestedArgs, CityArgs))
args = parser.parse_args()  # <-- args now has type Tuple[NestedArgs, CityArgs]

Note: we recommend to use one of the specific parsers yada.Parser<N> instead of the generic yada.YadaParser if possible as they provide strong typing support.

Configuring Yada

Add help message

Yada reads the help message from the key property of dataclasses.Field.metadata

import yada
from dataclasses import dataclass, field
from typing import *

@dataclass
class CityArgs:
    city: Literal["LA", "NY"] = field(metadata={"help": "city's which you want to get the timezone"})

parser = yada.Parser1(CityArgs)

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

t2_yada-1.2.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

t2_yada-1.2.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file t2_yada-1.2.1.tar.gz.

File metadata

  • Download URL: t2_yada-1.2.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for t2_yada-1.2.1.tar.gz
Algorithm Hash digest
SHA256 3d30f6109feeed8579c41205fb8b63d3c0db4175733ea6e45f19befe0e280a77
MD5 e9117d118e57f9a074c7d05f8e12c7ae
BLAKE2b-256 70efbb46b2fb1b34a75a4ead01248c6dae5c8b18ef328b91c3eae089593e4899

See more details on using hashes here.

File details

Details for the file t2_yada-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: t2_yada-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for t2_yada-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83a756e40dcdbec1cf8c226b8310f3c1314142e4703807766a00dc28ab15bd43
MD5 a492abee20669196b01b14d06e3497e8
BLAKE2b-256 c66eb4634d1d3fd0820eb0501bb31b48b25a147c4717301d4f0a23a43e3b25ee

See more details on using hashes here.

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