Merge ~cjwatson/lazr.restful:remove-six into lazr.restful:main

Proposed by Colin Watson
Status: Merged
Merged at revision: 6614e9088b62aca53bfb52ca13259dd34529492a
Proposed branch: ~cjwatson/lazr.restful:remove-six
Merge into: lazr.restful:main
Diff against target: 242 lines (+26/-29)
8 files modified
setup.py (+0/-1)
src/lazr/restful/debug.py (+1/-2)
src/lazr/restful/declarations.py (+1/-2)
src/lazr/restful/docs/webservice.rst (+14/-10)
src/lazr/restful/example/base/tests/collection.txt (+1/-3)
src/lazr/restful/example/base/tests/entry.txt (+1/-3)
src/lazr/restful/testing/helpers.py (+1/-2)
src/lazr/restful/testing/webservice.py (+7/-6)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+414022@code.launchpad.net

Commit message

Remove remaining uses of six

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

It is a bit unfortunate that we do not run coverage for this project (and all projects).

Some occurrences of `six.ensure_str` were just removed, some were replaced by xxx.decode().

I cannot say whether this is correct or not.

I quickly tried to setup coverage for the review, the tests ran, but got a "no data collected" error :-/

```
[testenv]
deps =
    .[test,xml]
    zope.testrunner
    coverage
commands =
    coverage run -m zope.testrunner --test-path src --tests-pattern ^tests {posargs}
    coverage report -m
```

I have no clue what is going wrong here.

Revision history for this message
Colin Watson (cjwatson) wrote :

All but one of the `six.ensure_*` calls that I removed are themselves in test code, so it's fairly easy to see that they're covered. The one exception is in `lazr.restful.declarations._versioned_class_name`, which is called by `lazr.restful.declarations.generate_*`. For removing `six.ensure_str` to be a problem there, somebody would have to have explicitly included a bytes item in their `IWebServiceConfiguration.active_versions`, which is declared as a list of `TextLine`; so I don't think this can be a problem.

Coverage seems to work fine for me, but declaring `coverage:run` would help so that it doesn't try to consider coverage of all dependencies as well. I'll propose a separate branch to set up coverage.

Revision history for this message
Jürgen Gmach (jugmac00) wrote :

Would you please rebase this MP on current main so includes the coverage env?

Revision history for this message
Colin Watson (cjwatson) wrote :

Done.

