pythonindexinggithub3.py

TypeError: 'GitHubIterator' object does not support indexing


Using github3.py, I want to retrieve the last comment in the list of comments associated with a pull request and then search it for a string. I've tried the code below, but I get the error TypeError: 'GitHubIterator' object does not support indexing (without indexing, I can retrieve the list of comments).

for comments in list(GitAuth.repo.issue(prs.number).comments()[-1]):
    if sign_off_regex_search_string.search(comments.body) and comments.user.login == githubID:
        sign_off_by_author_search_string_found = 'True'
        break

Solution

  • I'm pretty sure the first line of your code doesn't do what you want. You're trying to index (with [-1]) an object that doesn't support indexing (it is some kind of iterator). You've also go a list call wrapped around it, and a loop running on that list. I think you don't need the loop. Try:

    comments = list(GitAuth.repo.issue(prs.number).comments())[-1]
    

    I've moved the closing parenthesis from the list call to come before the indexing. This means the indexing happens on the list, rather than on the iterator. It does however waste a bit of memory, since all the comments get stored in a list before we index the last one and throw the list away. If memory usage is a concern, you could bring back the loop and get rid of the list call:

    for comments in GitAuth.repo.issue(prs.number).comments():
        pass # the purpose of this loop is to get the last `comments` value
    

    The rest of the code should not be inside this loop. The loop variable comments (which should probably be comment, since it refers to a single item) will remain bound to the last value from the iterator after the loop ends. That's what you want to do your search on.