Skip to content

Commit 4bda990

Browse files
authored
Merge pull request #716 from Thetwam/issue/715-obj_or_str-fix
obj_or_str fix #715
2 parents 77ff5b2 + 384b0e5 commit 4bda990

2 files changed

Lines changed: 31 additions & 24 deletions

File tree

canvasapi/util.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,37 +133,34 @@ def obj_or_id(parameter, param_name, object_types):
133133
raise TypeError(message)
134134

135135

136-
def obj_or_str(obj, attr, object_types):
136+
def obj_or_str(parameter, param_name, object_types):
137137
"""
138-
Accepts an object. If the object has the attribute, return the
138+
Accepts either an object or a string. If it is a string, return it directly.
139+
If it is an object and the object is of correct type, return the object's
139140
corresponding string. Otherwise, throw an exception.
140141
141-
:param obj: object from which to retrieve attribute
142-
:type obj: object
143-
:param attr: name of the attribute to retrieve
144-
:type attr: str
142+
:param parameter: object from which to retrieve attribute
143+
:type parameter: str or object
144+
:param param_name: name of the attribute to retrieve
145+
:type param_name: str
145146
:param object_types: tuple containing the types of the object being passed in
146147
:type object_types: tuple
147148
:rtype: str
148149
"""
149-
try:
150-
return str(getattr(obj, attr))
151-
except (AttributeError, TypeError):
152-
if not isinstance(attr, str):
153-
raise TypeError(
154-
"Atttibute parameter {} must be of type string".format(attr)
155-
)
156-
for obj_type in object_types:
157-
if isinstance(obj, obj_type):
158-
try:
159-
return str(getattr(obj, attr))
160-
except AttributeError:
161-
raise AttributeError("{} object does not have {} attribute").format(
162-
obj, attr
163-
)
150+
if isinstance(parameter, str):
151+
return parameter
164152

165-
obj_type_list = ",".join([obj_type.__name__ for obj_type in object_types])
166-
raise TypeError("Parameter {} must be of type {}.".format(obj, obj_type_list))
153+
for obj_type in object_types:
154+
if isinstance(parameter, obj_type):
155+
try:
156+
return str(getattr(parameter, param_name))
157+
except AttributeError:
158+
raise AttributeError("{} object does not have {} attribute").format(
159+
parameter, param_name
160+
)
161+
162+
obj_type_list = ",".join([obj_type.__name__ for obj_type in object_types])
163+
raise TypeError("Parameter {} must be of type {}.".format(parameter, obj_type_list))
167164

168165

169166
def get_institution_url(base_url):

tests/test_util.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ def test_obj_or_id_nonuser_self(self, m):
431431
obj_or_id("self", "user_id", (CourseNickname,))
432432

433433
# obj_or_str()
434+
def test_obj_or_str_str(self, m):
435+
name = obj_or_str("test", "name", (User,))
436+
437+
self.assertIsInstance(name, str)
438+
self.assertEqual(name, "test")
439+
434440
def test_obj_or_str_obj_attr(self, m):
435441
register_uris({"user": ["get_by_id"]}, m)
436442

@@ -467,8 +473,12 @@ def test_obj_or_str_invalid_attr_parameter(self, m):
467473
obj_or_str(user, user, (User,))
468474

469475
def test_obj_or_str_invalid_obj_type(self, m):
476+
register_uris({"course": ["get_by_id"]}, m)
477+
478+
course = self.canvas.get_course(1)
479+
470480
with self.assertRaises(TypeError):
471-
obj_or_str("user", "name", (User,))
481+
obj_or_str(course, "name", (User,))
472482

473483
# get_institution_url()
474484
def test_get_institution_url(self, m):

0 commit comments

Comments
 (0)