Skip to content

Deprecate use of .has_key() to follow modern Python #153

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions Lib/ldap/cidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

from ldap import __version__

Expand Down Expand Up @@ -39,6 +40,12 @@ def update(self,dict):
self[key] = dict[key]

def has_key(self,key):
warnings.warn(
"%s.has_key() is deprecated and will be removed in a future version of "
"python-ldap. Use the 'in' operator instead." % self.__class__.__name__,
category=DeprecationWarning,
stacklevel=2,
)
return key in self

def __contains__(self,key):
Expand Down
6 changes: 6 additions & 0 deletions Lib/ldap/schema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ def __delitem__(self,nameoroid):
del self._keytuple2attrtype[k]

def has_key(self,nameoroid):
warnings.warn(
"%s.has_key() is deprecated and will be removed in a future version of "
"python-ldap. Use the 'in' operator instead." % self.__class__.__name__,
category=DeprecationWarning,
stacklevel=2,
)
k = self._at2key(nameoroid)
return k in self.data

Expand Down
35 changes: 32 additions & 3 deletions Tests/t_cidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@

See https://www.python-ldap.org/ for details.
"""
from __future__ import unicode_literals

# from Python's standard lib
import sys
import unittest
import warnings

# from python-ldap
import ldap, ldap.cidict


if sys.version_info[0] <= 2:
text_type = unicode
else:
text_type = str


class TestCidict(unittest.TestCase):
"""
test ldap.cidict.cidict
Expand Down Expand Up @@ -41,8 +49,29 @@ def test_cidict(self):
self.assertEqual("AbCDef" in cix._keys, False)
self.assertEqual("abcdef" in cix, False)
self.assertEqual("AbCDef" in cix, False)
self.assertEqual(cix.has_key("abcdef"), False)
self.assertEqual(cix.has_key("AbCDef"), False)
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
warnings.simplefilter("always")
self.assertEqual(cix.has_key("abcdef"), False)
self.assertEqual(len(w), 1)
msg = w[-1]
self.assertIs(msg.category, DeprecationWarning)
self.assertEqual(
text_type(msg.message),
"cidict.has_key() is deprecated and will be removed in a future version of "
"python-ldap. Use the 'in' operator instead."
)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.assertEqual(cix.has_key("AbCDef"), False)
self.assertEqual(len(w), 1)
msg = w[-1]
self.assertIs(msg.category, DeprecationWarning)
self.assertEqual(
text_type(msg.message),
"cidict.has_key() is deprecated and will be removed in a future version of "
"python-ldap. Use the 'in' operator instead."
)


if __name__ == '__main__':
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy