From 9fd8f00b6113cb8185aaacb81ba6700bc2e642eb Mon Sep 17 00:00:00 2001 From: William Brown Date: Fri, 17 Nov 2017 11:26:54 +1000 Subject: [PATCH] Ticket 106 lib389 - assert objects are correctly typed. Bug Description: It was possible to give wrong types to functions leading to some vague and weird errors. While experienced devs can see these easier, it's hard for new contributors to track. Fix Description: python 3.5 supports a static typing model. We should use this to help improve the quality of our code, and eliminate these issue quickly and at the source. https://pagure.io/lib389/issue/106 Author: wibrown Review by: ??? --- src/lib389/lib389/_mapped_object.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/lib389/lib389/_mapped_object.py b/src/lib389/lib389/_mapped_object.py index 4e5bc72..27f3973 100644 --- a/src/lib389/lib389/_mapped_object.py +++ b/src/lib389/lib389/_mapped_object.py @@ -8,10 +8,13 @@ import ldap import ldap.dn +from ldap.ldapobject import SimpleLDAPObject from ldap import filter as ldap_filter import logging from functools import partial +from typing import Type, Optional + from lib389._entry import Entry from lib389._constants import DIRSRV_STATE_ONLINE from lib389.utils import ( @@ -91,7 +94,7 @@ class DSLdapObject(DSLogging): """ # TODO: Automatically create objects when they are requested to have properties added - def __init__(self, instance, dn=None, batch=False): + def __init__(self, instance: Type[SimpleLDAPObject], dn: Optional[str]=None, batch: bool=False): self._instance = instance super(DSLdapObject, self).__init__(self._instance.verbose) # This allows some factor objects to be overriden @@ -111,14 +114,14 @@ class DSLdapObject(DSLogging): self._server_controls = None self._client_controls = None - def __unicode__(self): + def __unicode__(self) -> str: val = self._dn if self._rdn_attribute: # What if the rdn is multi value and we don't get the primary .... ARGHHH val = self.get_attr_val(self._rdn_attribute) return ensure_str(val) - def __str__(self): + def __str__(self) -> str: return self.__unicode__() def raw_entry(self): @@ -129,7 +132,7 @@ class DSLdapObject(DSLogging): return self._instance.search_ext_s(self._dn, ldap.SCOPE_BASE, attrlist=["*"], serverctrls=self._server_controls, clientctrls=self._client_controls)[0] - def exists(self): + def exists(self) -> bool: """Check if the entry exists :returns: True if it exists @@ -209,7 +212,7 @@ class DSLdapObject(DSLogging): # How can we be sure this returns the primary one? return ensure_str(self.get_attr_val(self._rdn_attribute)) - def present(self, attr, value=None): + def present(self, attr, value=None) -> bool: """Assert that some attr, or some attr / value exist on the entry. :param attr: an attribute name -- 1.8.3.1