Home > Net >  Missing 1 required positional argument - Python
Missing 1 required positional argument - Python

Time:12-21

I use gitlab-python module for get all commits from all branches in list of repository

When I try to get list of branches from projects I receive error

TypeError: get_branches() missing 1 required positional argument: 'project'

import gitlab

gl = gitlab.Gitlab(url="https://gitlab.site.com", private_token="token")

projects_list = ['23', '218', '239', '246', '245', '265']

def get_projects():
    for project_id in projects_list:
        project = gl.projects.get(project_id)
        return (project)
        
def get_branches(project):
    branches = project.branches.list(get_all=True, all=True)
    for branch in branches:
        print(branch)

def main():
    get_projects()
    get_branches()

main()

How can I provide arguments for changes to fix it?

CodePudding user response:

get_projects() should return a list of projects, not just the first project in the loop.

You need to pass a project to get_branches(). You can do this by looping over the results of get_projects().

import gitlab

gl = gitlab.Gitlab(url="https://gitlab.site.com", private_token="token")

projects_list = ['23', '218', '239', '246', '245', '265']

def get_projects():
    return [gl.projects.get(project_id) for project_id in projects_list]
        
def get_branches(project):
    branches = project.branches.list(get_all=True, all=True)
    for branch in branches:
        print(branch)

def main():
    for project in get_projects():
        get_branches(project)

main()

CodePudding user response:

A shorter version of your code

import gitlab

gl = gitlab.Gitlab(url="https://gitlab.site.com", private_token="token")

projects_list = ['23', '218', '239', '246', '245', '265']


def main():
    projects = [gl.projects.get(project_id) for project_id in projects_list]
    branches = {str(p): p.branches.list(get_all=True, all=True) for p in projects}


main()

CodePudding user response:

As it is obvious in the definition of the function get_branches(project); you need to pass it a project as an argument. You may write a loop which iterates over all projects returned by get_projects() and for each project you can use get_branches(project) (given the project as an argument).

for project in get_projects():
    get_branches(project)
  • Related