Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41ffdf3

Browse files
committed
[python-ldap] Fix comments and suggestions from first review round
Backwards compatibility fixes (the majority of the changes): * Use Optional[<type>] instead of <type> | None * Use Union[<typeA>, <typeB>] instead of <typeA> | <typeB> * Remove use of from __future__ import annotations Other: * Move module from Lib/ldap_types.py to Lib/ldap/types.py * Remove use of from ldap_types import * * Move mypy.ini content to setup.cfg (pyproject.toml caused intermittent errors) * Extend tox.ini with mypy checks * Move _ldap.pyi to Lib/_ldap.pyi and add to MANIFEST.in * Add PEP561 type marker file (Lib/ldap/py.typed)
1 parent 3ebd8a1 commit 41ffdf3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+430
-405
lines changed
File renamed without changes.

Lib/ldap/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
44
See https://www.python-ldap.org/ for details.
55
"""
6-
from __future__ import annotations
7-
86
# This is also the overall release version number
97

108
from ldap.pkginfo import __version__, __author__, __license__
119

1210
import os
1311
import sys
1412

15-
from typing import Any, Type
13+
from typing import Any, Type, Optional
1614

1715

1816
if __debug__:
@@ -78,7 +76,7 @@ class LDAPLock:
7876

7977
def __init__(
8078
self,
81-
lock_class: Type[Any] | None = None,
79+
lock_class: Optional[Type[Any]] = None,
8280
desc: str = ''
8381
) -> None:
8482
"""

Lib/ldap/asyncsearch.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
44
See https://www.python-ldap.org/ for details.
55
"""
6-
from __future__ import annotations
7-
86
import ldap
97

108
from ldap.pkginfo import __version__
119
from ldap.controls import RequestControl
12-
from typing import Any, Dict as DictType, Iterable, List as ListType, Sequence, TextIO, Tuple
13-
from ldap_types import *
10+
from typing import (
11+
Any,
12+
Dict as DictType,
13+
Iterable,
14+
List as ListType,
15+
Sequence,
16+
TextIO,
17+
Tuple,
18+
Optional,
19+
Union,
20+
)
21+
from ldap.types import LDAPSearchResult, LDAPEntryDict
1422

1523
import ldif
1624

@@ -56,20 +64,20 @@ class AsyncSearchHandler:
5664

5765
def __init__(self, l: ldap.ldapobject.LDAPObject) -> None:
5866
self._l = l
59-
self._msgId: int | None = None
67+
self._msgId: Optional[int] = None
6068
self._afterFirstResult = 1
6169

6270
def startSearch(
6371
self,
6472
searchRoot: str,
6573
searchScope: int,
6674
filterStr: str,
67-
attrList: ListType[str] | None = None,
75+
attrList: Optional[ListType[str]] = None,
6876
attrsOnly: int = 0,
6977
timeout: int = -1,
7078
sizelimit: int = 0,
71-
serverctrls: ListType[RequestControl] | None = None,
72-
clientctrls: ListType[RequestControl] | None = None,
79+
serverctrls: Optional[ListType[RequestControl]] = None,
80+
clientctrls: Optional[ListType[RequestControl]] = None,
7381
) -> None:
7482
"""
7583
searchRoot
@@ -237,7 +245,7 @@ class IndexedDict(Dict):
237245
def __init__(
238246
self,
239247
l: ldap.ldapobject.LDAPObject,
240-
indexed_attrs: Sequence[str] | None = None,
248+
indexed_attrs: Optional[Sequence[str]] = None,
241249
) -> None:
242250
Dict.__init__(self,l)
243251
self.indexed_attrs = indexed_attrs or ()
@@ -314,7 +322,7 @@ class LDIFWriter(FileWriter):
314322
def __init__(
315323
self,
316324
l: ldap.ldapobject.LDAPObject,
317-
writer_obj: TextIO | ldif.LDIFWriter,
325+
writer_obj: Union[TextIO, ldif.LDIFWriter],
318326
headerStr: str = '',
319327
footerStr: str = '',
320328
) -> None:

Lib/ldap/cidict.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
66
See https://www.python-ldap.org/ for details.
77
"""
8-
from __future__ import annotations
9-
108
import warnings
119

1210
from collections.abc import MutableMapping
1311
from ldap.pkginfo import __version__
1412

15-
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, MutableMapping as MutableMappingType, TypeVar
13+
from typing import (
14+
Any,
15+
Dict,
16+
Iterator,
17+
List,
18+
Mapping,
19+
MutableMapping as MutableMappingType,
20+
TypeVar,
21+
Optional,
22+
)
1623

1724
T = TypeVar('T', bound=Any)
1825

19-
if TYPE_CHECKING:
20-
from typing_extensions import Self
26+
from typing_extensions import Self
2127

2228

2329
class cidict(MutableMappingType[str, T]):
@@ -26,7 +32,7 @@ class cidict(MutableMappingType[str, T]):
2632
"""
2733
__slots__ = ('_keys', '_data')
2834

