• Stars
    star
    462
  • Rank 94,832 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CLI interfaces & config objects, from types

tyro logo

Documentation   •   pip install tyro

build mypy lint codecov codecov


tyro is a tool for building command-line interfaces and configuration objects in Python.

Our core interface, tyro.cli(), generates command-line interfaces from type-annotated callables.


Brief walkthrough

To summarize how tyro.cli() can be used, let's consider a script based on argparse. We define two inputs and print the sum:

"""Sum two numbers from argparse."""
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--a", type=int, required=True)
parser.add_argument("--b", type=int, default=3)
args = parser.parse_args()

total = args.a + args.b

print(total)

This pattern is dramatically cleaner than manually parsing sys.argv, but has several issues: it lacks type checking and IDE support (consider: jumping to definitions, finding references, docstrings, refactoring and renaming tools), requires a significant amount of parsing-specific boilerplate, and becomes difficult to manage for larger projects.

The basic goal of tyro.cli() is to provide a wrapper for argparse that solves these issues.

(1) Command-line interfaces from functions.

We can write the same script as above using tyro.cli():

"""Sum two numbers by calling a function with tyro."""
import tyro

def add(a: int, b: int = 3) -> int:
    return a + b

# Populate the inputs of add(), call it, then return the output.
total = tyro.cli(add)

print(total)

Or, more succinctly:

"""Sum two numbers by calling a function with tyro."""
import tyro

def add(a: int, b: int = 3) -> None:
    print(a + b)

tyro.cli(add)  # Returns `None`.

(2) Command-line interfaces from config objects.

A class in Python can be treated as a function that returns an instance. This makes it easy to populate explicit configuration structures:

"""Sum two numbers by instantiating a dataclass with tyro."""
from dataclasses import dataclass

import tyro

@dataclass
class Args:
    a: int
    b: int = 3

args = tyro.cli(Args)
print(args.a + args.b)

Unlike directly using argparse, both the function-based and dataclass-based approaches are compatible with static analysis; tab completion and type checking will work out-of-the-box.

(3) Additional features.

These examples only scratch the surface of what's possible. tyro aims to support all reasonable type annotations, which can help us define things like hierachical structures, enums, unions, variable-length inputs, and subcommands. See documentation for examples.

In the wild

tyro is still a new library, but being stress tested in several projects!

More Repositories

1

jaxlie

Rigid transforms + Lie groups in JAX
Python
220
star
2

jaxls

Sparse nonlinear least squares for JAX
Python
168
star
3

tilted

Canonical Factors for Hybrid Neural Fields @ ICCV 2023
Python
101
star
4

dfgo

Differentiable Factor Graph Optimization for Learning Smoothers @ IROS 2021
Python
78
star
5

jax_dataclasses

Pytrees + dataclasses ❤️
Python
59
star
6

tensorf-jax

Unofficial implementation of Tensorial Radiance Fields (Chen & Xu ‘22)
Python
38
star
7

pips-jax

JAX port of Persistent Independent Particles
Python
36
star
8

multimodalfilter

Jupyter Notebook
27
star
9

jelly_mechanical

Solidworks files for ME135 quadruped project
21
star
10

jax-ekf

Generic EKF, with support for non-Euclidean manifolds
Python
20
star
11

isort.vim

Async isort plugin for Vim + Neovim
Vim Script
20
star
12

minGPT-flax

GPT implementation in Flax
Python
18
star
13

stl_web_viewer2

Javascript utility for embedding 3D models
JavaScript
12
star
14

sparky_firmware

Firmware for RHex-style robot
C++
9
star
15

brushless_driver

BLDC motor driver design; 48V, 3.5A continuous / 6.5A peak
HTML
7
star
16

dotfilesp

Configuration files & setup scripts
Vim Script
6
star
17

fannypack

Tools for training PyTorch models
Python
5
star
18

jax_cuda_boilerplate

Toy package for custom CUDA kernels + JAX
Python
5
star
19

fifteen

Python
4
star
20

as5047d_breakout

Absolute encoder breakout PCB for prototyping
HTML
3
star
21

drawing_machine_firmware

Firmware + G-code parser for plotter
C
3
star
22

jelly2_mechanical

2
star
23

as5048b_breakout

daisy chainable absolute magnetic encoder
HTML
2
star
24

stl_web_viewer

deprecated in favor of https://github.com/brentyi/stl_web_viewer2
JavaScript
2
star
25

jaxfg

new version: https://github.com/brentyi/jaxls
Python
2
star
26

crossmodal_filtering1

obsolete; see https://github.com/stanford-iprl-lab/torchfilter
Jupyter Notebook
1
star
27

digikey_parser

nodejs package for scraping part information from digikey barcodes
JavaScript
1
star
28

marshmello_web

HTML
1
star
29

opthex

Python
1
star
30

170_proj

Python
1
star
31

keyboard

Mechanical keyboard PCB designs
1
star
32

jax-ldr

(unofficial) Closed-Loop Data Transcription to an LDR via Minimaxing Rate Reduction
Jupyter Notebook
1
star