Skip to content

Commit

Permalink
Fix version_info type hint + more refactoring
Browse files Browse the repository at this point in the history
This broadens the type annoation on the Git.version_info property,
for #1830.

It also revises the docstring for clarity, and continues
refactoring and comment changes (also for clarity).
  • Loading branch information
EliahKagan committed Feb 22, 2024
1 parent eb438ee commit dc6b90f
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def __init__(self, working_dir: Union[None, PathLike] = None):
self._environment: Dict[str, str] = {}

# Cached version slots
self._version_info: Union[Tuple[int, int, int, int], None] = None
self._version_info: Union[Tuple[int, ...], None] = None
self._version_info_token: object = None

# Cached command slots
Expand Down Expand Up @@ -831,10 +831,10 @@ def working_dir(self) -> Union[None, PathLike]:
return self._working_dir

@property
def version_info(self) -> Tuple[int, int, int, int]:
def version_info(self) -> Tuple[int, ...]:
"""
:return: tuple(int, int, int, int) tuple with integers representing the major,
minor and additional version numbers as parsed from git version.
:return: tuple with integers representing the major, minor and additional
version numbers as parsed from git version. Up to four fields are used.
This value is generated on demand and is cached.
"""
Expand All @@ -846,16 +846,14 @@ def version_info(self) -> Tuple[int, int, int, int]:
assert self._version_info is not None, "Bug: corrupted token-check state"
return self._version_info

# We only use the first 4 numbers, as everything else could be strings in fact (on Windows).
process_version = self._call_process("version") # Should be as default *args and **kwargs used.
version_numbers = process_version.split(" ")[2]
# Run "git version" and parse it.
process_version = self._call_process("version")
version_string = process_version.split(" ")[2]
version_fields = version_string.split(".")[:4]
self._version_info = tuple(int(n) for n in version_fields if n.isdigit())

self._version_info = cast(
Tuple[int, int, int, int],
tuple(int(n) for n in version_numbers.split(".")[:4] if n.isdigit()),
)
# This value will be considered valid until the next refresh.
self._version_info_token = refresh_token

return self._version_info

@overload
Expand Down

0 comments on commit dc6b90f

Please sign in to comment.