Doist/todoist-api-python

The Task "from_dict" constructor should populate "section_id" with None by default using ".get"

michael-genson opened this issue · 0 comments

Bug description

Optional fields in the "from_dict" constructor for each object type use .get such that, if the field is missing, it's populated with None. The section_id on class Task does not do this, even though it should default to None.

It seems that the Todoist API always returns a null section_id so this doesn't matter in practice, but if you're mocking the API (for testing) it's a minor inconvenience.

Expected behaviour

If a Task is constructed from a dict that does not have a section_id, it should set section_id to None.

Is reproducible

Yes

To reproduce

# this raises a KeyError due to a missing section_id
task = Task.from_dict(
    {
        "assigner_id": "abc123",
        "assignee_id": "abc123",
        "comment_count": 0,
        "is_completed": False,
        "content": "def234",
        "created_at": "2023-04-24T00:00:00.0000Z",
        "creator_id": "abc123",
        "description": "",
        "labels": [],
        "order": 1,
        "priority": 1,
        "url": "https://github.com/",
    }
)

Steps taken to try to reproduce

N/A, the fix is easy, you can simply replace section_id=obj["section_id"], with section_id=obj.get("section_id") (or, as a user, just supply a null section_id, of course)

Screenshots

N/A

Version information:

  • Package version: v2.0.2
  • Python version: 3.9

Additional information

See https://github.com/Doist/todoist-api-python/blob/main/todoist_api_python/models.py#L158