Typing Stubs for nanoid

2.0.0.20260408 · active · verified Thu Apr 16

types-nanoid is a PEP 561 type stub package that provides static type annotations for the `nanoid` library. It allows type checkers such as MyPy, Pyright, and PyCharm to perform static analysis and type inference on code that uses `nanoid`, ensuring type safety without impacting runtime behavior. This version aims to provide accurate annotations for `nanoid==2.0.0`. It is actively maintained as part of the `typeshed` project, with updates typically correlating to upstream `nanoid` releases or general typeshed improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install both the `nanoid` library and its corresponding type stubs, `types-nanoid`. It then shows how to use `nanoid.generate` for secure IDs and `nanoid.non_secure_generate` for performance-critical, non-security-sensitive cases, all while leveraging the type hints provided by `types-nanoid` for static analysis.

import os
from nanoid import generate, non_secure_generate

def create_ids() -> None:
    # Generate a cryptographically strong, URL-friendly unique ID
    secure_id: str = generate()
    print(f"Secure ID: {secure_id}")

    # Generate a non-secure ID (for non-security-critical applications)
    non_secure_id: str = non_secure_generate(size=10)
    print(f"Non-Secure ID (10 chars): {non_secure_id}")

    # Generate with a custom alphabet and size
    custom_alphabet_id: str = generate('1234567890abcdef', 8)
    print(f"Custom Alphabet ID: {custom_alphabet_id}")


if __name__ == "__main__":
    create_ids()

view raw JSON →