Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Add custom coverage test
Browse files Browse the repository at this point in the history
  • Loading branch information
YasinKaryagdi committed Jun 13, 2024
1 parent de2f99c commit 017ddcd
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import typing
import warnings
import logging
from cov import test, mark

import yarl

Expand Down Expand Up @@ -480,25 +481,28 @@ def find(predicate: Callable[[T], Any], iterable: _Iter[T], /) -> Union[Optional
else _find(predicate, iterable) # type: ignore
)


@test(3)
def _get(iterable: Iterable[T], /, **attrs: Any) -> Optional[T]:
# global -> local
_all = all
attrget = attrgetter

# Special case the single element call
if len(attrs) == 1:
mark(0)
k, v = attrs.popitem()
pred = attrget(k.replace('__', '.'))
return next((elem for elem in iterable if pred(elem) == v), None)

converted = [(attrget(attr.replace('__', '.')), value) for attr, value in attrs.items()]
for elem in iterable:
if _all(pred(elem) == value for pred, value in converted):
mark(1)
return elem
mark(2)
return None


@test(4)
async def _aget(iterable: AsyncIterable[T], /, **attrs: Any) -> Optional[T]:
# global -> local
_all = all
Expand All @@ -510,14 +514,18 @@ async def _aget(iterable: AsyncIterable[T], /, **attrs: Any) -> Optional[T]:
pred = attrget(k.replace('__', '.'))
async for elem in iterable:
if pred(elem) == v:
mark(0)
return elem
mark(1)
return None

converted = [(attrget(attr.replace('__', '.')), value) for attr, value in attrs.items()]

async for elem in iterable:
if _all(pred(elem) == value for pred, value in converted):
mark(2)
return elem
mark(3)
return None


Expand Down

1 comment on commit 017ddcd

@YasinKaryagdi
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this fails the lint check, after this commit I ran the black command on the file and commited it so that it does pass the lint check, though these are the important changes for the instrumentation.

Please sign in to comment.