Skip to content

[python-ldap] Add type hints #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6c3530a
[python-ldap] Remove compatibility wrappers for Py2/Py3
Alphix Jan 26, 2024
d80e4be
[python-ldap] Make __version__ imports more consistent
Alphix Jan 26, 2024
fad0b14
[python-ldap] Type fixes for Lib/ldap/__init__.py
Alphix Jan 26, 2024
5120bb8
[python-ldap] Type fixes for Lib/ldap/asyncsearch.py
Alphix Jan 26, 2024
c65e2db
[python-ldap] Type fixes for Lib/ldap/cidict.py
Alphix Jan 26, 2024
99a71b8
[python-ldap] Type fixes for Lib/ldap/constants.py
Alphix Jan 26, 2024
9969bd5
[python-ldap] Type fixes for Lib/ldap/controls/psearch.py
Alphix Jan 26, 2024
c5512e3
[python-ldap] Type fixes for Lib/ldap/controls/psearch.py
Alphix Jan 26, 2024
69dce72
[python-ldap] Type fixes for Lib/ldap/controls/simple.py
Alphix Jan 26, 2024
48d0949
[python-ldap] Type fixes for Lib/ldap/controls/sss.py
Alphix Jan 26, 2024
6cb5e19
[python-ldap] Type fixes for Lib/ldap/extop/__init__.py
Alphix Jan 26, 2024
4775c56
[python-ldap] Type fixes for Lib/ldap/modlist.py
Alphix Jan 26, 2024
a754932
[python-ldap] Type fixes for Lib/ldap/resiter.py
Alphix Jan 26, 2024
7ab5fc5
[python-ldap] Type fixes for Lib/ldap/sasl.py
Alphix Jan 26, 2024
b73ca2d
[python-ldap] Type fixes for Lib/ldap/syncrepl.py
Alphix Jan 27, 2024
66e130c
[python-ldap] Type fixes for Lib/ldapurl.py
Alphix Jan 27, 2024
a37570d
[python-ldap] Type fixes for Lib/slaptest/*
Alphix Jan 27, 2024
d2f20b5
[python-ldap] Type fixes for Lib/ldif.py
Alphix Jan 27, 2024
2f91645
[python-ldap] Type fixes for Lib/ldap/ldapobject.py
Alphix Jan 27, 2024
75f871f
[python-ldap] Type fixes for Lib/ldap/schema/subentry.py
Alphix Jan 27, 2024
4a74ec7
[python-ldap] Type fixes for Lib/ldap/schema/models.py
Alphix Jan 27, 2024
9a0856a
[python-ldap] Type fixes for Lib/ldap/controls/openldap.py
Alphix Jan 27, 2024
6c08182
[python-ldap] Type fixes for Lib/ldap/controls/pagedresults.py
Alphix Jan 27, 2024
c1a44b4
[python-ldap] Type fixes for Lib/ldap/extop/__init__.py
Alphix Jan 27, 2024
14a305d
[python-ldap] Improve documentation in Lib/ldap/dn.py
Alphix Jan 27, 2024
ec5dc51
[python-ldap] Correct KNOWN_RESPONSE_CONTROLS registration in Lib/lda…
Alphix Jan 27, 2024
1897ba8
[python-ldap] Add type annotations
Alphix Jan 27, 2024
1b51ea1
[python-ldap] Integrate typing into the build/CI system
Alphix Jan 27, 2024
c30644e
[python-ldap] Move _ldap to ldap._ldap
Alphix Jan 30, 2024
ba337e8
[python-ldap] Add stubtest to typing CI
Alphix Jan 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Doc/fake_ldap_module_for_documentation.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"""
A module that mocks `_ldap` for the purposes of generating documentation
A module that mocks `ldap._ldap` for the purposes of generating documentation

This module provides placeholders for the contents of `_ldap`, making it
possible to generate documentation even _ldap is not compiled.
This module provides placeholders for the contents of `ldap._ldap`, making it
possible to generate documentation even if ldap._ldap is not compiled.
It should also make the documentation independent of which features are
available in the system OpenLDAP library.

The overly long module name will show up in AttributeError messages,
hinting that this is not the actual _ldap.
hinting that this is not the actual ldap._ldap.

See https://www.python-ldap.org/ for details.
"""

import sys

# Cause `import _ldap` to import this module instead of the actual `_ldap`.
sys.modules['_ldap'] = sys.modules[__name__]
# Cause `import ldap._ldap` to import this module instead of the actual module.
sys.modules['ldap._ldap'] = sys.modules[__name__]

from constants import CONSTANTS
from pkginfo import __version__
Expand Down
38 changes: 24 additions & 14 deletions Lib/ldap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
import os
import sys

from typing import Any, Type, Optional, Union


if __debug__:
# Tracing is only supported in debugging mode
import atexit
import traceback
_trace_level = int(os.environ.get("PYTHON_LDAP_TRACE_LEVEL", 0))
_trace_file = os.environ.get("PYTHON_LDAP_TRACE_FILE")
if _trace_file is None:
_trace_file_path = os.environ.get("PYTHON_LDAP_TRACE_FILE")
if _trace_file_path is None:
_trace_file = sys.stderr
else:
_trace_file = open(_trace_file, 'a')
_trace_file = open(_trace_file_path, 'a')
atexit.register(_trace_file.close)
_trace_stack_limit = None
else:
Expand All @@ -31,10 +34,10 @@
_trace_file = sys.stderr
_trace_stack_limit = None

import _ldap
import ldap._ldap as _ldap
assert _ldap.__version__==__version__, \
ImportError(f'ldap {__version__} and _ldap {_ldap.__version__} version mismatch!')
from _ldap import *
from ldap._ldap import *
# call into libldap to initialize it right now
LIBLDAP_API_INFO = _ldap.get_option(_ldap.OPT_API_INFO)

Expand All @@ -45,18 +48,21 @@

class DummyLock:
"""Define dummy class with methods compatible to threading.Lock"""
def __init__(self):
pass
def acquire(self):
def __init__(self) -> None:
pass
def release(self):

def acquire(self) -> bool:
return True

def release(self) -> None:
pass

try:
# Check if Python installation was build with thread support
# FIXME: This can be simplified, from Python 3.7 this module is mandatory
import threading
except ImportError:
LDAPLockBaseClass = DummyLock
LDAPLockBaseClass: Union[Type[DummyLock], Type[threading.Lock]] = DummyLock
else:
LDAPLockBaseClass = threading.Lock

Expand All @@ -69,7 +75,11 @@ class LDAPLock:
"""
_min_trace_level = 3

def __init__(self,lock_class=None,desc=''):
def __init__(
self,
lock_class: Optional[Type[Any]] = None,
desc: str = ''
) -> None:
"""
lock_class
Class compatible to threading.Lock
Expand All @@ -79,19 +89,19 @@ def __init__(self,lock_class=None,desc=''):
self._desc = desc
self._lock = (lock_class or LDAPLockBaseClass)()

def acquire(self):
def acquire(self) -> bool:
if __debug__:
global _trace_level
if _trace_level>=self._min_trace_level:
_trace_file.write('***{}.acquire() {} {}\n'.format(self.__class__.__name__,repr(self),self._desc))
return self._lock.acquire()

def release(self):
def release(self) -> None:
if __debug__:
global _trace_level
if _trace_level>=self._min_trace_level:
_trace_file.write('***{}.release() {} {}\n'.format(self.__class__.__name__,repr(self),self._desc))
return self._lock.release()
self._lock.release()


# Create module-wide lock for serializing all calls into underlying LDAP lib
Expand Down
Loading