Skip to content

TYP: annotate functions that always error with NoReturn #48002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Generic,
Hashable,
Iterator,
NoReturn,
Sequence,
final,
)
Expand Down Expand Up @@ -1243,7 +1244,7 @@ def groupings(self) -> list[grouper.Grouping]:
ping = grouper.Grouping(lev, lev, in_axis=False, level=None)
return [ping]

def _aggregate_series_fast(self, obj: Series, func: Callable) -> np.ndarray:
def _aggregate_series_fast(self, obj: Series, func: Callable) -> NoReturn:
Copy link
Member

Choose a reason for hiding this comment

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

When we have NotImplementedError, does the NoReturn not create issues with subtypes? (in general, no checked this particular case)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it would create issues with sub-types that actually implement these functions. Parent methods should probably be abstract with a signature that the child will implement.

It should be safe adding NoReturn to final methods as no child class is meant to implement them. Based on the doc-string of _aggregate_series_fast I assume this method is basically deprecated and is not implemented in sub-classes.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. yes it does appear that the exception is not for public consumption.

Copy link
Member Author

Choose a reason for hiding this comment

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

NoReturn is useful in cases like this to narrow the type:

def error() -> NoReturn: ...

def test(x: int | str) -> str:
    # x: int | str
    if isinstance(x, int):
        error()
    # x: str
    return x + "some_string"

I don't there are many places where NoReturn can be used in pandas, in most cases we should make classes abstract.

Is there a historic reason to avoid inheriting from ABC and using abstractmethod? Happy to make PRs to make more methods abstract.

# -> np.ndarray[object]
raise NotImplementedError(
"This should not be reached; use _aggregate_series_pure_python"
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Hashable,
Iterable,
Literal,
NoReturn,
Sequence,
TypeVar,
cast,
Expand Down Expand Up @@ -3166,7 +3167,7 @@ def __xor__(self, other):
return self.symmetric_difference(other)

@final
def __nonzero__(self):
def __nonzero__(self) -> NoReturn:
raise ValueError(
f"The truth value of a {type(self).__name__} is ambiguous. "
"Use a.empty, a.bool(), a.item(), a.any() or a.all()."
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/indexes/frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"""
from __future__ import annotations

from typing import Any
from typing import (
Any,
NoReturn,
)

from pandas.core.base import PandasObject

Expand Down Expand Up @@ -93,7 +96,7 @@ def __reduce__(self):
def __hash__(self) -> int: # type: ignore[override]
return hash(tuple(self))

def _disabled(self, *args, **kwargs):
def _disabled(self, *args, **kwargs) -> NoReturn:
"""
This method will not function because object is immutable.
"""
Expand Down