Typing stubs for qrcode

8.2.0.20260408 · active · verified Fri Apr 10

types-qrcode is a type stub package providing external type annotations for the 'qrcode' Python library. It enables static type checkers like Mypy and Pyright to analyze code that uses 'qrcode', ensuring type correctness without altering runtime behavior. This package is part of the Typeshed project, which releases updates frequently (sometimes daily) to maintain alignment with the 'qrcode' library's versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate and save a basic QR code using the 'qrcode' library. With 'types-qrcode' installed, a type checker would analyze this code for type correctness. The `qrcode[pil]` installation is crucial for the `img.save()` method to work correctly.

import qrcode

# Data to be encoded in the QR code
data = "https://www.example.com/your-data-here"

# Create QR code instance with desired parameters
# version: controls the size of the QR Code (1 to 40). None for auto-sizing.
# error_correction: controls the error correction level.
# box_size: size of each box (pixel) in the QR Code.
# border: thickness of the border (minimum 4).
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction
    box_size=10,
    border=4,
)

# Add data to the QR code
qr.add_data(data)
qr.make(fit=True)

# Create an image from the QR code instance
# Pillow (PIL) is required for saving as an image file like PNG
img = qr.make_image(fill_color="black", back_color="white")

# Save the image
# In a real application, you might use os.path.join for paths
output_filename = "my_qrcode.png"
img.save(output_filename)

print(f"QR Code saved to {output_filename}")

view raw JSON →