$ account Instagram
MonkeyType is a Python library and CLI tool that collects runtime types of function arguments and return values using Python's sys.setprofile hook. It stores these call traces (default SQLite) and can generate PEP 484 stub files or directly apply type annotations to source code via libcst. The architecture separates tracing (CallTracer), storage (CallTraceStore interface with SQLite implementation), stub generation (FunctionDefinition, ModuleStub), and application (libcst-based code transformation). It provides a configurable plugin system (Config/DefaultConfig) for customizing trace storage, code filtering, sampling, and type rewriting. The type rewriting subsystem includes a GenericTypeRewriter visitor pattern and several built-in rewriters (RemoveEmptyContainers, RewriteConfigDict, RewriteLargeUnion, RewriteMostSpecificCommonBase, RewriteGenerator) that refine raw union types into more idiomatic annotations. The project is well-tested with a comprehensive test suite and has been used in production at Instagram.
LibCST is a Concrete Syntax Tree (CST) parser, analyzer, and transformation framework for Python 3.9–3.15, developed by Meta (Instagram). Unlike an AST, the CST preserves every byte of the original source—including whitespace, comments, parentheses, and formatting—enabling lossless round-trips and precise automated refactoring. The library provides a rich node hierarchy (expressions, statements, operators, whitespace), a full parser with both a pure-Python fallback and a high-performance Rust native implementation via PyO3, and a visitor/transformer system with typed visit/leave methods. It also includes a reusable metadata system with providers for source positions, scopes, qualified names, expression context, and type inference (via Pyre), as well as a pattern-matching DSL (“matchers”) for declarative node queries and a CLI tool for running codemods. The codemod framework is a major feature: it offers base classes for writing automated code transformations (VisitorBasedCodemodCommand, MagicArgsCodemodCommand), bundled commands for common refactoring tasks (removing unused imports, converting type comments, migrating format strings to f-strings, renaming symbols, adding trailing commas, etc.), and a testing harness (CodemodTest). The library is designed for tooling such as linters, formatters, and large-scale migration scripts—and is used internally at Meta for exactly these purposes. It is distributed as a pip package with pre-built wheels, supports multiple operating systems and Python free-threaded builds, and is actively maintained with a mature CI pipeline. Architecturally, LibCST is a library, not a standalone application, though it exposes a CLI via ‘python -m libcst.tool’. It has no API endpoints, databases, or data pipelines. The codebase is well-structured, heavily tested (93%+ coverage), and includes code generation for typed visitor and matcher classes from node definitions. While the dual parser (Python vs. Rust) introduces some maintenance complexity, the overall craftsmanship is high—clean abstractions, immutable nodes, dependency-injected metadata, and a robust visitor pattern make this a reference implementation for CST processing.