Skip to content

AttributeError: 'str' object has no attribute 'branches' #2003

Description

@binbjz

Description of the problem, including code/CLI snippet

Expected Behavior

project should be callable object

Actual Behavior

AttributeError: 'str' object has no attribute 'branches'

Specifications

  • python version: 3.10.4
  • python-gitlab version: 3.4.0
  • API version you are using (v3/v4): v4
  • Gitlab server version (or gitlab.com): GitLab Community Edition [13.2.4]
def get_all_projects(gl: Gitlab, all_flag: bool = True) -> Generator:
    if not gl:
        raise Exception("Pls specify gl srv conn.")
    projects = gl.projects.list(all=all_flag, as_list=False, max_retries=12)
    return projects
def cl_all_br_fmt_tmpl(prj: RESTObject, s_time: str, e_time: str, prj_lst: Optional[list] = None):
    if not prj_lst:
        prj_lst = []

    for branch in prj.branches.list(all=True):
        commits = prj.commits.list(all=True,
                                   query_parameters={"since": s_time,
                                                     "until": e_time,
                                                     "ref_name": branch.name})
        for commit in commits:
            if "Merge" not in commit.title:
                com = prj.commits.get(commit.id)
                _dict = {"committed_project_path": prj.path_with_namespace,
                         "committed_author": com.author_name,
                         "committed_branch": branch.name,
                         "committed_email": com.committer_email,
                         "committed_additions": com.stats["additions"],
                         "committed_deletions": com.stats["deletions"],
                         "committed_nums": com.stats["total"],
                         "committed_date": com.committed_date[:10]}
                prj_lst.append(_dict)
    return prj_lst

First way - works fine (Multiple processes are not used)

def cl_all_br_fmt(gl: Gitlab, s_time: str, e_time: str):
    if not gl:
        raise Exception("Pls specify gl srv conn.")

    projects = get_all_projects(gl)
        yield cl_all_br_fmt_tmpl(project, s_time, e_time)

Second way --> An exception is thrown (Multiple processes are used)

for branch in prj.branches.list(all=True): --> AttributeError: 'str' object has no attribute 'branches'

def cl_all_br_fmt(gl: Gitlab, s_time: str, e_time: str):
    if not gl:
        raise Exception("Pls specify gl srv conn.")

    projects = get_all_projects(gl)
    pool_size = multiprocessing.cpu_count() * 3
    pool = multiprocessing.Pool(
        processes=pool_size,
    )
    p_func = partial(cl_all_br_fmt_tmpl, s_time, e_time)
    result_data = pool.map(p_func, projects)
    return result_data

Execute the program to call cl_all_br_fmt

gl = Gitlab(url=gl_url, private_token=gl_token, retry_transient_errors=True, timeout=30.0)
for itr in cl_all_br_fmt(gl, s_time, e_time):
    for i in itr:
        print(i)

Why can the following method be called ?project.branches

projects = gl.projects.list(all=True, as_list=False, max_retries=12)
for project in projects:
    for branch in project.branches.list(all=True):

Why is this object a string type and it's not callable

How can I fix it ? I need to use multiple processes to speed up processing jobs

Please help me to fix this issue

pool_size = multiprocessing.cpu_count() * 3
pool = multiprocessing.Pool(
    processes=pool_size,
)
p_func = partial(cl_all_br_fmt_tmpl, s_time, e_time)
projects = gl.projects.list(all=True, as_list=False, max_retries=12)
pool.map(p_func, projects)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions