Repository: ucfopen/canvasapi
Pull request #342 (2019-11-18 20:44:10)

Deprecate CanvasObject.attributes and CanvasOject.to_json()
Related: #341

coveralls: 
[![Coverage Status](https://coveralls.io/builds/27065348/badge)](https://coveralls.io/builds/27065348)

Coverage remained the same at 100.0% when pulling **1227e1a05961ae16c0bd1ef8ee0fa60685c8486b on Thetwam:issue/341-deprecate-attributes** into **8bd85663a2a91cf2ec37de431257ab80ce8e7d57 on ucfopen:develop**.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b7d0992..62ec157 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,11 @@
 
 - Throw `IndexError` when using negative indexes on `PaginatedList` objects (Thanks, [@UniversalSuperBox](https://github.com/UniversalSuperBox))
 
+### Deprecation Warnings
+
+- `CanvasObject.attributes` is now deprecated and will be removed in a future version.
+- `CanvasObject.to_json()` is now deprecated and will be removed in a future version. To view the original attributes sent by Canvas, enable logs from the requests library.
+
 ### Bugfixes
 
 - Fixed an issue where `util.clean_headers()` would throw a `ValueError` if a user accidentally included a space in their API token. (Thanks, [@keeeeeegan](https://github.com/keeeeeegan))
diff --git a/canvasapi/canvas_object.py b/canvasapi/canvas_object.py
index 184c26f..96ef03b 100644
--- a/canvasapi/canvas_object.py
+++ b/canvasapi/canvas_object.py
@@ -1,8 +1,10 @@
 from __future__ import absolute_import, division, print_function, unicode_literals
+
 from datetime import datetime
 import json
 import pytz
 import re
+import warnings
 
 from six import text_type
 
@@ -17,6 +19,14 @@ class CanvasObject(object):
     to dynamically construct this object's attributes with a JSON object.
     """
 
+    def __getattribute__(self, name):
+        if name == "attributes":
+            warnings.warn(
+                "CanvasObject.attributes is deprecated and will be removed in a future version.",
+                DeprecationWarning,
+            )
+        return super(CanvasObject, self).__getattribute__(name)
+
     def __init__(self, requester, attributes):
         """
         :param requester: The requester to pass HTTP requests through.
@@ -77,5 +87,16 @@ class CanvasObject(object):
         """
         Return the original JSON response from the API that was used to
         construct the object.
+
+        .. warning::
+            .. deprecated:: 0.15.0
+                To view the original attributes sent by Canvas, enable logs from the
+                requests library.
         """
+        warnings.warn(
+            "`CanvasObject.to_json()` is deprecated and will be removed in a future version. "
+            "To view the original attributes sent by Canvas, enable logs from the "
+            "requests library",
+            DeprecationWarning,
+        )
         return json.dumps(self.attributes)


Test patch:
diff --git a/tests/test_canvas_object.py b/tests/test_canvas_object.py
index 6e3266d..405f295 100644
--- a/tests/test_canvas_object.py
+++ b/tests/test_canvas_object.py
@@ -1,5 +1,6 @@
 from __future__ import absolute_import, division, print_function, unicode_literals
 import unittest
+import warnings
 
 from canvasapi.canvas_object import CanvasObject
 
@@ -8,13 +9,22 @@ class TestCanvasObject(unittest.TestCase):
 
     # to_json()
     def test_canvas_object_to_json(self):
-        attributes = {"name": "Test Object", "id": 1}
-        canvas_obj = CanvasObject(None, attributes)
+        warnings.simplefilter("always", DeprecationWarning)
 
-        prev_json = canvas_obj.to_json()
-        self.assertIsInstance(prev_json, str)
+        with warnings.catch_warnings(record=True) as warning_list:
+            attributes = {"name": "Test Object", "id": 1}
+            canvas_obj = CanvasObject(None, attributes)
 
-        attributes.update({"name": "Test Object 2"})
-        canvas_obj.set_attributes(attributes)
+            prev_json = canvas_obj.to_json()
+            self.assertIsInstance(prev_json, str)
 
-        self.assertNotEqual(canvas_obj.to_json(), prev_json)
+            attributes.update({"name": "Test Object 2"})
+            canvas_obj.set_attributes(attributes)
+
+            self.assertNotEqual(canvas_obj.to_json(), prev_json)
+
+        self.assertEqual(len(warning_list), 4)
+        self.assertEqual(warning_list[0].category, DeprecationWarning)
+        self.assertEqual(warning_list[1].category, DeprecationWarning)
+        self.assertEqual(warning_list[2].category, DeprecationWarning)
+        self.assertEqual(warning_list[3].category, DeprecationWarning)