29-
def __init__(self, default: Mapping[str, T] | None = None) -> None:
35+
def __init__(self, default: Optional[Mapping[str, T]] = None) -> None:
3036
self._keys: Dict[str, str] = {}
3137
self._data: Dict[str, T] = {}
3238
if default:

Lib/ldap/constants.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
- Provide support for building documentation without compiling python-ldap
1010
1111
"""
12-
from __future__ import annotations
13-
14-
from typing import Any, List, Sequence
12+
from typing import Any, List, Sequence, Optional
1513

1614
# This module cannot import anything from ldap.
1715
# When building documentation, it is used to initialize ldap.__init__.
@@ -21,14 +19,14 @@ class Constant:
2119
"""Base class for a definition of an OpenLDAP constant
2220
"""
2321

24-
c_template: str | None = None
22+
c_template: Optional[str] = None
2523

2624
def __init__(
2725
self,
2826
name: str,
2927
optional: bool = False,
3028
requirements: Sequence[str] = (),
31-
doc: str | None = None,
29+
doc: Optional[str] = None,
3230
) -> None:
3331
self.name = name
3432
if optional:

Lib/ldap/controls/__init__.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
The ldap.controls module provides LDAPControl classes.
88
Each class provides support for a certain control.
99
"""
10-
from __future__ import annotations
11-
1210
from ldap.pkginfo import __version__
1311

1412
import _ldap
@@ -19,7 +17,7 @@
1917

2018
from pyasn1.error import PyAsn1Error
2119

22-
from typing import Dict, List, Tuple, Type
20+
from typing import Dict, List, Tuple, Type, Optional
2321

2422

2523
__all__ = [
@@ -41,7 +39,7 @@
4139
]
4240

4341
# response control OID to class registry
44-
KNOWN_RESPONSE_CONTROLS: Dict[str, Type[ResponseControl]] = {}
42+
KNOWN_RESPONSE_CONTROLS: Dict[str, Type["ResponseControl"]] = {}
4543

4644

4745
class RequestControl:
@@ -59,15 +57,15 @@ class RequestControl:
5957

6058
def __init__(
6159
self,
62-
controlType: str | None = None,
60+
controlType: Optional[str] = None,
6361
criticality: bool = False,
64-
encodedControlValue: bytes | None = None
62+
encodedControlValue: Optional[bytes] = None
6563
) -> None:
6664
self.controlType = controlType
6765
self.criticality = criticality
6866
self.encodedControlValue = encodedControlValue
6967

70-
def encodeControlValue(self) -> bytes | None:
68+
def encodeControlValue(self) -> Optional[bytes]:
7169
"""
7270
sets class attribute encodedControlValue to the BER-encoded ASN.1
7371
control value composed by class attributes set before
@@ -87,7 +85,7 @@ class ResponseControl:
8785

8886
def __init__(
8987
self,
90-
controlType: str | None = None,
88+
controlType: Optional[str] = None,
9189
criticality: bool = False
9290
) -> None:
9391
self.controlType = controlType
@@ -99,7 +97,7 @@ def decodeControlValue(self, encodedControlValue: bytes) -> None:
9997
class attributes
10098
"""
10199
# The type hint can be removed once class LDAPControl is removed
102-
self.encodedControlValue: bytes | None = encodedControlValue
100+
self.encodedControlValue: Optional[bytes] = encodedControlValue
103101

104102

105103
class LDAPControl(RequestControl, ResponseControl):
@@ -110,10 +108,10 @@ class LDAPControl(RequestControl, ResponseControl):
110108

