From 8197e9043f26a88168dc3227c4fbb0ec64ef42f9 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Thu, 19 Oct 2023 21:17:19 -0400 Subject: [PATCH 1/3] Remove now-unused import in top-level __init__.py The inspect module was only used to dynamically generate __all__, which is statically written since c862845. Although it includes everything it originally had for compatibility (8edc53b), this did not include modules, since the comprehension used to generate it omitted those (which is what it needed the inspect module for). So it was not, and is not, listed in __all__, and can be removed. --- git/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/git/__init__.py b/git/__init__.py index be8338ddc..38a59e517 100644 --- a/git/__init__.py +++ b/git/__init__.py @@ -6,7 +6,6 @@ # flake8: noqa # @PydevCodeAnalysisIgnore from git.exc import * # @NoMove @IgnorePep8 -import inspect import os import sys import os.path as osp From 7545b802d811128ac367a47b794917f2f678836b Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Thu, 19 Oct 2023 21:41:39 -0400 Subject: [PATCH 2/3] 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. --- git/__init__.py | 8 ++++++-- git/exc.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/git/__init__.py b/git/__init__.py index 38a59e517..05a02a7ff 100644 --- a/git/__init__.py +++ b/git/__init__.py @@ -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" @@ -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 @@ -53,6 +56,7 @@ def _init_externals() -> None: BlockingLockFile, Stats, Actor, + remove_password_if_present, rmtree, ) except GitError as _exc: diff --git a/git/exc.py b/git/exc.py index b4ffe7568..32c371d0b 100644 --- a/git/exc.py +++ b/git/exc.py @@ -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, @@ -14,7 +41,6 @@ ODBError, ParseError, UnsupportedOperation, - to_hex_sha, ) from git.compat import safe_decode from git.util import remove_password_if_present From 2af36792db870c6336b52d3c8aa3d19c0b2fe9c4 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Fri, 20 Oct 2023 02:56:16 -0400 Subject: [PATCH 3/3] Remove `@UnusedImport` from an import that is used Inclusion in __all__ is considered a use. --- git/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/db.py b/git/db.py index bff43347b..b1a0d108a 100644 --- a/git/db.py +++ b/git/db.py @@ -1,7 +1,7 @@ """Module with our own gitdb implementation - it uses the git command""" from git.util import bin_to_hex, hex_to_bin from gitdb.base import OInfo, OStream -from gitdb.db import GitDB # @UnusedImport +from gitdb.db import GitDB from gitdb.db import LooseObjectDB from gitdb.exc import BadObject