Mypyc Runtime Library

0.8.1 · active · verified Sat Mar 28

librt is the Mypyc runtime library, providing efficient C implementations of various Python standard library classes and functions. Mypyc leverages this library to produce faster C extensions when compiling Python code. It is primarily an internal dependency for `mypyc` and `mypy` (since version 1.19.0 for fixed-format cache serialization). The current version is 0.8.1, with development occurring in the main `mypy` repository and releases synced to the `librt` PyPI package.

Warnings

Install

Imports

Quickstart

librt is a runtime library for code compiled with `mypyc`. Therefore, its 'quickstart' involves demonstrating how to use `mypyc` to compile Python code. The compiled code will then depend on `librt` at runtime. The example shows a simple Python function that `mypyc` could compile, implicitly leveraging `librt`'s optimizations.

# librt is implicitly used by mypyc-compiled modules.
# A typical quickstart involves using mypyc, which then utilizes librt.
# First, ensure librt (and mypy, which includes mypyc) are installed.
# pip install mypy mypyc

# example_module.py
# This module will be compiled by mypyc, which relies on librt at runtime.

def factorial(n: int) -> int:
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

if __name__ == '__main__':
    # In a real scenario, this would be compiled and imported as a C extension.
    # For illustration, let's just run it as normal Python.
    # To compile: mypyc example_module.py
    # This creates a C extension (e.g., example_module.cpython-XYZ.so) that depends on librt.
    print(f'Factorial of 5: {factorial(5)}')

view raw JSON →