GitPython: error: Module "git" does not explicitly export attribute "Repo" [attr-defined]

1.9k Views Asked by At

I am using Python 3.10.4, GitPython version 3.1.31, mypy version 1.4.1:

$ pip show GitPython
Name: GitPython
Version: 3.1.31
Location: /home/hakon/.pyenv/versions/3.10.4/lib/python3.10/site-packages
Requires: gitdb

$ python --version
Python 3.10.4

$ mypy --version
mypy 1.4.1 (compiled: yes)

If run mypy on this minimal example (git-python-types.py) :

import git
repo = git.Repo('some_dir')

I get the following error:

$ mypy --strict git-python-types.py 
git-python-types.py:3: error: Module "git" does not explicitly export attribute "Repo"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Any ideas on why this error occurs and how to fix it?

Some clues

I can see the following line in the GitPython source code :

from git.repo import Repo  # @NoMove @IgnorePep8

but I am not sure if mypy is reading this line or not.

2

There are 2 best solutions below

1
Mikael Öhman On BEST ANSWER

As documentation suggests this is proper usage

https://gitpython.readthedocs.io/en/stable/tutorial.html#meet-the-repo-type

from git import Repo
...

then I would consider this a minor bug that should be fixed in GitPython.

You can work around this bug by importing it from the submodule

git.repo.Repo('some_dir')

or don't use strict. The check it adds here is --no-implicit-reexport, which GitPython currently breaks.

2
Sebastian On

I would suggest to import it like the following example.

from git.repo import Repo

This is working in Neovim using pyright as LSP.