Typing Stubs for ldap3

2.9.13.20260408 · active · verified Thu Apr 16

types-ldap3 provides type hinting stubs for the popular `ldap3` library, enabling static type checkers like MyPy to validate `ldap3` code. It is part of the Python typeshed project, ensuring up-to-date and high-quality type definitions. This package does not provide any runtime functionality itself but enhances developer experience by catching type-related errors early. It is actively maintained and updated regularly as part of typeshed.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic LDAP connection and search using the `ldap3` library. By installing `types-ldap3`, type checkers like MyPy can now provide accurate type suggestions and detect errors in this code, enhancing development quality without changing the runtime behavior.

import os
from ldap3 import Server, Connection, AUTH_SIMPLE, STRATEGY_SYNC

# Configure LDAP connection details (replace with your server/credentials)
LDAP_SERVER = os.environ.get('LDAP_SERVER', 'ldap.forumsys.com') # Example public LDAP server
LDAP_PORT = int(os.environ.get('LDAP_PORT', '389'))
LDAP_USER = os.environ.get('LDAP_USER', 'cn=read-only-admin,dc=example,dc=com')
LDAP_PASSWORD = os.environ.get('LDAP_PASSWORD', 'password')

try:
    # Initialize LDAP server and connection
    server = Server(LDAP_SERVER, port=LDAP_PORT)
    conn = Connection(server,
                      user=LDAP_USER,
                      password=LDAP_PASSWORD,
                      authentication=AUTH_SIMPLE,
                      strategy=STRATEGY_SYNC)

    # Bind to the server
    if not conn.bind():
        print(f"LDAP bind failed: {conn.result}")
        exit(1)

    print(f"Successfully connected to LDAP server: {LDAP_SERVER}")

    # Example: Perform a search operation
    search_base = 'dc=example,dc=com'
    search_filter = '(objectClass=person)'
    conn.search(search_base, search_filter, attributes=['cn', 'mail'])

    print(f"Found {len(conn.entries)} entries:")
    for entry in conn.entries:
        print(f"  CN: {entry.cn}, Mail: {entry.mail}")

    # Unbind from the server
    conn.unbind()
    print("Connection unbound.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →