Merge ~cjwatson/lazr.restful:deprecate-safe-hasattr into lazr.restful:main

Proposed by Colin Watson
Status: Merged
Merged at revision: 56d6794c1fc692856b53291b28a00bd43d78b872
Proposed branch: ~cjwatson/lazr.restful:deprecate-safe-hasattr
Merge into: lazr.restful:main
Diff against target: 115 lines (+26/-13)
4 files modified
NEWS.rst (+2/-0)
src/lazr/restful/docs/utils.rst (+12/-9)
src/lazr/restful/marshallers.py (+1/-2)
src/lazr/restful/utils.py (+11/-2)
Reviewer Review Type Date Requested Status
Guruprasad Approve
Review via email: mp+413874@code.launchpad.net

Commit message

Deprecate safe_hasattr

Description of the change

It's no longer needed now that `hasattr` only catches `AttributeError`. See https://docs.python.org/3/whatsnew/3.2.html#other-language-changes.

To post a comment you must log in.
Revision history for this message
Guruprasad (lgp171188) wrote :

These changes are straightforward and look good to me. 👍

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/NEWS.rst b/NEWS.rst
2index a909598..6e42cce 100644
3--- a/NEWS.rst
4+++ b/NEWS.rst
5@@ -25,6 +25,8 @@ NEWS for lazr.restful
6 cannot work on Python 3. Use the class decorators
7 ``@exported_as_webservice_entry`` and
8 ``@exported_as_webservice_collection`` instead.
9+- Deprecate ``lazr.restful.utils.safe_hasattr``, since Python's builtin
10+ ``hasattr`` is fixed as of Python 3.2.
11
12 1.1.0 (2021-10-07)
13 ==================
14diff --git a/src/lazr/restful/docs/utils.rst b/src/lazr/restful/docs/utils.rst
15index ea1636a..525473e 100644
16--- a/src/lazr/restful/docs/utils.rst
17+++ b/src/lazr/restful/docs/utils.rst
18@@ -300,6 +300,7 @@ makes problems harder to diagnose.
19 (The builtin hasattr() is fixed as of Python 3.2.)
20
21 >>> import sys
22+ >>> import warnings
23 >>> from lazr.restful.utils import safe_hasattr
24
25 >>> class Oracle:
26@@ -307,26 +308,28 @@ makes problems harder to diagnose.
27 ... def is_full_moon(self):
28 ... return full_moon
29 >>> oracle = Oracle()
30- >>> if sys.version_info[:2] < (3, 2):
31- ... hasattr(oracle, 'is_full_moon')
32- ... else:
33- ... False
34- False
35- >>> safe_hasattr(oracle, 'is_full_moon')
36+ >>> with warnings.catch_warnings(record=True):
37+ ... safe_hasattr(oracle, 'is_full_moon')
38 Traceback (most recent call last):
39 ...
40- NameError: ...name 'full_moon' is not defined
41+ NameError: name 'full_moon' is not defined
42
43 >>> full_moon = True
44 >>> hasattr(oracle, 'is_full_moon')
45 True
46- >>> safe_hasattr(oracle, 'is_full_moon')
47+ >>> with warnings.catch_warnings(record=True) as warnings_log:
48+ ... safe_hasattr(oracle, 'is_full_moon')
49 True
50+ >>> print(warnings_log[0].message)
51+ safe_hasattr is deprecated; use the hasattr builtin instead.
52
53 >>> hasattr(oracle, 'weather')
54 False
55- >>> safe_hasattr(oracle, 'weather')
56+ >>> with warnings.catch_warnings(record=True) as warnings_log:
57+ ... safe_hasattr(oracle, 'weather')
58 False
59+ >>> print(warnings_log[0].message)
60+ safe_hasattr is deprecated; use the hasattr builtin instead.
61
62 smartquote()
63 ============
64diff --git a/src/lazr/restful/marshallers.py b/src/lazr/restful/marshallers.py
65index e8b38fe..3279657 100644
66--- a/src/lazr/restful/marshallers.py
67+++ b/src/lazr/restful/marshallers.py
68@@ -55,7 +55,6 @@ from lazr.restful.interfaces import (
69 IServiceRootResource,
70 IWebServiceConfiguration,
71 )
72-from lazr.restful.utils import safe_hasattr
73
74
75 class URLDereferencingMixin:
76@@ -310,7 +309,7 @@ class BytesFieldMarshaller(SimpleFieldMarshaller):
77 """
78 if value is None:
79 return None
80- if safe_hasattr(value, "seek"):
81+ if hasattr(value, "seek"):
82 value.seek(0)
83 value = value.read()
84 elif not isinstance(value, (bytes, str)):
85diff --git a/src/lazr/restful/utils.py b/src/lazr/restful/utils.py
86index 2175977..244bfed 100644
87--- a/src/lazr/restful/utils.py
88+++ b/src/lazr/restful/utils.py
89@@ -24,6 +24,7 @@ import operator
90 import re
91 import string
92 import subprocess
93+import warnings
94
95 from zope.component import getUtility
96 from zope.schema import getFieldsInOrder
97@@ -357,8 +358,16 @@ def camelcase_to_underscore_separated(name):
98
99
100 def safe_hasattr(ob, name):
101- """hasattr() that doesn't hide exceptions."""
102- return getattr(ob, name, missing) is not missing
103+ """hasattr() that doesn't hide exceptions.
104+
105+ This is no longer useful in Python >= 3.2.
106+ """
107+ warnings.warn(
108+ "safe_hasattr is deprecated; use the hasattr builtin instead.",
109+ DeprecationWarning,
110+ stacklevel=2,
111+ )
112+ return hasattr(ob, name)
113
114
115 def smartquote(str):

Subscribers

People subscribed via source and target branches