Skip to content

Add __all__ in git.exc #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
EliahKagan committed Oct 20, 2023
commit 7545b802d811128ac367a47b794917f2f678836b
8 changes: 6 additions & 2 deletions git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# @PydevCodeAnalysisIgnore
from git.exc import * # @NoMove @IgnorePep8
import os
import sys
import os.path as osp
import sys

from typing import Optional
from typing import List, Optional, Sequence, Tuple, Union, TYPE_CHECKING
from git.types import PathLike

__version__ = "git"
Expand Down Expand Up @@ -38,7 +38,10 @@ def _init_externals() -> None:

# { Imports

from gitdb.util import to_hex_sha

try:
from git.compat import safe_decode # @NoMove @IgnorePep8
from git.config import GitConfigParser # @NoMove @IgnorePep8
from git.objects import * # @NoMove @IgnorePep8
from git.refs import * # @NoMove @IgnorePep8
Expand All @@ -53,6 +56,7 @@ def _init_externals() -> None:
BlockingLockFile,
Stats,
Actor,
remove_password_if_present,
rmtree,
)
except GitError as _exc:
Expand Down
30 changes: 28 additions & 2 deletions git/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@
# the BSD License: https://opensource.org/license/bsd-3-clause/
""" Module containing all exceptions thrown throughout the git package """

from gitdb.exc import ( # noqa: @UnusedImport
__all__ = [
# Defined in gitdb.exc:
"AmbiguousObjectName",
"BadName",
"BadObject",
"BadObjectType",
"InvalidDBRoot",
"ODBError",
"ParseError",
"UnsupportedOperation",
# Introduced in this module:
"GitError",
"InvalidGitRepositoryError",
"WorkTreeRepositoryUnsupported",
"NoSuchPathError",
"UnsafeProtocolError",
"UnsafeOptionError",
"CommandError",
"GitCommandNotFound",
"GitCommandError",
"CheckoutError",
"CacheError",
"UnmergedEntriesError",
"HookExecutionError",
"RepositoryDirtyError",
]

from gitdb.exc import (
AmbiguousObjectName,
BadName,
BadObject,
Expand All @@ -14,7 +41,6 @@
ODBError,
ParseError,
UnsupportedOperation,
to_hex_sha,
)
from git.compat import safe_decode
from git.util import remove_password_if_present
Expand Down