Skip to content

Commit 7545b80

Browse files
committed
Add __all__ in git.exc, adjust __init__.py imports
The git.exc module imports exceptions from gitdb.exc to republish them, as well as defining its own (also for use from outside). But because it did not define __all__, the intent for the exceptions it imported was unclear, since names that are introduced by imports and not present in __all__ are not generally considered public, even when __all__ is absent and a "*" import would reimport them. This rectifies that by adding __all__ and listing both imported and newly introduced exceptions explicitly in it. Although this strictly expands which names are public under typical conventions, it strictly contracts which names are imported by a "*" import, because the presence of __all__ suppresses names not listed in it from being imported that way. However, because under typical conventions those other names are not considered public, and they were not even weakly documented as public, this should be okay. (Even though this is not a breaking change, in that code it would break would already technically be broken... if it turns out that it is common to wrongly rely on the availabiliy of those names, then this may need to be revisited and slightly modified.) This brings the readily identified public interface of git.exc in line with what is weakly implied (and intended) by its docstring. This also modifies __init__.py accordingly: The top-level git module has for some time used a "*" import on git.exc, causing the extra names originally meant as implementation details to be included. Because its own __all__ was dynamically generated until c862845, #1659 also added 8edc53b to retain the formerly present names in __all__. So the change here imports those names from the modules that deliberately provide them, to preserve compatibility.
1 parent 8197e90 commit 7545b80

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

git/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
# @PydevCodeAnalysisIgnore
88
from git.exc import * # @NoMove @IgnorePep8
99
import os
10-
import sys
1110
import os.path as osp
11+
import sys
1212

13-
from typing import Optional
13+
from typing import List, Optional, Sequence, Tuple, Union, TYPE_CHECKING
1414
from git.types import PathLike
1515

1616
__version__ = "git"
@@ -38,7 +38,10 @@ def _init_externals() -> None:
3838

3939
# { Imports
4040

41+
from gitdb.util import to_hex_sha
42+
4143
try:
44+
from git.compat import safe_decode # @NoMove @IgnorePep8
4245
from git.config import GitConfigParser # @NoMove @IgnorePep8
4346
from git.objects import * # @NoMove @IgnorePep8
4447
from git.refs import * # @NoMove @IgnorePep8
@@ -53,6 +56,7 @@ def _init_externals() -> None:
5356
BlockingLockFile,
5457
Stats,
5558
Actor,
59+
remove_password_if_present,
5660
rmtree,
5761
)
5862
except GitError as _exc:

git/exc.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,34 @@
55
# the BSD License: https://opensource.org/license/bsd-3-clause/
66
""" Module containing all exceptions thrown throughout the git package """
77

8-
from gitdb.exc import ( # noqa: @UnusedImport
8+
__all__ = [
9+
# Defined in gitdb.exc:
10+
"AmbiguousObjectName",
11+
"BadName",
12+
"BadObject",
13+
"BadObjectType",
14+
"InvalidDBRoot",
15+
"ODBError",
16+
"ParseError",
17+
"UnsupportedOperation",
18+
# Introduced in this module:
19+
"GitError",
20+
"InvalidGitRepositoryError",
21+
"WorkTreeRepositoryUnsupported",
22+
"NoSuchPathError",
23+
"UnsafeProtocolError",
24+
"UnsafeOptionError",
25+
"CommandError",
26+
"GitCommandNotFound",
27+
"GitCommandError",
28+
"CheckoutError",
29+
"CacheError",
30+
"UnmergedEntriesError",
31+
"HookExecutionError",
32+
"RepositoryDirtyError",
33+
]
34+
35+
from gitdb.exc import (
936
AmbiguousObjectName,
1037
BadName,
1138
BadObject,
@@ -14,7 +41,6 @@
1441
ODBError,
1542
ParseError,
1643
UnsupportedOperation,
17-
to_hex_sha,
1844
)
1945
from git.compat import safe_decode
2046
from git.util import remove_password_if_present

0 commit comments

Comments
 (0)