Skip to content

Commit

Permalink
improve utils.get_class_... and utils.is_implemented coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
camriddell committed Feb 2, 2025
1 parent fd40698 commit 005574c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
17 changes: 8 additions & 9 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,9 +1160,11 @@ def get_class_that_defines_method(method: Callable[..., Any]) -> type:
type
"""
if ismethod(method):
for cls in getmro(method.__self__.__class__):
if method.__name__ in cls.__dict__:
return cls
return next(
cls
for cls in getmro(method.__self__.__class__)
if method.__name__ in cls.__dict__
)

elif isfunction(method):
maybe_cls = getattr(
Expand Down Expand Up @@ -1290,12 +1292,9 @@ def visit_Return(self, node: Return) -> None: # noqa: N802
self.has_return = True

def visit_Raise(self, node: Raise) -> None: # noqa: N802
if isinstance(node.exc, Call):
name_node = node.exc.func
elif isinstance(node.exc, Name):
name_node = node.exc

if name_node.id == "NotImplementedError": # type: ignore[attr-defined]
if (
isinstance(node.exc, Call) and node.exc.func.id == "NotImplementedError" # type: ignore[attr-defined]
) or (isinstance(node.exc, Name) and node.exc.id == "NotImplementedError"):
self.has_notimplemented = True

source = dedent(getsource(func))
Expand Down
13 changes: 13 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,16 @@ def i() -> None: # pragma: no cover
raise NotImplementedError

assert not is_implemented(i)

def j() -> None: # pragma: no cover
raise NotImplementedError

assert not is_implemented(j)

def k() -> None: # pragma: no cover
try:
raise NotImplementedError # noqa: TRY301
except NotImplementedError: # noqa: TRY203
raise

assert not is_implemented(k)

0 comments on commit 005574c

Please sign in to comment.