feat(request,response) add __dict__ to __slots__ (#868)
Add `__dict__` to the `__slots__` methods to the Request and Response classes to make it them extensible via subclassing and adding custom attributes. I have also added tests to make sure adding custom attributes do not break and raise an `AttributeError`. Fixes #785
This commit is contained in:
@@ -243,6 +243,7 @@ class Request(object):
|
|||||||
'options',
|
'options',
|
||||||
'_cookies',
|
'_cookies',
|
||||||
'_cached_access_route',
|
'_cached_access_route',
|
||||||
|
'__dict__',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Child classes may override this
|
# Child classes may override this
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ class Response(object):
|
|||||||
'stream',
|
'stream',
|
||||||
'stream_len',
|
'stream_len',
|
||||||
'context',
|
'context',
|
||||||
|
'__dict__',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Child classes may override this
|
# Child classes may override this
|
||||||
|
|||||||
22
tests/test_slots.py
Normal file
22
tests/test_slots.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from falcon import Request, Response
|
||||||
|
import falcon.testing as testing
|
||||||
|
|
||||||
|
|
||||||
|
class TestSlots(testing.TestBase):
|
||||||
|
|
||||||
|
def test_slots_request(self):
|
||||||
|
env = testing.create_environ()
|
||||||
|
req = Request(env)
|
||||||
|
|
||||||
|
try:
|
||||||
|
req.doesnt = 'exist'
|
||||||
|
except AttributeError:
|
||||||
|
self.fail('Unable to add additional variables dynamically')
|
||||||
|
|
||||||
|
def test_slots_response(self):
|
||||||
|
resp = Response()
|
||||||
|
|
||||||
|
try:
|
||||||
|
resp.doesnt = 'exist'
|
||||||
|
except AttributeError:
|
||||||
|
self.fail('Unable to add additional variables dynamically')
|
||||||
Reference in New Issue
Block a user