Skip to content

Commit c28b096

Browse files
authored
do not setup progress reporter if git is not installed (#426)
1 parent dbbfc5a commit c28b096

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

pyproject.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ select = [
192192

193193
[tool.ruff.lint.per-file-ignores]
194194
"noxfile.py" = ["D", "PTH"]
195-
"tests/**" = ["S", "ARG001", "ARG002", "ANN"]
195+
"tests/**" = ["S", "ARG001", "ARG002", "ANN", "TID251", "TID253"]
196196
"docs/**" = ["INP"]
197+
"src/scmrepo/git/backend/gitpython.py" = ["TID251"]
197198

198199
[tool.ruff.lint.flake8-pytest-style]
199200
fixture-parentheses = false
@@ -203,6 +204,14 @@ parametrize-names-type = "csv"
203204
[tool.ruff.lint.flake8-type-checking]
204205
strict = true
205206

207+
[tool.ruff.lint.flake8-tidy-imports.banned-api]
208+
"git".msg = "importing from 'git' is not allowed except inside `gitpython` backend"
209+
210+
[tool.ruff.lint.flake8-tidy-imports]
211+
# Ban certain modules from being imported at module level, instead requiring
212+
# that they're imported lazily (e.g., within a function definition).
213+
banned-module-level-imports = ["git"]
214+
206215
[tool.ruff.lint.isort]
207216
known-first-party = ["scmrepo"]
208217

src/scmrepo/progress.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def code2desc(op_code):
7-
from git import RootUpdateProgress as OP # noqa: N814
7+
from git import RootUpdateProgress as OP # noqa: N814, TID251
88

99
ops = {
1010
OP.COUNTING: "Counting",
@@ -44,16 +44,20 @@ def parsed_from_gitpython(
4444

4545
class GitProgressReporter:
4646
def __init__(self, fn) -> None:
47-
from git.util import CallableRemoteProgress
48-
49-
self._reporter = CallableRemoteProgress(self.wrap_fn(fn))
47+
try:
48+
from git.util import CallableRemoteProgress # noqa: TID251
49+
except ImportError:
50+
self._reporter = None
51+
else:
52+
self._reporter = CallableRemoteProgress(self.wrap_fn(fn))
5053

5154
def __call__(self, msg: Union[str, bytes]) -> None:
52-
self._reporter._parse_progress_line(
53-
msg.decode("utf-8", errors="replace").strip()
54-
if isinstance(msg, bytes)
55-
else msg
56-
)
55+
if self._reporter is not None:
56+
self._reporter._parse_progress_line(
57+
msg.decode("utf-8", errors="replace").strip()
58+
if isinstance(msg, bytes)
59+
else msg
60+
)
5761

5862
@staticmethod
5963
def wrap_fn(fn):

0 commit comments

Comments
 (0)