Revision history for this message
Jürgen Gmach (jugmac00) :
review: Approve
Revision history for this message
Colin Watson (cjwatson) :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/setup.py b/setup.py
2index 1017963..c21f37a 100755
3--- a/setup.py
4+++ b/setup.py
5@@ -62,7 +62,6 @@ setup(
6 "pytz",
7 "setuptools",
8 "simplejson>=2.1.0",
9- "six>=1.13.0",
10 "testtools",
11 "van.testing",
12 "zope.component [zcml]",
13diff --git a/src/lazr/restful/debug.py b/src/lazr/restful/debug.py
14index ebcf526..3dfb865 100644
15--- a/src/lazr/restful/debug.py
16+++ b/src/lazr/restful/debug.py
17@@ -4,10 +4,9 @@
18
19 __all__ = ["debug_proxy", "typename"]
20
21+from io import StringIO
22
23-from six import StringIO
24 import zope.proxy
25-
26 from zope.security.checker import getChecker, Checker, CheckerPublic, Proxy
27
28
29diff --git a/src/lazr/restful/declarations.py b/src/lazr/restful/declarations.py
30index 0fca6c1..ecfcc00 100644
31--- a/src/lazr/restful/declarations.py
32+++ b/src/lazr/restful/declarations.py
33@@ -44,7 +44,6 @@ import copy
34 import itertools
35 import sys
36
37-import six
38 from zope.component import getUtility, getGlobalSiteManager
39 from zope.interface import classImplements
40 from zope.interface.interface import fromFunction, InterfaceClass, TAGGED_DATA
41@@ -1943,5 +1942,5 @@ def _versioned_class_name(base_name, version):
42 # runtime. Use a generic string that won't conflict with a
43 # real version string.
44 version = "__Earliest"
45- name = "%s_%s" % (base_name, six.ensure_str(version))
46+ name = "%s_%s" % (base_name, version)
47 return make_identifier_safe(name)
48diff --git a/src/lazr/restful/docs/webservice.rst b/src/lazr/restful/docs/webservice.rst
49index 411d9e8..d10d4a3 100644
50--- a/src/lazr/restful/docs/webservice.rst
51+++ b/src/lazr/restful/docs/webservice.rst
52@@ -1088,9 +1088,8 @@ top-level collections of authors, cookbooks, and dishes. It's the
53 'home page' for the web service.
54
55 >>> import simplejson
56- >>> import six
57 >>> response = app(request)
58- >>> representation = simplejson.loads(six.ensure_text(response))
59+ >>> representation = simplejson.loads(response)
60
61 >>> print(representation["authors_collection_link"])
62 http://api.cookbooks.dev/beta/authors
63@@ -1165,7 +1164,7 @@ parsed with standard tools.
64
65 >>> def load_json(s):
66 ... """Convert a JSON string to Unicode and then load it."""
67- ... return simplejson.loads(six.ensure_text(s))
68+ ... return simplejson.loads(s)
69
70 >>> representation = load_json(collection())
71 >>> print(representation['resource_type_link'])
72@@ -1678,7 +1677,9 @@ easily accessible data structure.
73 ... headers['HTTP_IF_NONE_MATCH'] = etag
74 ... get_request = create_web_service_request(
75 ... julia_url, environ=headers)
76- ... entity_body = six.ensure_text(get_request.traverse(app)())
77+ ... entity_body = get_request.traverse(app)()
78+ ... if isinstance(entity_body, bytes):
79+ ... entity_body = entity_body.decode()
80 ... return dict(entity_body=entity_body,
81 ... response=get_request.response)
82
83@@ -1820,7 +1821,7 @@ set the field value to 'tag:launchpad.net:2008:redacted'.
84 >>> body = simplejson.dumps(author).encode()
85 >>> put_request = create_web_service_request(
86 ... beard_url, body=body, environ=headers, method='PUT')
87- >>> print(six.ensure_text(put_request.traverse(app)()))
88+ >>> print(put_request.traverse(app)().decode())
89 {...}
90
91 And since no special permission is necessary to _change_ a person's
92@@ -1833,7 +1834,7 @@ PUT, even when its current value is redacted.
93 >>> body = simplejson.dumps(author).encode()
94 >>> put_request = create_web_service_request(
95 ... beard_url, body=body, environ=headers, method='PUT')
96- >>> print(six.ensure_text(put_request.traverse(app)()))
97+ >>> print(put_request.traverse(app)().decode())
98 {...}
99
100 After that PUT, James Beard's 'favorite_recipe' attribute is no longer
101@@ -1897,7 +1898,7 @@ A cookbook can be given a cover with PUT.
102 >>> file_resource = put_request.traverse(app)
103 >>> file_resource()
104
105- >>> print(six.ensure_str(C2.cover.representation))
106+ >>> print(C2.cover.representation.decode())
107 Pretend...
108
109 At this point it exists:
110@@ -1934,7 +1935,7 @@ An entry's primitive data fields are exposed as subordinate resources.
111
112 >>> field_resource = create_web_service_request(
113 ... '/beta/cookbooks/The%20Joy%20of%20Cooking/name').traverse(app)
114- >>> print(six.ensure_text(field_resource()))
115+ >>> print(field_resource().decode())
116 "The Joy of Cooking"
117
118 Requesting non available resources
119@@ -2059,7 +2060,10 @@ their URLs, we make the change by modifying the cookbook's
120 ... '/beta/cookbooks/The%20Joy%20of%20Cooking',
121 ... body=simplejson.dumps(representation).encode(),
122 ... environ=headers, method='PATCH', hostname=host).traverse(app)
123- ... return six.ensure_text(resource())
124+ ... result = resource()
125+ ... if isinstance(result, bytes):
126+ ... result = result.decode()
127+ ... return result
128 >>> path = '/beta/authors/Julia%20Child'
129
130 >>> print(change_joy_author('http://api.cookbooks.dev' + path))
131@@ -2122,7 +2126,7 @@ Now you see it...
132
133 >>> resource = create_web_service_request(
134 ... recipe_url, method='GET').traverse(app)
135- >>> print(six.ensure_text(resource()))
136+ >>> print(resource().decode())
137 {...}
138
139 >>> resource = create_web_service_request(
140diff --git a/src/lazr/restful/example/base/tests/collection.txt b/src/lazr/restful/example/base/tests/collection.txt
141index e156a43..44065aa 100644
142--- a/src/lazr/restful/example/base/tests/collection.txt
143+++ b/src/lazr/restful/example/base/tests/collection.txt
144@@ -325,8 +325,6 @@ POST operations may involve text fields. These are marshalled via a
145 multipart/form-data request, which requires line breaks to be represented as
146 CRLF. The server turns these into Unix-style LF.
147
148- >>> import six
149-
150 >>> def create_recipe(id, cookbook_link, dish_link, instructions,
151 ... private=False):
152 ... return webservice.named_post(
153@@ -344,6 +342,6 @@ CRLF. The server turns these into Unix-style LF.
154 >>> response.status
155 201
156 >>> recipe = webservice.get(response.getHeader("Location")).jsonBody()
157- >>> six.ensure_str(recipe["instructions"])
158+ >>> recipe["instructions"]
159 'Recipe\ncontaining\nsome\nline\n\nbreaks'
160 >>> _ = webservice.delete(recipe["self_link"])
161diff --git a/src/lazr/restful/example/base/tests/entry.txt b/src/lazr/restful/example/base/tests/entry.txt
162index f24c7a3..decab39 100644
163--- a/src/lazr/restful/example/base/tests/entry.txt
164+++ b/src/lazr/restful/example/base/tests/entry.txt
165@@ -118,9 +118,7 @@ a simple definition list.
166 Getting the XHTML representation works correctly even when some of the fields
167 have non-ascii values.
168
169- >>> import six
170- >>> print(six.text_type(
171- ... webservice.get(construsions_url, 'application/xhtml+xml')))
172+ >>> print(str(webservice.get(construsions_url, 'application/xhtml+xml')))
173 HTTP/1.1 200 Ok
174 ...
175 <dl ...>
176diff --git a/src/lazr/restful/testing/helpers.py b/src/lazr/restful/testing/helpers.py
177index c0dd63c..bfd8685 100644
178--- a/src/lazr/restful/testing/helpers.py
179+++ b/src/lazr/restful/testing/helpers.py
180@@ -1,7 +1,6 @@
181 import sys
182 from types import ModuleType
183
184-import six
185 from zope.configuration import xmlconfig
186 from zope.interface import implementer
187
188@@ -63,7 +62,7 @@ def encode_unicode(unicode_string):
189
190 :param unicode_string: A Unicode string.
191 """
192- return six.ensure_str(unicode_string.encode("ascii", "backslashreplace"))
193+ return unicode_string.encode("ascii", "backslashreplace").decode()
194
195
196 @implementer(IWebServiceConfiguration)
197diff --git a/src/lazr/restful/testing/webservice.py b/src/lazr/restful/testing/webservice.py
198index 97bbc11..af7496b 100644
199--- a/src/lazr/restful/testing/webservice.py
200+++ b/src/lazr/restful/testing/webservice.py
201@@ -34,7 +34,6 @@ from urllib.parse import (
202
203 import wsgi_intercept
204
205-import six
206 from zope.component import adapter, getGlobalSiteManager, getUtility
207 from zope.configuration import xmlconfig
208 from zope.interface import alsoProvides, implementer, Interface
209@@ -316,8 +315,8 @@ class WebServiceCaller:
210 headers=None,
211 api_version=None,
212 ):
213- if isinstance(path_or_url, (bytes, str)):
214- path_or_url = six.ensure_str(path_or_url)
215+ if isinstance(path_or_url, bytes):
216+ path_or_url = path_or_url.decode()
217 else:
218 path_or_url = str(path_or_url)
219 if path_or_url.startswith("/"):
220@@ -459,9 +458,9 @@ class WebServiceCaller:
221 value = simplejson.dumps(value)
222 lines = re.split(r"\r\n|\r|\n", value)
223 for line in lines[:-1]:
224- buf.write(six.ensure_binary(line))
225+ buf.write(line.encode())
226 buf.write(b"\r\n")
227- buf.write(six.ensure_binary(lines[-1]))
228+ buf.write(lines[-1].encode())
229 encoded_parts.append(buf.getvalue())
230
231 # Create a suitable boundary.
232@@ -580,7 +579,9 @@ class WebServiceResponseWrapper(ProxyBase):
233
234 def jsonBody(self):
235 """Return the body of the web service request as a JSON document."""
236- body = six.ensure_text(self.body)
237+ body = self.body
238+ if isinstance(body, bytes):
239+ body = body.decode()
240 try:
241 return simplejson.loads(body, object_pairs_hook=OrderedDict)
242 except ValueError:

Subscribers

People subscribed via source and target branches