Skip to content

Empty search results iterate on dictionary keys instead of an empty list #128

@jstrot

Description

@jstrot

Since commit e038ac5 , when a search result is empty, iteration of the results instead iterates on the dictionary keys.

Searching for "Matrix", like in search_movies.py, returns 5 results that can be successfully iterated:

>>> from tmdbv3api import TMDb, Search
>>> tmdb = TMDb()
>>> tmdb.api_key = "..."
>>> search = Search()
>>> results = search.movies("Matrix", year=1999)
>>> results
{'page': 1, 'results': [...], 'total_pages': 1, 'total_results': 5}
>>> len(results)
5
>>> results[0]
{'adult': False, 'backdrop_path': '/ncEsesgOJDNrTUED89hYbA117wo.jpg', 'genre_ids': [28, 878], 'id': 603, 'original_language': 'en', 'original_title': 'The Matrix', 'overview': 'Set in the 22nd century, The Matrix tells the story of a computer hacker who joins a group of underground insurgents fighting the vast and powerful computers who now rule the earth.', 'popularity': 101.802, 'poster_path': '/f89U3ADr1oiB1s9GkdPOEpXUk5H.jpg', 'release_date': '1999-03-31', 'title': 'The Matrix', 'video': False, 'vote_average': 8.217, 'vote_count': 25376}

But when the query returns no valid results, it cannot be iterated directly:

>>> results = search.movies("something not found", year=1999)
>>> results
{'page': 1, 'results': {}, 'total_pages': 1, 'total_results': 0}
>>> len(results)
4
>>> list(results)
['page', 'results', 'total_pages', 'total_results']
>>> results[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jst/tools/venv/lib/python3.12/site-packages/tmdbv3api/as_obj.py", line 47, in __getitem__
    return getattr(self, key)
           ^^^^^^^^^^^^^^^^^^
TypeError: attribute name must be string, not 'int'

Expected behavior:

>>> results = search.movies("something not found", year=1999)
>>> results
{'page': 1, 'results': {}, 'total_pages': 1, 'total_results': 0}
>>> len(results)
0
>>> list(results)
[]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions