Skip to content

Commit 42ab31a

Browse files
committed
define __bool__ for Series and DataFrame to be NoReturn
1 parent 3418530 commit 42ab31a

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from typing import (
1414
Any,
1515
ClassVar,
1616
Literal,
17+
NoReturn,
1718
overload,
1819
)
1920

@@ -2478,6 +2479,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
24782479
) -> Self: ...
24792480
def __truediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
24802481
def __rtruediv__(self, other: float | DataFrame | Series | Sequence) -> Self: ...
2482+
def __bool__(self) -> NoReturn: ...
24812483

24822484
class _PandasNamedTuple(tuple[Any, ...]):
24832485
def __getattr__(self, field: str) -> Scalar: ...

pandas-stubs/core/series.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ from typing import (
1818
ClassVar,
1919
Generic,
2020
Literal,
21+
NoReturn,
2122
overload,
2223
)
2324

@@ -2152,6 +2153,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
21522153
level: Level | None = ...,
21532154
drop_level: _bool = ...,
21542155
) -> Self: ...
2156+
def __bool__(self) -> NoReturn: ...
21552157

21562158
class TimestampSeries(Series[Timestamp]):
21572159
@property

tests/test_frame.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,3 +3781,16 @@ def test_frame_index_timestamp() -> None:
37813781
df2 = pd.DataFrame({"x": s})
37823782
check(assert_type(df2.loc[dt1, "x"], Scalar), np.integer)
37833783
check(assert_type(df2.loc[[dt1], "x"], pd.Series), pd.Series, np.integer)
3784+
3785+
3786+
def test_frame_bool_fails() -> None:
3787+
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
3788+
3789+
try:
3790+
# We want the type checker to tell us the next line is invalid
3791+
# mypy doesn't seem to figure that out, but pyright does
3792+
if df == "foo": # pyright: ignore[reportGeneralTypeIssues]
3793+
# Next line is unreachable.
3794+
s = df["a"]
3795+
except ValueError:
3796+
pass

tests/test_series.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,3 +3505,16 @@ def test_series_index_timestamp() -> None:
35053505
s = pd.Series([1, 2], index=[dt1, dt2])
35063506
check(assert_type(s[dt1], int), np.integer)
35073507
check(assert_type(s.loc[[dt1]], "pd.Series[int]"), pd.Series, np.integer)
3508+
3509+
3510+
def test_series_bool_fails() -> None:
3511+
s = pd.Series([1, 2, 3])
3512+
3513+
try:
3514+
# We want the type checker to tell us the next line is invalid
3515+
# mypy doesn't seem to figure that out, but pyright does
3516+
if s == "foo": # pyright: ignore[reportGeneralTypeIssues]
3517+
# Next line is unreachable.
3518+
a = s[0]
3519+
except ValueError:
3520+
pass

0 commit comments

Comments
 (0)