From 94992071b1b9c6f828bb827b8be092146a514200 Mon Sep 17 00:00:00 2001 From: Kurt Griffiths Date: Mon, 15 Aug 2016 15:26:12 -0600 Subject: [PATCH] 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 --- falcon/request.py | 1 + falcon/response.py | 1 + tests/test_slots.py | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 tests/test_slots.py diff --git a/falcon/request.py b/falcon/request.py index 30a2bd3..dd49159 100644 --- a/falcon/request.py +++ b/falcon/request.py @@ -243,6 +243,7 @@ class Request(object): 'options', '_cookies', '_cached_access_route', + '__dict__', ) # Child classes may override this diff --git a/falcon/response.py b/falcon/response.py index 44d3d5d..fd25479 100644 --- a/falcon/response.py +++ b/falcon/response.py @@ -116,6 +116,7 @@ class Response(object): 'stream', 'stream_len', 'context', + '__dict__', ) # Child classes may override this diff --git a/tests/test_slots.py b/tests/test_slots.py new file mode 100644 index 0000000..665ea32 --- /dev/null +++ b/tests/test_slots.py @@ -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')