diff --git a/canvasapi/canvas_object.py b/canvasapi/canvas_object.py index 65d7679f..54b6ed4e 100644 --- a/canvasapi/canvas_object.py +++ b/canvasapi/canvas_object.py @@ -1,3 +1,5 @@ +import warnings + import arrow import pytz @@ -11,6 +13,15 @@ class CanvasObject(object): """ def __getattribute__(self, name): + if name == "content-type": + warnings.warn( + ( + "The 'content-type' attribute will be removed " + "in a future version. Please use " + "'content_type' instead." + ), + DeprecationWarning, + ) return super(CanvasObject, self).__getattribute__(name) def __init__(self, requester, attributes): @@ -60,6 +71,8 @@ def set_attributes(self, attributes): """ for attribute, value in attributes.items(): self.__setattr__(attribute, value) + if attribute == "content-type": + self.__setattr__("content_type", value) try: naive = arrow.get(str(value)).datetime diff --git a/tests/test_canvas_object.py b/tests/test_canvas_object.py index 0a3b176c..bad1d599 100644 --- a/tests/test_canvas_object.py +++ b/tests/test_canvas_object.py @@ -1,4 +1,5 @@ import unittest +import warnings from datetime import datetime import pytz @@ -62,3 +63,53 @@ def test_set_attributes_invalid_date(self, m): self.assertFalse(hasattr(self.canvas_object, "end_at_date")) self.assertTrue(hasattr(self.canvas_object, "start_at")) self.assertTrue(hasattr(self.canvas_object, "end_at")) + + # set_attributes 'content-type' + def test_set_attributes_with_content_type(self, m): + attributes = { + "content-type": "application/json", + "content_type": "another_application/json", + "filename": "example.json", + } + + self.canvas_object.set_attributes(attributes) + + self.assertTrue(hasattr(self.canvas_object, "content-type")) + self.assertEqual( + getattr(self.canvas_object, "content-type"), "application/json" + ) + self.assertTrue(hasattr(self.canvas_object, "content_type")) + self.assertEqual(self.canvas_object.content_type, "another_application/json") + self.assertTrue(hasattr(self.canvas_object, "filename")) + self.assertEqual(self.canvas_object.filename, "example.json") + + def test_set_attributes_with_content_type_reversed(self, m): + # Reversed the order of the attributes to test overwrite behavior + attributes = { + "content_type": "another_application/json", + "content-type": "application/json", + "filename": "example.json", + } + + self.canvas_object.set_attributes(attributes) + + self.assertTrue(hasattr(self.canvas_object, "content-type")) + self.assertEqual( + getattr(self.canvas_object, "content-type"), "application/json" + ) + self.assertTrue(hasattr(self.canvas_object, "content_type")) + self.assertEqual(self.canvas_object.content_type, "another_application/json") + self.assertTrue(hasattr(self.canvas_object, "filename")) + self.assertEqual(self.canvas_object.filename, "example.json") + + def test__getattribute__content_type_warns(self, m): + attributes = {"content-type": "application/json"} + self.canvas_object.set_attributes(attributes) + + warnings.simplefilter("always", DeprecationWarning) + + with warnings.catch_warnings(record=True) as warning_list: + self.canvas_object.__getattribute__("content-type") + + self.assertEqual(len(warning_list), 1) + self.assertEqual(warning_list[0].category, DeprecationWarning)