Skip to content
Open
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-stubs/core/dtypes/missing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from pandas._libs.tslibs import NaTType
from pandas._typing import (
Scalar,
ScalarT,
ScalarT0,
ShapeT,
np_1darray_bool,
np_ndarray,
Expand Down Expand Up @@ -54,6 +55,6 @@ def notna(obj: np_ndarray[ShapeT]) -> np_ndarray[ShapeT, np.bool]: ...
@overload
def notna(obj: list[Any]) -> np_ndarray_bool: ...
@overload
def notna(obj: ScalarT | NaTType | NAType | None) -> TypeIs[ScalarT]: ...
def notna(obj: ScalarT0 | NaTType | NAType | None) -> TypeIs[ScalarT0]: ...

notnull = notna
1 change: 1 addition & 0 deletions pandas-stubs/errors/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ class InvalidColumnName(Warning): ...
class CategoricalConversionWarning(Warning): ...
class InvalidVersion(ValueError): ...
class NoBufferPresent(Exception): ...
class Pandas4Warning(Warning): ...
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ enableTypeIgnoreComments = false # use pyright-specific ignores
# disable subset of strict
reportMissingParameterType = false
reportUnnecessaryTypeIgnoreComment = true
reportUnknownArgumentType = false
reportUnknownLambdaType = false
reportUnknownMemberType = false
reportUnknownParameterType = false
Expand Down
14 changes: 8 additions & 6 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from collections.abc import Iterable
from contextlib import (
AbstractContextManager,
nullcontext,
Expand All @@ -11,6 +12,7 @@
Any,
Final,
Literal,
cast,
get_args,
get_origin,
)
Expand Down Expand Up @@ -140,17 +142,17 @@ def check(
elif isinstance(actual, pd.Index):
value = actual[index_to_check_for_type]
elif isinstance(actual, BaseGroupBy):
value = actual.obj # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
elif hasattr(actual, "__iter__"):
value = next(
iter(actual) # pyright: ignore[reportArgumentType,reportCallIssue]
)
value = actual.obj # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
elif isinstance(actual, Iterable):
value = next(iter(cast("Iterable[Any]", actual)))
else:
assert hasattr(actual, attr)
value = getattr(actual, attr)

if not isinstance(value, dtype):
raise RuntimeError(f"Expected type '{dtype}' but got '{type(value)}'")
raise RuntimeError(
f"Expected type '{dtype}' but got '{type(value)}'" # pyright: ignore[reportUnknownArgumentType]
)
return actual


Expand Down
10 changes: 7 additions & 3 deletions tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pandas._typing import (
ArrayLike,
AstypeArg,
Dtype,
ListLike,
ScalarIndexer,
SequenceIndexer,
Expand Down Expand Up @@ -199,8 +200,11 @@ def reconstruct(
return DecimalArray._from_sequence(x)

if ufunc.nout > 1:
return tuple(reconstruct(x) for x in result)
return reconstruct(result)
return tuple(
reconstruct(x) # pyright: ignore[reportUnknownArgumentType]
for x in result
)
return reconstruct(result) # pyright: ignore[reportUnknownArgumentType]

def __getitem__(self, item: ScalarIndexer | SequenceIndexer) -> Any:
if isinstance(item, numbers.Integral):
Expand Down Expand Up @@ -241,7 +245,7 @@ def astype(self, dtype: ExtensionDtype, copy: bool = True) -> ExtensionArray: ..
@overload
def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike: ...

def astype(self, dtype, copy=True):
def astype(self, dtype: Dtype, copy: bool = True):
if is_dtype_equal(dtype, self._dtype):
if not copy:
return self
Expand Down
6 changes: 3 additions & 3 deletions tests/frame/test_frame.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pyright: reportUnknownArgumentType=false
from __future__ import annotations

from collections import (
Expand Down Expand Up @@ -76,7 +77,7 @@
_PandasNamedTuple: TypeAlias = tuple

if not PD_LTE_23:
from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip
from pandas.errors import Pandas4Warning # pyright: ignore[reportRedeclaration]
else:
Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef]

Expand Down Expand Up @@ -2315,8 +2316,7 @@ def test_types_rename_axis() -> None:
check(
assert_type(
df.rename_axis(
index=lambda name: name.upper(),
columns=lambda name: name.upper(),
index=lambda name: name.upper(), columns=lambda name: name.upper()
),
pd.DataFrame,
),
Expand Down
2 changes: 1 addition & 1 deletion tests/frame/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_types_groupby() -> None:
# GH 284
df.groupby(df["col1"] > 2)
df.groupby([df["col1"] > 2, df["col2"] % 2 == 1])
df.groupby(lambda x: x)
df.groupby(lambda x: x) # pyright: ignore[reportUnknownArgumentType]
df.groupby([lambda x: x % 2, lambda x: x % 3])
df.groupby(np.array([1, 0, 1]))
df.groupby([np.array([1, 0, 0]), np.array([0, 0, 1])])
Expand Down
8 changes: 7 additions & 1 deletion tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,13 @@ def test_index_view() -> None:
# - pyright: ndarray[tuple[Any, ...], dtype[Any]]
check(assert_type(ind.view(np.ndarray), np.ndarray), np.ndarray) # type: ignore[assert-type]
else:
check(assert_type(ind.view(np.ndarray), np.ndarray[Any, Any]), np.ndarray)
check(
assert_type(
ind.view(np.ndarray), # pyright: ignore[reportUnknownArgumentType]
np.ndarray[Any, Any],
),
np.ndarray,
)

if sys.version_info >= (3, 11):

Expand Down
2 changes: 1 addition & 1 deletion tests/scalars/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
)

if not PD_LTE_23:
from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip
from pandas.errors import Pandas4Warning # pyright: ignore[reportRedeclaration]
else:
Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef]

Expand Down
36 changes: 6 additions & 30 deletions tests/series/test_series.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pyright: reportUnknownArgumentType=false
from __future__ import annotations

from collections.abc import (
Expand Down Expand Up @@ -102,7 +103,7 @@
)

if not PD_LTE_23:
from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip
from pandas.errors import Pandas4Warning # pyright: ignore[reportRedeclaration]
else:
Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef]

Expand Down Expand Up @@ -1160,20 +1161,8 @@ def transform_func(
pd.Series,
float,
)
check(
assert_type(
s.groupby(lambda x: x).transform("mean"),
"pd.Series",
),
pd.Series,
)
check(
assert_type(
s.groupby(lambda x: x).transform("first"),
"pd.Series",
),
pd.Series,
)
check(assert_type(s.groupby(lambda x: x).transform("mean"), pd.Series), pd.Series)
check(assert_type(s.groupby(lambda x: x).transform("first"), pd.Series), pd.Series)


def test_types_groupby_aggregate() -> None:
Expand Down Expand Up @@ -3254,11 +3243,7 @@ def first_arg_series(
check(
assert_type(
ser.pipe(
first_arg_series,
1,
[1.0, 2.0],
argument_2="hi",
keyword_only=(1, 2),
first_arg_series, 1, [1.0, 2.0], argument_2="hi", keyword_only=(1, 2)
),
pd.Series,
),
Expand Down Expand Up @@ -3319,16 +3304,7 @@ def first_arg_series(
def first_arg_not_series(argument_1: int, ser: pd.Series) -> pd.Series:
return ser

check(
assert_type(
ser.pipe(
(first_arg_not_series, "ser"),
1,
),
pd.Series,
),
pd.Series,
)
check(assert_type(ser.pipe((first_arg_not_series, "ser"), 1), pd.Series), pd.Series)

if TYPE_CHECKING_INVALID_USAGE:
ser.pipe(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api_typing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pyright: reportMissingTypeArgument=false
# pyright: reportMissingTypeArgument=false, reportUnknownArgumentType=false
"""Test module for classes in pandas.api.typing."""

from typing import TypeAlias
Expand Down
26 changes: 22 additions & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,12 @@ def test_excel_reader() -> None:
def test_excel_writer() -> None:
with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path) as ew:
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
check(
assert_type(
ew, pd.ExcelWriter # pyright: ignore[reportUnknownArgumentType]
),
pd.ExcelWriter,
)
DF.to_excel(ew, sheet_name="A")
check(assert_type(read_excel(path, sheet_name="A"), DataFrame), DataFrame)
check(assert_type(read_excel(path), DataFrame), DataFrame)
Expand Down Expand Up @@ -1188,7 +1193,13 @@ def test_excel_writer_io() -> None:
def test_excel_writer_engine() -> None:
with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path, engine="auto") as ew:
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
check(
assert_type(
ew,
pd.ExcelWriter, # pyright: ignore[reportUnknownArgumentType]
),
pd.ExcelWriter,
)
DF.to_excel(ew, sheet_name="A")

with ensure_clean(".xlsx") as path:
Expand All @@ -1206,7 +1217,12 @@ def test_excel_writer_engine() -> None:
with ensure_clean(".ods") as path:
with pd.ExcelWriter(path, engine="odf") as ew:
check(
assert_type(ew, pd.ExcelWriter[OpenDocument]),
assert_type(
ew,
pd.ExcelWriter[
OpenDocument
], # pyright: ignore[reportUnknownArgumentType]
),
pd.ExcelWriter[OpenDocument],
)
DF.to_excel(ew, sheet_name="A")
Expand All @@ -1218,7 +1234,9 @@ def test_excel_writer_engine() -> None:
with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path, engine="xlsxwriter") as ew:
check(
assert_type(ew, pd.ExcelWriter[XlsxWorkbook]),
assert_type(
ew, pd.ExcelWriter[XlsxWorkbook]
), # pyright: ignore[reportUnknownArgumentType]
pd.ExcelWriter[XlsxWorkbook],
)
DF.to_excel(ew, sheet_name="A")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_natype.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_arithmetic() -> None:
# )
# https://github.com/microsoft/pyright/issues/10899.
check(
assert_type(
assert_type( # pyright: ignore[reportUnknownArgumentType]
divmod( # pyright: ignore[reportCallIssue, reportAssertTypeFailure]
na, 1 # pyright: ignore[reportArgumentType]
),
Expand Down
7 changes: 3 additions & 4 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ def test_concat_series_mixed_numeric() -> None:
)
check(
assert_type(
pd.concat([s, s2], keys=["first", "second"], names=None),
pd.Series,
pd.concat([s, s2], keys=["first", "second"], names=None), pd.Series
),
pd.Series,
np.floating,
Expand Down Expand Up @@ -527,7 +526,7 @@ def test_isna() -> None:
if not pd.notna(nullable1):
assert_type(nullable1, NaTType | NAType | None)

nullable2: int | None = random.choice([2, None])
nullable2 = random.choice([2, None])
if pd.notna(nullable2):
check(assert_type(nullable2, int), int)
if not pd.isna(nullable2):
Expand Down Expand Up @@ -1965,7 +1964,7 @@ def f(x: pd.Series) -> float:
)

def g(x: pd.Series) -> int:
return int(np.round(x.sum()))
return int(np.round(x.sum())) # pyright: ignore[reportUnknownArgumentType]

check(
assert_type(
Expand Down
16 changes: 2 additions & 14 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,8 @@ def test_plot_barh(close_figures: None) -> None:
def test_plot_box(close_figures: None) -> None:
check(assert_type(IRIS_DF.plot.box(), Axes), Axes)
check(assert_type(IRIS_DF.plot(kind="box"), Axes), Axes)
check(
assert_type(
IRIS_DF.plot.box(subplots=True),
pd.Series,
),
pd.Series,
)
check(
assert_type(
IRIS_DF.plot(kind="box", subplots=True),
pd.Series,
),
pd.Series,
)
check(assert_type(IRIS_DF.plot.box(subplots=True), pd.Series), pd.Series)
check(assert_type(IRIS_DF.plot(kind="box", subplots=True), pd.Series), pd.Series)


def test_plot_density(close_figures: None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)

if not PD_LTE_23:
from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip
from pandas.errors import Pandas4Warning # pyright: ignore[reportRedeclaration]
else:
Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef]

Expand Down
4 changes: 2 additions & 2 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
)

if not PD_LTE_23:
from pandas.errors import Pandas4Warning # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue,reportRedeclaration] # isort: skip
from pandas.errors import Pandas4Warning # pyright: ignore[reportRedeclaration]
else:
Pandas4Warning: TypeAlias = FutureWarning # type: ignore[no-redef]

Expand Down Expand Up @@ -1646,7 +1646,7 @@ def test_timedeltaseries_add_timestampseries() -> None:
def test_timestamp_strptime_fails() -> None:
if TYPE_CHECKING_INVALID_USAGE:
assert_never(
pd.Timestamp.strptime(
pd.Timestamp.strptime( # pyright: ignore[reportUnknownArgumentType]
"2023-02-16", # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
"%Y-%M-%D", # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
)
Expand Down