question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: object of type 'NoneType' has no len()

See original GitHub issue

Sentry Issue: SIR-LANCEBOT-8R

TypeError: object of type 'NoneType' has no len()
  File "discord/ext/commands/core.py", line 190, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot/exts/holidays/halloween/scarymovie.py", line 23, in random_movie
    selection = await self.select_movie()
  File "bot/exts/holidays/halloween/scarymovie.py", line 50, in select_movie
    selection_id = random.choice(data.get("results")).get("id")
  File "random.py", line 378, in choice
    return seq[self._randbelow(len(seq))]

Unhandled command error: object of type 'NoneType' has no len()

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
TizzySauruscommented, Oct 22, 2022

I don’t think there’s any reason it should be None so probably send a message to the user saying no results were found and maybe log a warning so we can look into it.

Yep, that sounds good.

If we used the results from the first request we’d be much more limited in the results we got.

Right, I see. For some reason I was thinking that the initial response would include all entries, which isn’t actually the case as you stated (it seems to return 20).

Side note: we may also want a fix like https://github.com/python-discord/sir-lancebot/issues/1055, it’s the same API and similar use case, just in a different place. It’s possible it isn’t an issue in practice for this genre if the total count isn’t high enough though.

It seems like a smart thing to do for future-proofing. Seems it needs to be limited to a max of 500 (not the 1000 stated on the API’s docs)?.

0reactions
wookie184commented, Oct 20, 2022

random.choice will error on an empty sequence, so we should handle it being None/an empty array before trying to do that. Right, that makes sense.

What do we actually want to do in the event that .get("results") or selection_id ends up being None?

I don’t think there’s any reason it should be None so probably send a message to the user saying no results were found and maybe log a warning so we can look into it.

In fact, reading through the code and API documentation, I’m not entirely sure why we’re not using data from the first GET request and indexing that instead of the second GET request(which is the one raising the error above). data should contain a list of results, which we can index and then get the id from for what’s currently the third GET request).

The first request is to get the number of pages so one can be selected for the request. If we used the results from the first request we’d be much more limited in the results we got.

Side note: we may also want a fix like https://github.com/python-discord/sir-lancebot/issues/1055, it’s the same API and similar use case, just in a different place. It’s possible it isn’t an issue in practice for this genre if the total count isn’t high enough though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python: TypeError: object of type 'NoneType' has no len()
Drop the assignment. This function returns None and that's why you have the error: TypeError: object of type 'NoneType' has no len().
Read more >
Python TypeError: object of type 'NoneType' has no len()
The “TypeError: object of type 'NoneType' has no len() ” error is caused when you try to use the len() method on an...
Read more >
TypeError: object of type 'NoneType' has no len() - Yawin Tutor
The TypeError: object of type 'NoneType' has no len() error occurs while attempting to find the length of an object that returns 'None'....
Read more >
TypeError: object of type 'NoneType' has no len() - STechies
This error is generated in Python when we try to calculate the length of an object which returns 'none'. Let us understand it...
Read more >
Solve Python TypeError: object of type 'NoneType' has no len()
The “TypeError: object of type 'NoneType' has no len()” error in Python occurs when you try to call the len function on an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found