0

PyGithub does not appear to support the GitHub API call to acquire the latest release. I'm wondering if I can add a method to PyGithub at run time that would do this for me.

For example, the existing code has the following method:

# Repository.py
def get_releases(self):
    """
    :calls: `GET /repos/:owner/:repo/releases <http://developer.github.com/v3/repos>`_
    :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Tag.Tag`
    """
    return github.PaginatedList.PaginatedList(
        github.GitRelease.GitRelease,
        self._requester,
        self.url + "/releases",
        None
    )

I'd like to add this one to the Repository.py class:

def get_latest_release(self):
    return github.PaginatedList.PaginatedList(
        github.GitRelease.GitRelease,
        self._requester,
        self.url + "/releases/latest",
        None
    )

I tried this but I received an error:

# main.py

from types import MethodType
from github import Github
from github.Repository import Repository, github

def get_latest_release(self): ...

def main():
    Repository.method = MethodType(get_latest_release, None, Repository)
    g = Github(<my name>, <my password>)
    org = g.get_organization(<name of org>)
    repo = org.get_repo(<name of repository>)
    release = repo.get_latest_release()

    # AttributeError: 'Repository' object has no attribute 'get_latest_release'

Is it possible to add this method at runtime?

5
  • Why not create a child class of GitHub? Commented Mar 27, 2017 at 18:29
  • 1
    Why did you assign to Repository.method? Why method? Commented Mar 27, 2017 at 18:30
  • 1
    Wouldn't Repository.get_latest_release = get_latest_release be sufficient. You would only need MethodType if you were trying to assign to an instance, e.g. repo.get_lastest_release = MethodType(get_latest_release, repo) Commented Mar 27, 2017 at 18:34
  • DYZ, I could. I'm just seeing if this is possible. user2357112, I'm new to Python so this is likely incorrect. I'm just piecing together some information about adding unbound methods to a class. Commented Mar 27, 2017 at 18:34
  • 1
    Although also I wonder why you need it to be a method at all. Why not just a standalone function that takes the repo as its argument? Commented Mar 27, 2017 at 18:35

1 Answer 1

2

Assigning directly to Repository.get_latest_release should be sufficients, e.g.:

Repository.get_latest_release = get_latest_release

You would only need MethodType if you were trying to assign to an instance, e.g.

>>> import types
>>> class A(object):
...     pass
>>> a = A()
>>> A.one = lambda self: 1
>>> a.two = types.MethodType(lambda self: 2, a)
>>> a.one(), a.two()
(1, 2)

Where class assignment is available to all instances of that class, instance assignment is not:

>>> b = A()
>>> b.one()
1
>>> b.two()
AttributeError: 'A' object has no attribute 'two'
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank-you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.