Skip to content

Commit 3bb63f3

Browse files
committed
Condense FetchInfo.refresh using contextlib.suppress
I think this refactoring slightly improves readability. + Condense/sort/group imports (while adding contextlib import). + Minor style tweaks in FetchInfo.refresh comments. + Make a comment in Git.refresh clearer, building on revisions in: gitpython-developers#1810 (comment)
1 parent ae28c98 commit 3bb63f3

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

git/cmd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
478478
# We get here if this was the initial refresh and the refresh mode was
479479
# not error. Go ahead and set the GIT_PYTHON_GIT_EXECUTABLE such that we
480480
# discern the difference between the first refresh at import time
481-
# and subsequent calls to refresh().
481+
# and subsequent calls to git.refresh or this refresh method.
482482
cls.GIT_PYTHON_GIT_EXECUTABLE = cls.git_exec_name
483483
else:
484484
# After the first refresh (when GIT_PYTHON_GIT_EXECUTABLE is no longer

git/remote.py

+11-22
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,24 @@
55

66
"""Module implementing a remote object allowing easy access to git remotes."""
77

8+
import contextlib
89
import logging
910
import re
1011

11-
from git.cmd import handle_process_output, Git
12+
from git.cmd import Git, handle_process_output
1213
from git.compat import defenc, force_text
14+
from git.config import GitConfigParser, SectionConstraint, cp
1315
from git.exc import GitCommandError
16+
from git.refs import Head, Reference, RemoteReference, SymbolicReference, TagReference
1417
from git.util import (
15-
LazyMixin,
16-
IterableObj,
18+
CallableRemoteProgress,
1719
IterableList,
20+
IterableObj,
21+
LazyMixin,
1822
RemoteProgress,
19-
CallableRemoteProgress,
20-
)
21-
from git.util import (
2223
join_path,
2324
)
2425

25-
from git.config import (
26-
GitConfigParser,
27-
SectionConstraint,
28-
cp,
29-
)
30-
from git.refs import Head, Reference, RemoteReference, SymbolicReference, TagReference
31-
3226
# typing-------------------------------------------------------
3327

3428
from typing import (
@@ -345,18 +339,13 @@ class FetchInfo(IterableObj):
345339
@classmethod
346340
def refresh(cls) -> Literal[True]:
347341
"""This gets called by the refresh function (see the top level __init__)."""
348-
# clear the old values in _flag_map
349-
try:
342+
# Clear the old values in _flag_map.
343+
with contextlib.suppress(KeyError):
350344
del cls._flag_map["t"]
351-
except KeyError:
352-
pass
353-
354-
try:
345+
with contextlib.suppress(KeyError):
355346
del cls._flag_map["-"]
356-
except KeyError:
357-
pass
358347

359-
# set the value given the git version
348+
# Set the value given the git version.
360349
if Git().version_info[:2] >= (2, 10):
361350
cls._flag_map["t"] = cls.TAG_UPDATE
362351
else:

0 commit comments

Comments
 (0)