Skip to content

Commit dffa930

Browse files
committed
Refine deprecated module attributes and their warnings
- In the top-level git module, import util from git.index so there is no ambiguity for tools in detecting which module the util attribute of the top-level git module (i.e., git.util in an *expression*) is, even though it's subsequently deleted (and then dynamically supplied when requested, in a way that is opaque to static type checkers due to being only when `not TYPE_CHECKING`). This seems to be necessary for some tools. Curiously, guarding the del statement under `not TYPE_CHECKING` seems *not* to be needed by any tools of any kind. It should still possibly be done, but that's not included in these changes. - Add the missing deprecation warning for git.types.Lit_commit_ish. - When importing the warnings module, do so with a top-level import as in the other GitPython modules that have long (and reasonably) done so. In git/__init__.py, there already had to be an import of importlib, which seemed like it should be done locally in case of delays. Neither the importlib module nor any of its submodules were already imported anywhere in GitPython, and the code that uses it will most often not be exercised. So there is a potential benefit to avoiding loading it when not needed. When writing a local import for that, I had included the warnings module as a local import as well. But this obscures the potential benefit of locally importing importlib, and could lead to ill-advised changes in the future based on the idea that the degree of justification to be local imports was the same for them both.
1 parent 28bd4a3 commit dffa930

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

Diff for: git/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
if TYPE_CHECKING:
9494
from types import ModuleType
9595

96+
import warnings
97+
9698
from gitdb.util import to_hex_sha
9799

98100
from git.exc import (
@@ -167,6 +169,7 @@
167169
IndexEntry,
168170
IndexFile,
169171
StageType,
172+
util,
170173
)
171174
from git.util import ( # @NoMove
172175
Actor,
@@ -198,12 +201,11 @@
198201
# the intuitive but potentially incompatible binding occurs due to the usual rules for
199202
# Python submodule bindings. So for now we delete that and let __getattr__ handle it.
200203
#
201-
del util # type: ignore[name-defined] # noqa: F821
204+
del util
202205

203206

204207
def _warned_import(message: str, fullname: str) -> "ModuleType":
205208
import importlib
206-
import warnings
207209

208210
warnings.warn(message, DeprecationWarning, stacklevel=3)
209211
return importlib.import_module(fullname)

Diff for: git/compat.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import locale
1414
import os
1515
import sys
16+
import warnings
1617

1718
from gitdb.utils.encoding import force_bytes, force_text # noqa: F401
1819

@@ -48,8 +49,6 @@ def _getattr(name: str) -> Any:
4849
except KeyError:
4950
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from None
5051

51-
import warnings
52-
5352
warnings.warn(
5453
f"{__name__}.{name} and other is_<platform> aliases are deprecated. "
5554
"Write the desired os.name or sys.platform check explicitly instead.",

Diff for: git/types.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TypeVar,
1818
Union,
1919
)
20+
import warnings
2021

2122
if sys.version_info >= (3, 8):
2223
from typing import (
@@ -150,9 +151,19 @@
150151

151152

152153
def _getattr(name: str) -> Any:
153-
if name == "Lit_commit_ish":
154-
return Literal["commit", "tag"]
155-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
154+
if name != "Lit_commit_ish":
155+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
156+
157+
warnings.warn(
158+
"Lit_commit_ish is deprecated. It is currently defined as "
159+
'`Literal["commit", "tag"]`, which should be used in its place if desired. It '
160+
'had previously been defined as `Literal["commit", "tag", "blob", "tree"]`, '
161+
"covering all four git object type strings including those that are never "
162+
"commit-ish. For that, use the GitObjectTypeString type instead.",
163+
DeprecationWarning,
164+
stacklevel=2,
165+
)
166+
return Literal["commit", "tag"]
156167

157168

158169
if not TYPE_CHECKING: # Preserve static checking for undefined/misspelled attributes.

0 commit comments

Comments
 (0)