Skip to content

Commit 8fb0a9b

Browse files
authored
CI: check for some unused ignore comments (#213)
* CI: check for some unused ignore comments * pyright ignore * close file handles * address comments * mypy errors? * comments * unused import
1 parent 3690699 commit 8fb0a9b

File tree

9 files changed

+98
-42
lines changed

9 files changed

+98
-42
lines changed

pandas-stubs/core/groupby/generic.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,17 @@ class _DataFrameGroupByNonScalar(DataFrameGroupBy):
141141
class DataFrameGroupBy(GroupBy):
142142
def any(self, skipna: bool = ...) -> DataFrame: ...
143143
def all(self, skipna: bool = ...) -> DataFrame: ...
144-
# mypy and pyright see these overloads as overlapping
145144
@overload
146-
def apply( # type: ignore[misc]
145+
def apply(
147146
self, func: Callable[[DataFrame], Scalar | list | dict], *args, **kwargs
148147
) -> Series: ...
149148
@overload
150-
def apply( # type: ignore[misc]
149+
def apply(
151150
self, func: Callable[[DataFrame], Series | DataFrame], *args, **kwargs
152151
) -> DataFrame: ...
152+
# error: Overload 3 for "apply" will never be used because its parameters overlap overload 1
153153
@overload
154-
def apply( # type: ignore[misc]
154+
def apply( # pyright: ignore[reportOverlappingOverload]
155155
self, func: Callable[[Iterable], float], *args, **kwargs
156156
) -> DataFrame: ...
157157
def aggregate(self, arg: AggFuncType = ..., *args, **kwargs) -> DataFrame: ...

pandas-stubs/core/indexes/datetimes.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeIndexProperties):
7272
def __le__(self, other: Timestamp) -> np_ndarray_bool: ...
7373
def __gt__(self, other: Timestamp) -> np_ndarray_bool: ...
7474
def __ge__(self, other: Timestamp) -> np_ndarray_bool: ...
75-
# ignore for mypy because we know dtype of a DatetimeIndex
7675
@property
77-
def dtype(self) -> np.dtype | DatetimeTZDtype: ... # type: ignore[misc]
76+
def dtype(self) -> np.dtype | DatetimeTZDtype: ...
7877

7978
def date_range(
8079
start=...,

pandas-stubs/io/parsers.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ class TextFileReader(abc.Iterator):
516516
def __init__(self, f, engine=..., **kwds) -> None: ...
517517
def close(self) -> None: ...
518518
def __next__(self): ...
519+
def __enter__(self) -> TextFileReader: ...
520+
def __exit__(self, exc_type, exc_value, traceback) -> None: ...
519521
def read(self, nrows=...): ...
520522
def get_chunk(self, size=...): ...
521523

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ no_implicit_optional = true
138138
strict_optional = true
139139
# Configuring warnings
140140
warn_redundant_casts = true
141-
warn_unused_ignores = false # Change from pandas
141+
warn_unused_ignores = true
142142
warn_no_return = true
143143
warn_return_any = false # TODO
144144
warn_unreachable = false # GH#27396
@@ -168,6 +168,7 @@ reportMissingParameterType = false
168168
reportMissingTypeArgument = false
169169
reportMissingTypeStubs = false
170170
reportUnknownArgumentType = false
171+
reportUnusedExpression = false
171172
reportUnknownLambdaType = false
172173
reportUnknownMemberType = false
173174
reportUnknownParameterType = false

tests/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
from __future__ import annotations
22

3+
from typing import (
4+
TYPE_CHECKING,
5+
Final,
6+
)
37

4-
def check(
5-
actual: object, klass: type, dtype: type | None = None, attr: str = "left"
6-
) -> None:
8+
from pandas._typing import T
9+
10+
TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING
11+
12+
13+
def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left") -> T:
714

815
if not isinstance(actual, klass):
916
raise RuntimeError(f"Expected type '{klass}' but got '{type(actual)}'")
1017
if dtype is None:
11-
return None
18+
return actual # type: ignore[return-value]
1219

1320
if hasattr(actual, "__iter__"):
1421
value = next(iter(actual)) # type: ignore[call-overload]
1522
else:
1623
assert hasattr(actual, attr)
17-
value = getattr(actual, attr) # type: ignore[attr-defined]
24+
value = getattr(actual, attr)
1825

1926
if not isinstance(value, dtype):
2027
raise RuntimeError(f"Expected type '{dtype}' but got '{type(value)}'")
21-
return None
28+
return actual # type: ignore[return-value]

tests/test_frame.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,20 +1139,51 @@ def test_types_regressions() -> None:
11391139

11401140

11411141
def test_read_csv() -> None:
1142-
if TYPE_CHECKING: # skip pytest
1143-
# https://github.com/microsoft/python-type-stubs/issues/87
1144-
df11: pd.DataFrame = pd.read_csv("foo")
1145-
df12: pd.DataFrame = pd.read_csv("foo", iterator=False)
1146-
df13: pd.DataFrame = pd.read_csv("foo", iterator=False, chunksize=None)
1147-
df14: TextFileReader = pd.read_csv("foo", chunksize=0)
1148-
df15: TextFileReader = pd.read_csv("foo", iterator=False, chunksize=0)
1149-
df16: TextFileReader = pd.read_csv("foo", iterator=True)
1150-
df17: TextFileReader = pd.read_csv("foo", iterator=True, chunksize=None)
1151-
df18: TextFileReader = pd.read_csv("foo", iterator=True, chunksize=0)
1152-
df19: TextFileReader = pd.read_csv("foo", chunksize=0)
1142+
with ensure_clean() as path:
1143+
Path(path).write_text("A,B\n1,2")
1144+
check(assert_type(pd.read_csv(path), pd.DataFrame), pd.DataFrame)
1145+
check(
1146+
assert_type(pd.read_csv(path, iterator=False), pd.DataFrame), pd.DataFrame
1147+
)
1148+
check(
1149+
assert_type(
1150+
pd.read_csv(path, iterator=False, chunksize=None), pd.DataFrame
1151+
),
1152+
pd.DataFrame,
1153+
)
1154+
1155+
with check(
1156+
assert_type(pd.read_csv(path, chunksize=1), TextFileReader), TextFileReader
1157+
):
1158+
pass
1159+
with check(
1160+
assert_type(pd.read_csv(path, iterator=False, chunksize=1), TextFileReader),
1161+
TextFileReader,
1162+
):
1163+
pass
1164+
with check(
1165+
assert_type(pd.read_csv(path, iterator=True), TextFileReader),
1166+
TextFileReader,
1167+
):
1168+
pass
1169+
with check(
1170+
assert_type(
1171+
pd.read_csv(path, iterator=True, chunksize=None), TextFileReader
1172+
),
1173+
TextFileReader,
1174+
):
1175+
pass
1176+
with check(
1177+
assert_type(pd.read_csv(path, iterator=True, chunksize=1), TextFileReader),
1178+
TextFileReader,
1179+
):
1180+
pass
11531181

11541182
# https://github.com/microsoft/python-type-stubs/issues/118
1155-
pd.read_csv("foo", storage_options=None)
1183+
check(
1184+
assert_type(pd.read_csv(path, storage_options=None), pd.DataFrame),
1185+
pd.DataFrame,
1186+
)
11561187

11571188

11581189
def test_groupby_series_methods() -> None:
@@ -1358,6 +1389,7 @@ def test_set_columns() -> None:
13581389
df = pd.DataFrame({"a": [1, 2, 3], "b": [0.0, 1, 1]})
13591390
# Next line should work, but it is a mypy bug
13601391
# https://github.com/python/mypy/issues/3004
1392+
# pyright doesn't need the ignore
13611393
df.columns = ["c", "d"] # type: ignore[assignment]
13621394

13631395

tests/test_interval.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
4-
53
import pandas as pd
64
from typing_extensions import assert_type
75

8-
from tests import check
6+
from tests import (
7+
TYPE_CHECKING_INVALID_USAGE,
8+
check,
9+
)
910

1011

1112
def test_interval_init() -> None:
@@ -49,8 +50,8 @@ def test_interval_length() -> None:
4950
idres = i1 + pd.Timedelta(seconds=20)
5051

5152
check(assert_type(idres, "pd.Interval[pd.Timestamp]"), pd.Interval, pd.Timestamp)
52-
if TYPE_CHECKING:
53-
20 in i1 # type: ignore[operator]
53+
if TYPE_CHECKING_INVALID_USAGE:
54+
20 in i1 # TODO both: ignore[operator]
5455
i1 + pd.Timestamp("2000-03-03") # type: ignore[operator]
5556
i1 * 3 # type: ignore[operator]
5657
i1 * pd.Timedelta(seconds=20) # type: ignore[operator]
@@ -68,9 +69,13 @@ def test_interval_length() -> None:
6869
check(assert_type(i2 * 4, "pd.Interval[int]"), pd.Interval, int)
6970
check(assert_type(i2 * 4.2, "pd.Interval[float]"), pd.Interval, float)
7071

71-
if TYPE_CHECKING:
72-
pd.Timestamp("2001-01-02") in i2 # type: ignore[operator]
73-
i2 + pd.Timedelta(seconds=20) # type: ignore[operator]
72+
if TYPE_CHECKING_INVALID_USAGE:
73+
pd.Timestamp(
74+
"2001-01-02"
75+
) in i2 # pyright: ignore[reportGeneralTypeIssues] # TODO mypy: ignore[operator]
76+
i2 + pd.Timedelta(
77+
seconds=20
78+
) # pyright: ignore[reportGeneralTypeIssues] # TODO mypy: ignore[operator]
7479

7580
i3 = pd.Interval(13.2, 19.5)
7681
check(assert_type(i3.length, float), float)
@@ -82,6 +87,10 @@ def test_interval_length() -> None:
8287
check(assert_type(i3inres, bool), bool)
8388
check(assert_type(i3 + 3, "pd.Interval[float]"), pd.Interval, float)
8489
check(assert_type(i3 * 3, "pd.Interval[float]"), pd.Interval, float)
85-
if TYPE_CHECKING:
86-
pd.Timestamp("2001-01-02") in i3 # type: ignore[operator]
87-
i3 + pd.Timedelta(seconds=20) # type: ignore[operator]
90+
if TYPE_CHECKING_INVALID_USAGE:
91+
pd.Timestamp(
92+
"2001-01-02"
93+
) in i3 # pyright: ignore[reportGeneralTypeIssues] # TODO mypy: ignore[operator]
94+
i3 + pd.Timedelta(
95+
seconds=20
96+
) # pyright: ignore[reportGeneralTypeIssues] # TODO mypy: ignore[operator]

tests/test_series.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
from pandas._typing import Scalar
2727

28-
from tests import check
28+
from tests import (
29+
TYPE_CHECKING_INVALID_USAGE,
30+
check,
31+
)
2932

3033
if TYPE_CHECKING:
3134
from pandas._typing import np_ndarray_int # noqa: F401
@@ -661,7 +664,7 @@ def add1(x: int) -> int:
661664
# inplace
662665
s6: None = pd.Series([1, 2, 3]).rename("A", inplace=True)
663666

664-
if TYPE_CHECKING:
667+
if TYPE_CHECKING_INVALID_USAGE:
665668
s7 = pd.Series([1, 2, 3]).rename({1: [3, 4, 5]}) # type: ignore[dict-item]
666669

667670

tests/test_timefuncs.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
from pandas._libs import NaTType
1919
from pandas._libs.tslibs import BaseOffset
2020

21-
from tests import check
21+
from tests import (
22+
TYPE_CHECKING_INVALID_USAGE,
23+
check,
24+
)
2225

2326
if TYPE_CHECKING:
2427
from pandas.core.series import (
@@ -201,10 +204,10 @@ def test_iso_calendar() -> None:
201204
def fail_on_adding_two_timestamps() -> None:
202205
s1 = pd.Series(pd.to_datetime(["2022-05-01", "2022-06-01"]))
203206
s2 = pd.Series(pd.to_datetime(["2022-05-15", "2022-06-15"]))
204-
if TYPE_CHECKING:
205-
ssum: pd.Series = s1 + s2 # type: ignore[operator]
207+
if TYPE_CHECKING_INVALID_USAGE:
208+
ssum: pd.Series = s1 + s2 # TODO both: ignore[operator]
206209
ts = pd.Timestamp("2022-06-30")
207-
tsum: pd.Series = s1 + ts # type: ignore[operator]
210+
tsum: pd.Series = s1 + ts # TODO both: ignore[operator]
208211

209212

210213
def test_dtindex_tzinfo() -> None:

0 commit comments

Comments
 (0)