Skip to content

Commit 28e2114

Browse files
committed
Improve RemoteProgress parse return typing
1 parent 07e8055 commit 28e2114

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

git/util.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,13 +611,17 @@ def __init__(self) -> None:
611611
self.error_lines: List[str] = []
612612
self.other_lines: List[str] = []
613613

614-
def _parse_progress_line(self, line: AnyStr) -> None:
614+
def _parse_progress_line(self, line: AnyStr) -> Optional[object]:
615615
"""Parse progress information from the given line as retrieved by
616616
:manpage:`git-push(1)` or :manpage:`git-fetch(1)`.
617617
618618
- Lines that do not contain progress info are stored in :attr:`other_lines`.
619619
- Lines that seem to contain an error (i.e. start with ``error:`` or ``fatal:``)
620620
are stored in :attr:`error_lines`.
621+
622+
The base implementation returns ``None``. Subclasses may return another
623+
value for compatibility with existing overrides, but callers should treat
624+
the return value as unspecified.
621625
"""
622626
# handle
623627
# Counting objects: 4, done.
@@ -632,7 +636,7 @@ def _parse_progress_line(self, line: AnyStr) -> None:
632636

633637
if self._cur_line.startswith(("error:", "fatal:")):
634638
self.error_lines.append(self._cur_line)
635-
return
639+
return None
636640

637641
cur_count, max_count = None, None
638642
match = self.re_op_relative.match(line_str)
@@ -642,7 +646,7 @@ def _parse_progress_line(self, line: AnyStr) -> None:
642646
if not match:
643647
self.line_dropped(line_str)
644648
self.other_lines.append(line_str)
645-
return
649+
return None
646650
# END could not get match
647651

648652
op_code = 0
@@ -673,7 +677,7 @@ def _parse_progress_line(self, line: AnyStr) -> None:
673677
self.line_dropped(line_str)
674678
# Note: Don't add this line to the other lines, as we have to silently
675679
# drop it.
676-
return
680+
return None
677681
# END handle op code
678682

679683
# Figure out stage.
@@ -699,6 +703,7 @@ def _parse_progress_line(self, line: AnyStr) -> None:
699703
max_count and float(max_count),
700704
message,
701705
)
706+
return None
702707

703708
def new_message_handler(self) -> Callable[[str], None]:
704709
"""
@@ -708,7 +713,7 @@ def new_message_handler(self) -> Callable[[str], None]:
708713
"""
709714

710715
def handler(line: AnyStr) -> None:
711-
return self._parse_progress_line(line.rstrip())
716+
self._parse_progress_line(line.rstrip())
712717

713718
# END handler
714719

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This module is part of GitPython and is released under the
2+
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
3+
4+
"""Tests for static characteristics of remote progress helpers."""
5+
6+
from typing import AnyStr
7+
8+
from git import RemoteProgress
9+
10+
11+
def test_parse_progress_line_override_can_return_non_none() -> None:
12+
"""Subclass overrides may return a value without violating base typing."""
13+
14+
class ReturningProgress(RemoteProgress):
15+
def _parse_progress_line(self, line: AnyStr) -> str:
16+
super()._parse_progress_line(line)
17+
return "handled"
18+
19+
assert ReturningProgress()._parse_progress_line("unmatched progress") == "handled"

0 commit comments

Comments
 (0)