Skip to content

Commit 4082545

Browse files
codexByron
authored andcommitted
Deprecate Actor.name_email_regex (#2220)
Keep returning the historical compiled pattern for downstream compatibility, but emit DeprecationWarning whenever the class member is accessed. The warning explains its quadratic behavior on long malformed strings and recommends Actor._from_string() or direct string parsing. The regression verifies that access warns, names both alternatives, and still supports the existing match behavior. Validation: - test/test_actor.py: 8 passed - ruff check and format checks passed - basedpyright git/util.py: no errors
1 parent 8afd1fd commit 4082545

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

git/util.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,14 +853,26 @@ def update(self, *args: Any, **kwargs: Any) -> None:
853853
self._callable(*args, **kwargs)
854854

855855

856+
class _DeprecatedActorNameEmailRegex:
857+
_pattern = re.compile(r"(.*) <(.*?)>")
858+
859+
def __get__(self, _instance: Any, _owner: Any) -> Pattern[str]:
860+
warnings.warn(
861+
"Actor.name_email_regex is deprecated and will be removed in GitPython 4.0.0 because searching long "
862+
"malformed strings with it can take quadratic time. Use Actor._from_string() to parse actor identities, "
863+
"or str.partition() and str.find() for custom validation.",
864+
DeprecationWarning,
865+
stacklevel=2,
866+
)
867+
return self._pattern
868+
869+
856870
class Actor:
857871
"""Actors hold information about a person acting on the repository. They can be
858872
committers and authors or anything with a name and an email as mentioned in the git
859873
log entries."""
860874

861-
# Kept for compatibility only: searching long malformed strings with this regex can
862-
# take quadratic time. ``_from_string`` deliberately uses delimiter scans instead.
863-
name_email_regex = re.compile(r"(.*) <(.*?)>")
875+
name_email_regex = _DeprecatedActorNameEmailRegex()
864876

865877
# ENVIRONMENT VARIABLES
866878
# These are read when creating new commits.

test/test_actor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ def test_from_string_handles_unterminated_email_without_regex_backtracking(self)
3535
actor = Actor._from_string(value)
3636
self.assertEqual(actor, Actor(value, None))
3737

38-
def test_name_email_regex_is_available(self):
39-
match = Actor.name_email_regex.match("Michael Trier <mtrier@example.com>")
38+
def test_name_email_regex_is_available_but_deprecated(self):
39+
with self.assertWarns(DeprecationWarning) as context:
40+
match = Actor.name_email_regex.match("Michael Trier <mtrier@example.com>")
41+
42+
message = str(context.warning)
43+
self.assertIn("Actor._from_string()", message)
44+
self.assertIn("str.partition()", message)
4045
self.assertIsNotNone(match)
4146
self.assertEqual(match.groups(), ("Michael Trier", "mtrier@example.com"))
4247

0 commit comments

Comments
 (0)