pythonnonetypegithub3.py

Why does my repository object return Nonetype with github3.py?


Using github3.py version 0.9.5 documentation, I'm trying to create a repository object but it keeps returning Nonetype and therefore I am unable to access the contents of the repository. There doesn't seem to be any other posts on StackOverflow, or conversations on the library's GitHub issues that address this problem.

AttributeError: 'NoneType' object has no attribute 'contents' is the exact error I received.

On the line that says repo = repository('Django', auth) I tried changing auth with fv4 but that doesn't change anything other.

#!/usr/bin/env python

from github3 import authorize, repository, login
from pprint import PrettyPrinter as ppr
import github3
from getpass import getuser

pp = ppr(indent=4)


username = 'myusername'
password = 'mypassword'
scopes = ['user', 'repo', 'admin:public_key', 'admin:repo_hook']
note = 'github3.py test'
note_url = 'http://github.com/FreddieV4'

print("Attemping authorization...")


token = id = ''
with open('CREDENTIALS.txt', 'r') as fi:
    token = fi.readline().strip()
    id = fi.readline().strip()


print("AUTH token {}\nAUTH id {}\n".format(token, id))


print("Attempting login...\n")
fv4 = login(username, password, token=token)
print("Login successful!", str(fv4), '\n')


print("Attempting auth...\n")
auth = fv4.authorization(id)
print("Auth successful!", auth, '\n')


print("Reading repo...\n")
repo = repository('Django', auth)
print("Repo object...{}\n\n".format(dir(repo)))


print("Repo...{}\n\n".format(repo))
contents = repo.contents('README.md')


pp.pprint('CONTENTS {}'.format(contents))


contents.update('Testing github3.py', contents)

#print("commit: ", commit)

Solution

  • So there are a few things up with your code, but let me help you with your immediate problem first and then I'll move on to the other issues.

    You're using github3.repository in the line you're confused about. Let's look at the documentation for that specific function (which you can also see by calling help(repository)). You'll see that repository expects two arguments owner and repository and describes them as the owner of the repository and the name of the repository itself. So in your usage you would do

    repo = repository('Django', 'Django')
    

    But where does that leave your authentication credentials... Well here's the other thing, you're doing

    fv4 = login(username, password, token)
    

    You only need to specify some of those arguments. If you want to use a token then do

    fv4 = login(token=token)
    

    Or if you want to use basic authentication

    fv4 = login(username, password)
    

    Both will work just fine. If you want to continue to be authenticated you can then do

    repo = fv4.repository('Django', 'Django')
    

    Because fv4 is a GitHub object which is documented here and which the repository function uses underneath everything.

    So that should help you get through most of your problems.


    Note that in the documented examples for github3.py we usually call the result of login() gh. This is because gh is just a GitHub object with credentials stored. It isn't your user or anything like that. That would be (on your version of github3.py) fv4 = gh.user(). (If someone else is reading this and using a version of github3.py 1.0 (currently in pre-release) then it would be fv4 = gh.me().)