Skip to content

Commit 2b768c7

Browse files
committed
Shorten Iterable docstrings and put IterableObj first
This shortens the git.util.Iterable docstrings, removing most of the text duplicated from git.util.IterableObj docstrings and instead referring the reader to the specific corresponding methods in IterableObj. A comment about return types that appeared in both places but really only documented IterableObj is likewise removed from Iterable. This also reorders the classes, placing IterableObj before Iterable and its associated metaclass. This makes it easier to find, and may help remind users that IterableObj is the class they should use. These changes build on those in dfee31f (gitpython-developers#1780).
1 parent f5e1b10 commit 2b768c7

File tree

1 file changed

+46
-53
lines changed

1 file changed

+46
-53
lines changed

git/util.py

+46-53
Original file line numberDiff line numberDiff line change
@@ -1182,54 +1182,37 @@ def __delitem__(self, index: Union[SupportsIndex, int, slice, str]) -> None:
11821182
list.__delitem__(self, delindex)
11831183

11841184

1185-
class IterableClassWatcher(type):
1186-
"""Metaclass that issues :class:`DeprecationWarning` when :class:`git.util.Iterable`
1187-
is subclassed."""
1188-
1189-
def __init__(cls, name: str, bases: Tuple, clsdict: Dict) -> None:
1190-
for base in bases:
1191-
if type(base) is IterableClassWatcher:
1192-
warnings.warn(
1193-
f"GitPython Iterable subclassed by {name}. "
1194-
"Iterable is deprecated due to naming clash since v3.1.18"
1195-
" and will be removed in 3.1.20, "
1196-
"Use IterableObj instead \n",
1197-
DeprecationWarning,
1198-
stacklevel=2,
1199-
)
1200-
1201-
1202-
class Iterable(metaclass=IterableClassWatcher):
1203-
"""Deprecated, use :class:`IterableObj` instead.
1204-
1205-
Defines an interface for iterable items, so there is a uniform way to retrieve
1185+
@runtime_checkable
1186+
class IterableObj(Protocol):
1187+
"""Defines an interface for iterable items, so there is a uniform way to retrieve
12061188
and iterate items within the git repository.
1189+
1190+
Subclasses = [Submodule, Commit, Reference, PushInfo, FetchInfo, Remote]
12071191
"""
12081192

12091193
__slots__ = ()
12101194

1211-
_id_attribute_ = "attribute that most suitably identifies your instance"
1195+
_id_attribute_: str
12121196

12131197
@classmethod
1214-
def iter_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Any:
1215-
# return typed to be compatible with subtypes e.g. Remote
1216-
"""Deprecated, use :class:`IterableObj` instead.
1217-
1218-
Find (all) items of this type.
1198+
@abstractmethod
1199+
def iter_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Iterator[T_IterableObj]:
1200+
# Return-typed to be compatible with subtypes e.g. Remote.
1201+
"""Find (all) items of this type.
12191202
12201203
Subclasses can specify ``args`` and ``kwargs`` differently, and may use them for
12211204
filtering. However, when the method is called with no additional positional or
12221205
keyword arguments, subclasses are obliged to to yield all items.
12231206
1207+
For more information about the arguments, see list_items.
1208+
12241209
:return: Iterator yielding Items
12251210
"""
12261211
raise NotImplementedError("To be implemented by Subclass")
12271212

12281213
@classmethod
1229-
def list_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Any:
1230-
"""Deprecated, use :class:`IterableObj` instead.
1231-
1232-
Find (all) items of this type and collect them into a list.
1214+
def list_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> IterableList[T_IterableObj]:
1215+
"""Find (all) items of this type and collect them into a list.
12331216
12341217
For more information about the arguments, see :meth:`list_items`.
12351218
@@ -1239,52 +1222,62 @@ def list_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Any:
12391222
12401223
:return: list(Item,...) list of item instances
12411224
"""
1242-
out_list: Any = IterableList(cls._id_attribute_)
1225+
out_list: IterableList = IterableList(cls._id_attribute_)
12431226
out_list.extend(cls.iter_items(repo, *args, **kwargs))
12441227
return out_list
12451228

12461229

1247-
@runtime_checkable
1248-
class IterableObj(Protocol):
1249-
"""Defines an interface for iterable items, so there is a uniform way to retrieve
1250-
and iterate items within the git repository.
1230+
class IterableClassWatcher(type):
1231+
"""Metaclass that issues :class:`DeprecationWarning` when :class:`git.util.Iterable`
1232+
is subclassed."""
12511233

1252-
Subclasses = [Submodule, Commit, Reference, PushInfo, FetchInfo, Remote]
1234+
def __init__(cls, name: str, bases: Tuple, clsdict: Dict) -> None:
1235+
for base in bases:
1236+
if type(base) is IterableClassWatcher:
1237+
warnings.warn(
1238+
f"GitPython Iterable subclassed by {name}. "
1239+
"Iterable is deprecated due to naming clash since v3.1.18"
1240+
" and will be removed in 3.1.20, "
1241+
"Use IterableObj instead \n",
1242+
DeprecationWarning,
1243+
stacklevel=2,
1244+
)
1245+
1246+
1247+
class Iterable(metaclass=IterableClassWatcher):
1248+
"""Deprecated, use :class:`IterableObj` instead.
1249+
1250+
Defines an interface for iterable items, so there is a uniform way to retrieve
1251+
and iterate items within the git repository.
12531252
"""
12541253

12551254
__slots__ = ()
12561255

1257-
_id_attribute_: str
1256+
_id_attribute_ = "attribute that most suitably identifies your instance"
12581257

12591258
@classmethod
1260-
@abstractmethod
1261-
def iter_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Iterator[T_IterableObj]:
1262-
# Return-typed to be compatible with subtypes e.g. Remote.
1263-
"""Find (all) items of this type.
1259+
def iter_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Any:
1260+
"""Deprecated, use :class:`IterableObj` instead.
12641261
1265-
Subclasses can specify ``args`` and ``kwargs`` differently, and may use them for
1266-
filtering. However, when the method is called with no additional positional or
1267-
keyword arguments, subclasses are obliged to to yield all items.
1262+
Find (all) items of this type.
12681263
1269-
For more information about the arguments, see list_items.
1264+
See :meth:`IterableObj.list_items` for details on usage.
12701265
12711266
:return: Iterator yielding Items
12721267
"""
12731268
raise NotImplementedError("To be implemented by Subclass")
12741269

12751270
@classmethod
1276-
def list_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> IterableList[T_IterableObj]:
1277-
"""Find (all) items of this type and collect them into a list.
1271+
def list_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> Any:
1272+
"""Deprecated, use :class:`IterableObj` instead.
12781273
1279-
For more information about the arguments, see :meth:`list_items`.
1274+
Find (all) items of this type and collect them into a list.
12801275
1281-
:note: Favor the :meth:`iter_items` method as it will avoid eagerly collecting
1282-
all items. When there are many items, that can slow performance and increase
1283-
memory usage.
1276+
See :meth:`IterableObj.list_items` for details on usage.
12841277
12851278
:return: list(Item,...) list of item instances
12861279
"""
1287-
out_list: IterableList = IterableList(cls._id_attribute_)
1280+
out_list: Any = IterableList(cls._id_attribute_)
12881281
out_list.extend(cls.iter_items(repo, *args, **kwargs))
12891282
return out_list
12901283

0 commit comments

Comments
 (0)