canvasapi icon indicating copy to clipboard operation
canvasapi copied to clipboard

Parse Nested Objects

Open liblit opened this issue 7 years ago • 2 comments

Original title was "Canvas.get_assignment_group should turn assignments into Assignment instances"

A call to Course.get_assignment_group can optionally include full details about the assignments in the group. That is a nice way to reduce round-trips if we already know that the details will be needed.

In version 0.6.0 of canvasapi, these assignment details are provided as a list of dict.

group = course.get_assignment_group(group.id, include='assignments')
for assignment in group.assignments:
    print(assignment)  # prints each assignment as a dict

I was expecting them to be a list of Assignment instances. Each dict seemingly has all of the details needed to build an Assignment, and I can do that conversion myself:

group = course.get_assignment_group(group.id, include='assignments')
assignments = [Assignment(group._requester, raw) for raw in group.assignments]
for assignment in assignments:
    print(assignment)  # prints each assignment as an Assignment

However, accessing group._requester directly seems like cheating. And I would have expected canvasapi to take care of such details behind the scenes. Can that be done? Can Course.get_assignment_group convert each dict into a proper Assignment for me? (Obviously it should only do so if assignment details are present; without include='assignments', no details are given, so there is nothing to convert.

liblit avatar Aug 25 '17 16:08 liblit

Still using canvasapi 0.6.0, calling Course.get_assignment_group with discussion_topic on the include[] list returns Assignment instances have discussion_topic attributes. The values of these attributes are simple Python dict instances but should be DiscussionTopic instances. So this is essentially the same problem, but in a new context. Should I file a new issue for this new context, or do you want to bundle that in with the issue I originally reported here?

liblit avatar Sep 17 '17 23:09 liblit

I think bundling these issues together makes sense. This is essentially the issue for turning nested objects into actual objects instead of dict. I'll edit the title to reflect this.

Thetwam avatar Sep 18 '17 14:09 Thetwam