Skip to main content

argparse with type annotations

Project description


Build Status Version

Strong type args.

This project is inspired by TeXitoi/structopt.


From pypi

pip install typed-args

If you want to use it on python 3.5 and 3.6 please install dataclasses:

pip install dataclasses


import argparse
from dataclasses import dataclass

from typed_args import TypedArgs, add_argument

parser = argparse.ArgumentParser(prog='PROG')
    'data', metavar='DIR', type=str,
    help='path to dataset'
    '-a', '--arch', metavar='ARCH', default='resnet18', type=str,
    help='model architecture (default: resnet18)'
    '-j', '--workers', default=4, metavar='N', type=int, dest='num_workers',
    help='number of data loading workers (default: 4)'

class Args(TypedArgs):
    data: str = add_argument(metavar='DIR', help='path to dataset')
    arch: str = add_argument('-a', '--arch', metavar='ARCH', default='resnet18',
                             help='model architecture (default: resnet18)')
    num_workers: int = add_argument('-j', '--workers', default=4, metavar='N',
                                    help='number of data loading workers (default: 4)')

    def parser_factory(self):
        return argparse.ArgumentParser('PROG')

def test_args():
    data = '/path/to/dataset'
    arch = 'resnet50'
    num_workers = 8

    argv = f'{data} -a {arch} --workers {num_workers}'.split()

    from_args = parse_args, from_known_args = parse_known_args
    typed_args = Args.from_args(argv)
    args = parser.parse_args(argv)

    assert args.arch == typed_args.arch
    assert ==
    assert args.num_workers == typed_args.num_workers

if __name__ == "__main__":


Currently, we don't support add_group and sub parser.

Project details

Download files

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

Files for typed-args, version 0.4.2
Filename, size File type Python version Upload date Hashes
Filename, size typed_args-0.4.2-py3-none-any.whl (5.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size typed-args-0.4.2.tar.gz (6.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page