Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jstrot opened this issue Sep 7, 2024 · 0 comments
Open

Comments

@jstrot
Copy link

jstrot commented Sep 7, 2024

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)
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant