3

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.

3
  • Do you mean that you don't get an attribute error if you run the code? Commented Jul 9, 2023 at 21:08
  • Yes the code runs without errors (using this command line: python git-python-types.py) Commented Jul 9, 2023 at 21:10
  • The tools are not omniscient. Especially when using --strict, false positives do occur. Commented Jul 9, 2023 at 21:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Yes running mypy like this works also: mypy --strict --implicit-reexport git-python-types.py
1

I would suggest to import it like the following example.

from git.repo import Repo

This is working in Neovim using pyright as LSP.

1 Comment

But surely this is not the intended way to import Repo.

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.