{"id":8740,"library":"types-nanoid","title":"Typing Stubs for nanoid","description":"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.","status":"active","version":"2.0.0.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","nanoid","type-checking","typeshed"],"install":[{"cmd":"pip install types-nanoid","lang":"bash","label":"Install only type stubs"},{"cmd":"pip install nanoid types-nanoid","lang":"bash","label":"Install runtime and type stubs"}],"dependencies":[{"reason":"Provides runtime implementation for which these stubs offer type hints.","package":"nanoid","optional":false}],"imports":[{"symbol":"generate","correct":"from nanoid import generate"},{"symbol":"non_secure_generate","correct":"from nanoid import non_secure_generate"}],"quickstart":{"code":"import os\nfrom nanoid import generate, non_secure_generate\n\ndef create_ids() -> None:\n    # Generate a cryptographically strong, URL-friendly unique ID\n    secure_id: str = generate()\n    print(f\"Secure ID: {secure_id}\")\n\n    # Generate a non-secure ID (for non-security-critical applications)\n    non_secure_id: str = non_secure_generate(size=10)\n    print(f\"Non-Secure ID (10 chars): {non_secure_id}\")\n\n    # Generate with a custom alphabet and size\n    custom_alphabet_id: str = generate('1234567890abcdef', 8)\n    print(f\"Custom Alphabet ID: {custom_alphabet_id}\")\n\n\nif __name__ == \"__main__\":\n    create_ids()","lang":"python","description":"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."},"warnings":[{"fix":"Ensure `nanoid` and `types-nanoid` are installed with compatible versions. For example, if `types-nanoid` is for `nanoid==2.0.0`, install both as `pip install \"nanoid==2.0.0\" \"types-nanoid==2.0.0.*\"`. Consult the `typeshed` repository for specific version compatibility notes.","message":"Type stub versions should ideally align with the major version of the runtime library they type. A mismatch between `types-nanoid` and the installed `nanoid` version can lead to incorrect type-checking results or errors.","severity":"breaking","affected_versions":"All versions of `types-nanoid` when `nanoid` is a different major version."},{"fix":"For Python projects, always install `types-nanoid` explicitly for type checking alongside the `nanoid` runtime library.","message":"There can be confusion with the JavaScript `@types/nanoid` package, which is deprecated because the JS `nanoid` library now includes its own types. In Python, however, `types-nanoid` is still necessary for type checking as the `nanoid` Python package does not typically bundle `py.typed` files for static analysis.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For any security-critical ID generation (e.g., session tokens, API keys), always use `nanoid.generate()`. Reserve `nanoid.non_secure_generate()` only for cases where performance is prioritized over cryptographic strength and security is not a concern (e.g., temporary internal identifiers).","message":"The `nanoid` library offers `non_secure_generate` which uses `random.random` internally. This function is explicitly \"not recommended for generating tokens or secrets\" due to its weaker cryptographic strength compared to `generate()`. Using it in security-sensitive contexts is a footgun.","severity":"gotcha","affected_versions":"All versions of `nanoid` and `types-nanoid`."}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure both `nanoid` and `types-nanoid` are installed in your environment (`pip install nanoid types-nanoid`) and your type checker (e.g., MyPy) is correctly configured to find them. Verify version compatibility between `nanoid` and `types-nanoid`.","cause":"The type checker cannot find the `nanoid` runtime library or its associated stubs (`types-nanoid`), or there's a version mismatch.","error":"Module 'nanoid' has no 'generate' member (no-member)"},{"fix":"Install the `nanoid` runtime library: `pip install nanoid`.","cause":"The `nanoid` runtime library is not installed. `types-nanoid` only provides type hints and does not include the actual runnable code.","error":"ModuleNotFoundError: No module named 'nanoid'"},{"fix":"Verify that `nanoid` and `types-nanoid` versions are compatible. Upgrade both to their latest compatible versions. If the issue persists, explicitly cast the type or use type ignore comments as a temporary workaround while reporting the issue to the `typeshed` project.","cause":"This usually indicates a type checker issue where the return type of `nanoid.generate` (or related functions) is being misinterpreted, often due to an outdated `types-nanoid` stub for a newer `nanoid` version, or vice-versa.","error":"Incompatible types in assignment (expression has type \"str\", variable has type \"None\") (Mypy error code: assignment)"}]}