Merge ~cjwatson/lazr.restful:fix-more-flake8 into lazr.restful:main

Proposed by Colin Watson
Status: Merged
Merged at revision: 23ec6265b2c4965c47d7ebc1f02be989ee5e3ed7
Proposed branch: ~cjwatson/lazr.restful:fix-more-flake8
Merge into: lazr.restful:main
Diff against target: 322 lines (+64/-49)
5 files modified
src/lazr/restful/metazcml.py (+8/-2)
src/lazr/restful/tales.py (+0/-8)
src/lazr/restful/tests/test_error.py (+23/-5)
src/lazr/restful/tests/test_webservice.py (+16/-28)
tox.ini (+17/-6)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+412229@code.launchpad.net

Commit message

Fix several more problems noticed by flake8

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lazr/restful/metazcml.py b/src/lazr/restful/metazcml.py
2index 4e7ec93..1fe9422 100644
3--- a/src/lazr/restful/metazcml.py
4+++ b/src/lazr/restful/metazcml.py
5@@ -263,9 +263,15 @@ def find_exported_interfaces(module):
6 """
7 try:
8 module_all = set(module.__all__)
9- is_exported_name = lambda name: name in module_all
10+
11+ def is_exported_name(name):
12+ return name in module_all
13+
14 except AttributeError:
15- is_exported_name = lambda name: not name.startswith("_")
16+
17+ def is_exported_name(name):
18+ return not name.startswith("_")
19+
20 return (
21 interface
22 for name, interface in inspect.getmembers(
23diff --git a/src/lazr/restful/tales.py b/src/lazr/restful/tales.py
24index 24d7e45..2968667 100644
25--- a/src/lazr/restful/tales.py
26+++ b/src/lazr/restful/tales.py
27@@ -378,14 +378,6 @@ class WadlEntryResourceAPI(WadlResourceAPI):
28 def type_link(self):
29 return self.resource.type_url
30
31- @property
32- def fields_with_values(self):
33- """Return all of this entry's Field objects."""
34- fields = []
35- for name, field in getFieldsInOrder(self.schema):
36- fields.append({"field": field, "value": "foo"})
37- return fields
38-
39
40 class WadlCollectionResourceAPI(WadlResourceAPI):
41 "Namespace for WADL functions that operate on collection resources."
42diff --git a/src/lazr/restful/tests/test_error.py b/src/lazr/restful/tests/test_error.py
43index b667dbc..d407ccb 100644
44--- a/src/lazr/restful/tests/test_error.py
45+++ b/src/lazr/restful/tests/test_error.py
46@@ -17,9 +17,13 @@ from zope.component import getGlobalSiteManager
47 from zope.interface import Interface
48
49 from lazr.restful.declarations import error_status
50-from lazr.restful._resource import ReadWriteResource, UnknownEntryAdapter
51+from lazr.restful._resource import (
52+ EntryAdapterUtility,
53+ ReadWriteResource,
54+ UnknownEntryAdapter,
55+)
56 from lazr.restful.error import expose
57-from lazr.restful.interfaces import IWebServiceLayer
58+from lazr.restful.interfaces import IWebServiceLayer, IWebServiceVersion
59 from lazr.restful.testing.webservice import FakeRequest
60
61
62@@ -187,21 +191,35 @@ class ErrorsTestCase(unittest.TestCase):
63 def test_missing_adapter(self):
64 # If there is no multi-adapter from the entry interface (IMyEntry) and
65 # a request to IEntry an exception is raised.
66+ class IWebServiceTestRequestBeta(IWebServiceVersion):
67+ pass
68+
69 class IMyEntry(Interface):
70 pass
71
72+ sm = getGlobalSiteManager()
73+ sm.registerUtility(
74+ IWebServiceTestRequestBeta, IWebServiceVersion, name="beta"
75+ )
76+ self.addCleanup(
77+ sm.unregisterUtility,
78+ IWebServiceTestRequestBeta,
79+ IWebServiceVersion,
80+ name="beta",
81+ )
82+ beta_request = FakeRequest(version="beta")
83 # Since the test wants to inspect the exception message (below) we're
84 # not using self.assertRaises).
85 try:
86- EntryAdapterUtility.forSchemaInterface(IMyEntry, self.beta_request)
87+ EntryAdapterUtility.forSchemaInterface(IMyEntry, beta_request)
88 except Exception as e:
89 self.assertTrue(isinstance(e, Exception))
90
91 # The exception's message explains what went wrong.
92- self.assertTrue(
93- str(e),
94+ self.assertEqual(
95 "No IEntry adapter found for IMyEntry "
96 "(web service version: beta).",
97+ str(e),
98 )
99 else:
100 self.fail(
101diff --git a/src/lazr/restful/tests/test_webservice.py b/src/lazr/restful/tests/test_webservice.py
102index 399866a..4864150 100644
103--- a/src/lazr/restful/tests/test_webservice.py
104+++ b/src/lazr/restful/tests/test_webservice.py
105@@ -179,15 +179,11 @@ class EntryTestCase(WebServiceTestCase):
106 return request.publication.application.toWADL()
107
108 @contextmanager
109- def entry_resource(
110- self, entry_interface, entry_implementation, *implementation_args
111- ):
112+ def entry_resource(self, entry_implementation, *implementation_args):
113 """Create a request to an entry resource, and yield the resource."""
114- entry_class = get_resource_factory(entry_interface, IEntry)
115 data_object = entry_implementation(*implementation_args)
116
117 with self.request() as request:
118- entry = entry_class(data_object, request)
119 resource = EntryResource(data_object, request)
120 yield resource
121
122@@ -305,7 +301,7 @@ class TestEntryWebLink(EntryTestCase):
123
124 # Now a representation of IHasOneField includes a
125 # 'web_link'.
126- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
127+ with self.entry_resource(HasOneField, "") as resource:
128 representation = resource.toDataForJSON()
129 self.assertEqual(representation["self_link"], StubAbsoluteURL.URL)
130 self.assertEqual(
131@@ -340,7 +336,7 @@ class TestEntryWebLink(EntryTestCase):
132 # entry representations.
133 self._register_url_adapter(IHasOneField)
134
135- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
136+ with self.entry_resource(HasOneField, "") as resource:
137 representation = resource.toDataForJSON()
138 self.assertEqual(representation["self_link"], StubAbsoluteURL.URL)
139 self.assertFalse("web_link" in representation)
140@@ -376,9 +372,7 @@ class TestSuppressWebLink(EntryTestCase):
141 def test_entry_omits_web_link_when_suppressed(self):
142 self._register_website_url_space(IHasNoWebLink)
143
144- with self.entry_resource(IHasNoWebLink, HasNoWebLink, "") as (
145- resource
146- ):
147+ with self.entry_resource(HasNoWebLink, "") as resource:
148 representation = resource.toDataForJSON()
149 self.assertEqual(representation["self_link"], StubAbsoluteURL.URL)
150 self.assertFalse("web_link" in representation)
151@@ -442,7 +436,7 @@ class TestEntryWrite(EntryTestCase):
152 def test_applyChanges_rejects_nonexistent_web_link(self):
153 # If web_link is not published, applyChanges rejects a request
154 # that references it.
155- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
156+ with self.entry_resource(HasOneField, "") as resource:
157 errors = resource.applyChanges({"web_link": u"some_value"})
158 self.assertEqual(
159 errors,
160@@ -453,7 +447,7 @@ class TestEntryWrite(EntryTestCase):
161 """applyChanges rejects an attempt to change web_link ."""
162 self._register_website_url_space(IHasOneField)
163
164- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
165+ with self.entry_resource(HasOneField, "") as resource:
166 errors = resource.applyChanges({"web_link": u"some_value"})
167 self.assertEqual(
168 errors, "web_link: You tried to modify a read-only attribute."
169@@ -464,7 +458,7 @@ class TestEntryWrite(EntryTestCase):
170 # value isn't actually being changed.
171 self._register_website_url_space(IHasOneField)
172
173- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
174+ with self.entry_resource(HasOneField, "") as resource:
175 existing_web_link = resource.toDataForJSON()["web_link"]
176 representation = simplejson.loads(
177 resource.applyChanges({"web_link": existing_web_link})
178@@ -476,7 +470,7 @@ class TestEntryWrite(EntryTestCase):
179 # representation of the (unchanged) resource.
180 self._register_website_url_space(IHasOneField)
181
182- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
183+ with self.entry_resource(HasOneField, "") as resource:
184 existing_representation = resource.toDataForJSON()
185 representation = simplejson.loads(resource.applyChanges({}))
186 self.assertEqual(representation, existing_representation)
187@@ -489,7 +483,6 @@ class TestEntryWrite(EntryTestCase):
188 self._register_website_url_space(IHasFieldExportedAsDifferentName)
189
190 with self.entry_resource(
191- IHasFieldExportedAsDifferentName,
192 HasFieldExportedAsDifferentName,
193 u"initial value",
194 ) as resource:
195@@ -521,9 +514,7 @@ class TestEntryWriteForRestrictedField(EntryTestCase):
196 expose the right interface, it will raise an exception.
197 """
198 self._register_url_adapter(IHasRestrictedField)
199- with self.entry_resource(
200- IHasRestrictedField, HasRestrictedField, ""
201- ) as resource:
202+ with self.entry_resource(HasRestrictedField, "") as resource:
203 entry = resource.entry
204 entry.schema["a_field"].restrict_to_interface = IHasRestrictedField
205 self.assertEqual(entry.a_field, "")
206@@ -558,7 +549,7 @@ class HTMLRepresentationTest(EntryTestCase):
207
208 def test_entry_html_representation_is_utf8(self):
209 with self.entry_resource(
210- IHasOneField, HasOneField, self.unicode_message
211+ HasOneField, self.unicode_message
212 ) as resource:
213 html = resource.do_GET()
214 self.assertTrue(self.utf8_message in html)
215@@ -592,7 +583,6 @@ class JSONPlusHTMLRepresentationTest(EntryTestCase):
216 def resource(self, value_1="value 1", value_2="value 2"):
217 """Simplify the entry_resource call."""
218 with self.entry_resource(
219- IHasTwoFields,
220 HasTwoFields,
221 six.text_type(value_1),
222 six.text_type(value_2),
223@@ -721,9 +711,7 @@ class UnicodeErrorTestCase(EntryTestCase):
224 self._register_url_adapter(ICanBeSetToUnicodeValue)
225
226 def test_unicode_error(self):
227- with self.entry_resource(
228- ICanBeSetToUnicodeValue, CanBeSetToUnicodeValue, ""
229- ) as resource:
230+ with self.entry_resource(CanBeSetToUnicodeValue, "") as resource:
231
232 # This will raise an exception, which will cause the request
233 # to fail with a 400 error code.
234@@ -1135,7 +1123,7 @@ class NotificationsProviderTest(EntryTestCase):
235 @contextmanager
236 def resource(self):
237 """Simplify the entry_resource call."""
238- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
239+ with self.entry_resource(HasOneField, "") as resource:
240 yield resource
241
242 def test_response_notifications(self):
243@@ -1165,7 +1153,7 @@ class EventTestCase(EntryTestCase):
244 def test_event_fired_when_changeset_is_not_empty(self):
245 # Passing in a non-empty changeset spawns an
246 # IObjectModifiedEvent.
247- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
248+ with self.entry_resource(HasOneField, "") as resource:
249 resource.applyChanges({"a_field": u"Some value"})
250 events = eventtesting.getEvents()
251 self.assertEqual(len(events), 1)
252@@ -1176,7 +1164,7 @@ class EventTestCase(EntryTestCase):
253 def test_event_not_fired_when_changeset_is_empty(self):
254 # Passing in an empty changeset does not spawn an
255 # IObjectModifiedEvent.
256- with self.entry_resource(IHasOneField, HasOneField, "") as resource:
257+ with self.entry_resource(HasOneField, "") as resource:
258 resource.applyChanges({})
259 self.assertEqual(len(eventtesting.getEvents()), 0)
260
261@@ -1193,7 +1181,7 @@ class MalformedRequest(EntryTestCase):
262
263 def test_multiple_named_operations_generate_error_on_GET(self):
264 with self.entry_resource(
265- IHasOneField, HasOneField, self.unicode_message
266+ HasOneField, self.unicode_message
267 ) as resource:
268 resource.request.form["ws.op"] = ["foo", "bar"]
269 result = resource.do_GET()
270@@ -1202,7 +1190,7 @@ class MalformedRequest(EntryTestCase):
271
272 def test_multiple_named_operations_generate_error_on_POST(self):
273 with self.entry_resource(
274- IHasOneField, HasOneField, self.unicode_message
275+ HasOneField, self.unicode_message
276 ) as resource:
277 resource.request.form["ws.op"] = ["foo", "bar"]
278 result = resource.do_POST()
279diff --git a/tox.ini b/tox.ini
280index 8ea8d6d..4030fe5 100644
281--- a/tox.ini
282+++ b/tox.ini
283@@ -1,6 +1,14 @@
284 [tox]
285 envlist =
286- py27,py35,py36,py37,py38,py39,py310,docs
287+ lint
288+ py27
289+ py35
290+ py36
291+ py37
292+ py38
293+ py39
294+ py310
295+ docs
296
297 [testenv]
298 deps =
299@@ -9,6 +17,14 @@ deps =
300 commands =
301 zope-testrunner --test-path src --tests-pattern ^tests {posargs}
302
303+[testenv:lint]
304+basepython = python3.8
305+deps =
306+ pre-commit
307+skip_install = true
308+commands =
309+ pre-commit run -a
310+
311 [testenv:docs]
312 basepython = python3
313 deps =
314@@ -23,8 +39,3 @@ ignore =
315 # W503 and W504 are mutually exclusive
316 W503
317 W504
318- # todo
319- E731
320- F821
321- F841
322- W605

Subscribers

People subscribed via source and target branches