Typing Stubs for ldap3
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
-
ModuleNotFoundError: No module named 'ldap3'
cause The `types-ldap3` package provides only type definitions (stubs), not the actual runtime library. You need the `ldap3` package itself.fixInstall the `ldap3` library: `pip install ldap3`. -
error: Module 'ldap3' has no attribute 'Server' [attr-defined]
cause This error, reported by a type checker like MyPy, indicates that the type stubs for `ldap3` are either missing, incorrect for your `ldap3` version, or not being picked up correctly. It implies that MyPy doesn't know about `ldap3`'s `Server` class.fixEnsure `types-ldap3` is installed (`pip install types-ldap3`). If it is, verify that its version is compatible with your `ldap3` version. You might need to update or downgrade `ldap3` or check for a specific `types-ldap3` version. -
Skipping analyzing 'ldap3': module is installed, but missing library stubs or py.typed marker
cause MyPy reports this when it finds the `ldap3` runtime package but cannot locate corresponding type stubs, meaning it will treat `ldap3` as `Any`.fixInstall the type stubs for `ldap3`: `pip install types-ldap3`.
Warnings
- gotcha `types-ldap3` provides only type stubs and no runtime functionality. You must install the actual `ldap3` library for your application to run.
- gotcha Type checking issues can arise if the version of `types-ldap3` does not match the installed `ldap3` library version, especially after significant API changes in `ldap3`.
- gotcha Installing `types-ldap3` alone will not resolve `ModuleNotFoundError: No module named 'ldap3'` at runtime. Stubs are for static analysis, not runtime dependencies.
Install
-
pip install types-ldap3 ldap3 -
pip install types-ldap3
Imports
- Server, Connection
from ldap3 import Server, Connection
Quickstart
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}")