sarumont / py-trello

Python API wrapper around Trello's API
BSD 3-Clause "New" or "Revised" License
945 stars 330 forks source link

Fix card.badges being unset for cards created by from_json #284

Closed delucks closed 5 years ago

delucks commented 5 years ago

This PR fixes a bug with cards created by the from_json static method. The badges field of the object wasn't being set, which raises exceptions when lazy-loading comments or attachments later on.

This should fix issue #257.

Here's a small script to illustrate the problem:

import trello
client = trello.TrelloClient(...)
first_board = client.list_boards('open')[0]
cards = first_board.get_cards('open')
first_card = cards[0]

# Either one triggers the bug
first_card.attachments
first_card.comments

and the results for each:

$ python repro.py
Traceback (most recent call last):
  File "repro.py", line 13, in <module>
    first_card.comments
  File "/home/delucks/dev/scratch/lib/python3.6/site-packages/trello/card.py", line 76, in comments
    self._comments = self.fetch_comments()
  File "/home/delucks/dev/scratch/lib/python3.6/site-packages/trello/card.py", line 223, in fetch_comments
    if (force is True) or (self.badges['comments'] > 0):
AttributeError: 'Card' object has no attribute 'badges'
$ python repro.py
Traceback (most recent call last):
  File "repro.py", line 13, in <module>
    first_card.attachments
  File "/home/delucks/dev/scratch/lib/python3.6/site-packages/trello/card.py", line 103, in attachments
    self._attachments = self.fetch_attachments()
  File "/home/delucks/dev/scratch/lib/python3.6/site-packages/trello/card.py", line 265, in fetch_attachments
    if (force is True) or (self.badges['attachments'] > 0):
AttributeError: 'Card' object has no attribute 'badges'

After the patch, the script runs cleanly.