111109
def __init__(
112110
self,
113-
controlType: str | None = None,
111+
controlType: Optional[str] = None,
114112
criticality: bool = False,
115-
controlValue: str | None = None,
116-
encodedControlValue: bytes | None = None
113+
controlValue: Optional[str] = None,
114+
encodedControlValue: Optional[bytes] = None
117115
) -> None:
118116
self.controlType = controlType
119117
self.criticality = criticality
@@ -122,8 +120,8 @@ def __init__(
122120

123121

124122
def RequestControlTuples(
125-
ldapControls: List[RequestControl] | None
126-
) -> List[Tuple[str | None, bool, bytes | None]] | None:
123+
ldapControls: Optional[List[RequestControl]]
124+
) -> Optional[List[Tuple[Optional[str], bool, Optional[bytes]]]]:
127125
"""
128126
Return list of readily encoded 3-tuples which can be directly
129127
passed to C module _ldap
@@ -142,8 +140,8 @@ def RequestControlTuples(
142140

143141

144142
def DecodeControlTuples(
145-
ldapControlTuples: List[Tuple[str, bool, bytes]] | None,
146-
knownLDAPControls: Dict[str, Type[ResponseControl]] | None = None,
143+
ldapControlTuples: Optional[List[Tuple[str, bool, bytes]]],
144+
knownLDAPControls: Optional[Dict[str, Type[ResponseControl]]] = None,
147145
) -> List[ResponseControl]:
148146
"""
149147
Returns list of readily decoded ResponseControl objects

Lib/ldap/controls/deref.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
See https://www.python-ldap.org/ for project details.
66
"""
7-
from __future__ import annotations
8-
97
__all__ = [
108
'DEREF_CONTROL_OID',
119
'DereferenceControl',
@@ -19,7 +17,7 @@
1917
from pyasn1.codec.ber import encoder,decoder
2018
from pyasn1_modules.rfc2251 import LDAPDN,AttributeDescription,AttributeDescriptionList,AttributeValue
2119

22-
from typing import Dict, List, Tuple
20+
from typing import Dict, List, Tuple, Optional
2321

2422
DEREF_CONTROL_OID = '1.3.6.1.4.1.4203.666.5.16'
2523

@@ -85,7 +83,7 @@ class DereferenceControl(LDAPControl):
8583
def __init__(
8684
self,
8785
criticality: bool = False,
88-
derefSpecs: Dict[str, List[str]] | None = None,
86+
derefSpecs: Optional[Dict[str, List[str]]] = None,
8987
) -> None:
9088
LDAPControl.__init__(self,self.controlType,criticality)
9189
self.derefSpecs = derefSpecs or {}

Lib/ldap/controls/libldap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
See https://www.python-ldap.org/ for details.
66
"""
7-
from __future__ import annotations
8-
97
from ldap.pkginfo import __version__
108

119
import _ldap
@@ -16,6 +14,8 @@
1614

1715
from ldap.controls import RequestControl,LDAPControl,KNOWN_RESPONSE_CONTROLS
1816

17+
from typing import Optional, Union
18+
1919

2020
class AssertionControl(RequestControl):
2121
"""
@@ -80,8 +80,8 @@ class SimplePagedResultsControl(LDAPControl):
8080
def __init__(
8181
self,
8282
criticality: bool = False,
83-
size: int | None = None,
84-
cookie: str | bytes | None = None
83+
size: Optional[int] = None,
84+
cookie: Optional[Union[str, bytes]] = None
8585
) -> None:
8686
self.criticality = criticality
8787
self.size,self.cookie = size,cookie

Lib/ldap/controls/openldap.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
44
See https://www.python-ldap.org/ for project details.
55
"""
6-
from __future__ import annotations
7-
86
import ldap.controls
97
from ldap.controls import ValueLessRequestControl,ResponseControl
108

119
from pyasn1.type import univ
1210
from pyasn1.codec.ber import decoder
1311

14-
from typing import List, Tuple
12+
from typing import List, Tuple, Union
1513

1614
__all__ = [
1715
'SearchNoOpControl',
@@ -58,7 +56,7 @@ def noop_search_st(
5856
scope: int = ldap.SCOPE_SUBTREE,
5957
filterstr: str = '(objectClass=*)',
6058
timeout: int = -1,
61-
) -> Tuple[int, int] | Tuple[None, None]:
59+
) -> Union[Tuple[int, int], Tuple[None, None]]:
6260
try:
6361
msg_id = self.search_ext(
6462
base,

Lib/ldap/controls/pagedresults.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
See https://www.python-ldap.org/ for project details.
66
"""
7-
from __future__ import annotations
8-
97
__all__ = [
108
'SimplePagedResultsControl'
119
]
@@ -19,6 +17,8 @@
1917
from pyasn1.codec.ber import encoder,decoder
2018
from pyasn1_modules.rfc2251 import LDAPString
2119

20+
from typing import Union
21+
2222

2323
class PagedResultsControlValue(univ.Sequence): # type: ignore
2424
componentType = namedtype.NamedTypes(
@@ -35,7 +35,7 @@ def __init__(
3535
self,
3636
criticality: bool = False,
3737
size: int = 10,
38-
cookie: str | bytes = '',
38+
cookie: Union[str, bytes] = '',
3939
) -> None:
4040
self.criticality = criticality
4141
self.size = size

0 commit comments

Comments
 (0)