Skip to content

Commit 276f6d8

Browse files
committed
Fix linter
1 parent c7db44c commit 276f6d8

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

src/firebase_functions/https_fn.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class CallableRequest(_typing.Generic[_core.T]):
355355
class _IterWithReturn:
356356
def __init__(self, iterable):
357357
self.iterable = iterable
358-
358+
359359
def __iter__(self):
360360
self.value = yield from self.iterable
361361

@@ -410,11 +410,11 @@ def _on_call_handler(func: _C2, request: Request,
410410
result = _core._with_init(func)(context)
411411
if not _inspect.isgenerator(result):
412412
return _jsonify(result=result)
413-
413+
414414
if request.headers.get("Accept") != "text/event-stream":
415415
vals = _IterWithReturn(result)
416-
for _ in vals:
417-
next
416+
# Consume and drop yielded results
417+
list(vals)
418418
return _jsonify(result=vals.value)
419419

420420
else:
@@ -433,16 +433,20 @@ def _on_call_handler(func: _C2, request: Request,
433433

434434

435435
def _sse_encode_generator(gen: _typing.Generator):
436-
iter = _IterWithReturn(gen)
436+
with_return = _IterWithReturn(gen)
437437
try:
438-
for chunk in iter:
439-
yield f"data: %s\n\n" % _json.dumps(obj={"message": chunk})
440-
yield f"data: %s\n\n" % _json.dumps(obj={"result": iter.value})
438+
for chunk in with_return:
439+
data = _json.dumps(obj={"message": chunk})
440+
yield f"data: {data}\n\n"
441+
result = _json.dumps({"result": with_return.value})
442+
yield f"data: {result}\n\n"
443+
# pylint: disable=broad-except
441444
except Exception as err:
442445
if not isinstance(err, HttpsError):
443446
_logging.error("Unhandled error: %s", err)
444447
err = HttpsError(FunctionsErrorCode.INTERNAL, "INTERNAL")
445-
yield f"error: %s\n\n" % _json.dumps(obj={"error": err._as_dict()})
448+
json = _json.dumps(obj={"error": err._as_dict()})
449+
yield f"error: {json}\n\n"
446450
yield "END"
447451

448452
@_util.copy_func_kwargs(HttpsOptions)

tests/test_https_fn.py

+39-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import unittest
6-
from flask import Flask, Request, jsonify as _jsonify
6+
from flask import Flask, Request
77
from werkzeug.test import EnvironBuilder
88

99
from firebase_functions import core, https_fn
@@ -71,13 +71,14 @@ def func(_):
7171

7272
self.assertEqual("world", hello)
7373

74+
7475
def test_callable_encoding(self):
7576
app = Flask(__name__)
7677

7778
@https_fn.on_call()
7879
def add(req: https_fn.CallableRequest[int]):
7980
return req.data + 1
80-
81+
8182
with app.test_request_context("/"):
8283
environ = EnvironBuilder(
8384
method="POST",
@@ -90,18 +91,23 @@ def add(req: https_fn.CallableRequest[int]):
9091
response = add(request)
9192
self.assertEqual(response.status_code, 200)
9293
self.assertEqual(response.get_json(), { "result": 2 })
93-
94+
95+
9496
def test_callable_errors(self):
9597
app = Flask(__name__)
9698

9799
@https_fn.on_call()
98100
def throw_generic_error(req):
101+
# pylint: disable=broad-exception-raised
99102
raise Exception("Invalid type")
100103

101104
@https_fn.on_call()
102105
def throw_access_denied(req):
103-
raise https_fn.HttpsError(https_fn.FunctionsErrorCode.PERMISSION_DENIED, "Permission is denied")
104-
106+
raise https_fn.HttpsError(
107+
https_fn.FunctionsErrorCode.PERMISSION_DENIED,
108+
"Permission is denied"
109+
)
110+
105111
with app.test_request_context("/"):
106112
environ = EnvironBuilder(
107113
method="POST",
@@ -113,11 +119,18 @@ def throw_access_denied(req):
113119

114120
response = throw_generic_error(request)
115121
self.assertEqual(response.status_code, 500)
116-
self.assertEqual(response.get_json(), { "error": { "message": "INTERNAL", "status": "INTERNAL" } })
122+
self.assertEqual(response.get_json(), {
123+
"error": { "message": "INTERNAL", "status": "INTERNAL" }
124+
})
117125

118126
response = throw_access_denied(request)
119127
self.assertEqual(response.status_code, 403)
120-
self.assertEqual(response.get_json(), { "error": { "message": "Permission is denied", "status": "PERMISSION_DENIED" }})
128+
self.assertEqual(response.get_json(), {
129+
"error": {
130+
"message": "Permission is denied",
131+
"status": "PERMISSION_DENIED"
132+
}
133+
})
121134

122135
def test_yielding_without_streaming(self):
123136
app = Flask(__name__)
@@ -130,7 +143,10 @@ def yielder(req: https_fn.CallableRequest[int]):
130143
@https_fn.on_call()
131144
def yield_thrower(req: https_fn.CallableRequest[int]):
132145
yield from range(req.data)
133-
raise https_fn.HttpsError(https_fn.FunctionsErrorCode.PERMISSION_DENIED, "Can't read anymore")
146+
raise https_fn.HttpsError(
147+
https_fn.FunctionsErrorCode.PERMISSION_DENIED,
148+
"Can't read anymore"
149+
)
134150

135151
with app.test_request_context("/"):
136152
environ = EnvironBuilder(
@@ -158,7 +174,9 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
158174
response = yield_thrower(request)
159175

160176
self.assertEqual(response.status_code, 403)
161-
self.assertEqual(response.get_json(), { "error": { "message": "Can't read anymore", "status": "PERMISSION_DENIED" }})
177+
self.assertEqual(response.get_json(), {
178+
"error": { "message": "Can't read anymore", "status": "PERMISSION_DENIED" }
179+
})
162180

163181

164182
def test_yielding_with_streaming(self):
@@ -190,7 +208,12 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
190208

191209
self.assertEqual(response.status_code, 200)
192210
chunks = list(response.response)
193-
self.assertEqual(chunks, ['data: {"message": 0}\n\n', 'data: {"message": 1}\n\n', 'data: {"result": "OK"}\n\n', "END"])
211+
self.assertEqual(chunks, [
212+
'data: {"message": 0}\n\n',
213+
'data: {"message": 1}\n\n',
214+
'data: {"result": "OK"}\n\n',
215+
"END"
216+
])
194217

195218
with app.test_request_context("/"):
196219
environ = EnvironBuilder(
@@ -208,4 +231,9 @@ def yield_thrower(req: https_fn.CallableRequest[int]):
208231

209232
self.assertEqual(response.status_code, 200)
210233
chunks = list(response.response)
211-
self.assertEqual(chunks, ['data: {"message": 0}\n\n', 'data: {"message": 1}\n\n', 'error: {"error": {"status": "INTERNAL", "message": "Throwing"}}\n\n', "END"])
234+
self.assertEqual(chunks, [
235+
'data: {"message": 0}\n\n',
236+
'data: {"message": 1}\n\n',
237+
'error: {"error": {"status": "INTERNAL", "message": "Throwing"}}\n\n',
238+
"END"
239+
])

0 commit comments

Comments
 (0)