Skip to content

Commit 18aa834

Browse files
Backport PR pandas-dev#56617 on branch 2.2.x (TYP: some return types from ruff) (pandas-dev#56624)
Backport PR pandas-dev#56617: TYP: some return types from ruff Co-authored-by: Torsten Wörtwein <[email protected]>
1 parent 8f8b514 commit 18aa834

File tree

16 files changed

+38
-35
lines changed

16 files changed

+38
-35
lines changed

.pre-commit-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ repos:
3232
# TODO: remove autofixe-only rules when they are checked by ruff
3333
name: ruff-selected-autofixes
3434
alias: ruff-selected-autofixes
35+
files: ^pandas
36+
exclude: ^pandas/tests
3537
args: [--select, "ANN001,ANN2", --fix-only, --exit-non-zero-on-fix]
3638
- repo: https://github.com/jendrikseipp/vulture
3739
rev: 'v2.10'

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ Optional libraries below the lowest tested version may still work, but are not c
431431
+-----------------+-----------------+---------+
432432
| Package | Minimum Version | Changed |
433433
+=================+=================+=========+
434-
| mypy (dev) | 1.7.1 | X |
434+
| mypy (dev) | 1.8.0 | X |
435435
+-----------------+-----------------+---------+
436436
| | | X |
437437
+-----------------+-----------------+---------+

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ dependencies:
7676

7777
# code checks
7878
- flake8=6.1.0 # run in subprocess over docstring examples
79-
- mypy=1.7.1 # pre-commit uses locally installed mypy
79+
- mypy=1.8.0 # pre-commit uses locally installed mypy
8080
- tokenize-rt # scripts/check_for_inconsistent_pandas_namespace.py
8181
- pre-commit>=3.6.0
8282

pandas/_testing/asserters.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import (
55
TYPE_CHECKING,
66
Literal,
7+
NoReturn,
78
cast,
89
)
910

@@ -143,7 +144,7 @@ def assert_almost_equal(
143144
)
144145

145146

146-
def _check_isinstance(left, right, cls):
147+
def _check_isinstance(left, right, cls) -> None:
147148
"""
148149
Helper method for our assert_* methods that ensures that
149150
the two objects being compared have the right type before
@@ -576,7 +577,7 @@ def assert_timedelta_array_equal(
576577

577578
def raise_assert_detail(
578579
obj, message, left, right, diff=None, first_diff=None, index_values=None
579-
):
580+
) -> NoReturn:
580581
__tracebackhide__ = True
581582

582583
msg = f"""{obj} are different
@@ -664,7 +665,7 @@ def _get_base(obj):
664665
if left_base is right_base:
665666
raise AssertionError(f"{repr(left_base)} is {repr(right_base)}")
666667

667-
def _raise(left, right, err_msg):
668+
def _raise(left, right, err_msg) -> NoReturn:
668669
if err_msg is None:
669670
if left.shape != right.shape:
670671
raise_assert_detail(

pandas/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
386386
return pieces
387387

388388

389-
def plus_or_dot(pieces):
389+
def plus_or_dot(pieces) -> str:
390390
"""Return a + if we don't already have one, else return a ."""
391391
if "+" in pieces.get("closest-tag", ""):
392392
return "."

pandas/core/computation/expr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ def visit_Call(self, node, side=None, **kwargs):
695695
if not isinstance(key, ast.keyword):
696696
# error: "expr" has no attribute "id"
697697
raise ValueError(
698-
"keyword error in function call " # type: ignore[attr-defined]
699-
f"'{node.func.id}'"
698+
"keyword error in function call "
699+
f"'{node.func.id}'" # type: ignore[attr-defined]
700700
)
701701

702702
if key.arg:

pandas/io/html.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def _attr_getter(self, obj, attr):
269269
# Both lxml and BeautifulSoup have the same implementation:
270270
return obj.get(attr)
271271

272-
def _href_getter(self, obj):
272+
def _href_getter(self, obj) -> str | None:
273273
"""
274274
Return a href if the DOM node contains a child <a> or None.
275275
@@ -392,7 +392,7 @@ def _parse_tables(self, document, match, attrs):
392392
"""
393393
raise AbstractMethodError(self)
394394

395-
def _equals_tag(self, obj, tag):
395+
def _equals_tag(self, obj, tag) -> bool:
396396
"""
397397
Return whether an individual DOM node matches a tag
398398
@@ -629,7 +629,7 @@ def _href_getter(self, obj) -> str | None:
629629
def _text_getter(self, obj):
630630
return obj.text
631631

632-
def _equals_tag(self, obj, tag):
632+
def _equals_tag(self, obj, tag) -> bool:
633633
return obj.name == tag
634634

635635
def _parse_td(self, row):
@@ -758,7 +758,7 @@ def _parse_tables(self, document, match, kwargs):
758758
raise ValueError(f"No tables found matching regex {repr(pattern)}")
759759
return tables
760760

761-
def _equals_tag(self, obj, tag):
761+
def _equals_tag(self, obj, tag) -> bool:
762762
return obj.tag == tag
763763

764764
def _build_doc(self):

pandas/io/json/_json.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def __init__(
255255
self.is_copy = None
256256
self._format_axes()
257257

258-
def _format_axes(self):
258+
def _format_axes(self) -> None:
259259
raise AbstractMethodError(self)
260260

261261
def write(self) -> str:
@@ -287,7 +287,7 @@ def obj_to_write(self) -> NDFrame | Mapping[IndexLabel, Any]:
287287
else:
288288
return self.obj
289289

290-
def _format_axes(self):
290+
def _format_axes(self) -> None:
291291
if not self.obj.index.is_unique and self.orient == "index":
292292
raise ValueError(f"Series index must be unique for orient='{self.orient}'")
293293

@@ -304,7 +304,7 @@ def obj_to_write(self) -> NDFrame | Mapping[IndexLabel, Any]:
304304
obj_to_write = self.obj
305305
return obj_to_write
306306

307-
def _format_axes(self):
307+
def _format_axes(self) -> None:
308308
"""
309309
Try to format axes if they are datelike.
310310
"""
@@ -1193,7 +1193,7 @@ def parse(self):
11931193
self._try_convert_types()
11941194
return self.obj
11951195

1196-
def _parse(self):
1196+
def _parse(self) -> None:
11971197
raise AbstractMethodError(self)
11981198

11991199
@final
@@ -1217,7 +1217,7 @@ def _convert_axes(self) -> None:
12171217
new_axis = Index(new_ser, dtype=new_ser.dtype, copy=False)
12181218
setattr(self.obj, axis_name, new_axis)
12191219

1220-
def _try_convert_types(self):
1220+
def _try_convert_types(self) -> None:
12211221
raise AbstractMethodError(self)
12221222

12231223
@final

pandas/io/parsers/arrow_parser_wrapper.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, src: ReadBuffer[bytes], **kwds) -> None:
4141

4242
self._parse_kwds()
4343

44-
def _parse_kwds(self):
44+
def _parse_kwds(self) -> None:
4545
"""
4646
Validates keywords before passing to pyarrow.
4747
"""
@@ -104,7 +104,7 @@ def _get_pyarrow_options(self) -> None:
104104
] = None # PyArrow raises an exception by default
105105
elif on_bad_lines == ParserBase.BadLineHandleMethod.WARN:
106106

107-
def handle_warning(invalid_row):
107+
def handle_warning(invalid_row) -> str:
108108
warnings.warn(
109109
f"Expected {invalid_row.expected_columns} columns, but found "
110110
f"{invalid_row.actual_columns}: {invalid_row.text}",
@@ -219,7 +219,7 @@ def _finalize_pandas_output(self, frame: DataFrame) -> DataFrame:
219219
raise ValueError(e)
220220
return frame
221221

222-
def _validate_usecols(self, usecols):
222+
def _validate_usecols(self, usecols) -> None:
223223
if lib.is_list_like(usecols) and not all(isinstance(x, str) for x in usecols):
224224
raise ValueError(
225225
"The pyarrow engine does not allow 'usecols' to be integer "

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ def info(self) -> str:
17071707
# ------------------------------------------------------------------------
17081708
# private methods
17091709

1710-
def _check_if_open(self):
1710+
def _check_if_open(self) -> None:
17111711
if not self.is_open:
17121712
raise ClosedFileError(f"{self._path} file is not open!")
17131713

pandas/io/sas/sas_xport.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def close(self) -> None:
288288
def _get_row(self):
289289
return self.filepath_or_buffer.read(80).decode()
290290

291-
def _read_header(self):
291+
def _read_header(self) -> None:
292292
self.filepath_or_buffer.seek(0)
293293

294294
# read file header

pandas/io/sql.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ def _create_sql_schema(
15141514
keys: list[str] | None = None,
15151515
dtype: DtypeArg | None = None,
15161516
schema: str | None = None,
1517-
):
1517+
) -> str:
15181518
pass
15191519

15201520

@@ -2073,7 +2073,7 @@ def _create_sql_schema(
20732073
keys: list[str] | None = None,
20742074
dtype: DtypeArg | None = None,
20752075
schema: str | None = None,
2076-
):
2076+
) -> str:
20772077
table = SQLTable(
20782078
table_name,
20792079
self,
@@ -2433,7 +2433,7 @@ def _create_sql_schema(
24332433
keys: list[str] | None = None,
24342434
dtype: DtypeArg | None = None,
24352435
schema: str | None = None,
2436-
):
2436+
) -> str:
24372437
raise NotImplementedError("not implemented for adbc")
24382438

24392439

@@ -2879,7 +2879,7 @@ def _create_sql_schema(
28792879
keys=None,
28802880
dtype: DtypeArg | None = None,
28812881
schema: str | None = None,
2882-
):
2882+
) -> str:
28832883
table = SQLiteTable(
28842884
table_name,
28852885
self,

pandas/io/stata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def __init__(
687687

688688
self._prepare_value_labels()
689689

690-
def _prepare_value_labels(self):
690+
def _prepare_value_labels(self) -> None:
691691
"""Encode value labels."""
692692

693693
self.text_len = 0

pandas/plotting/_matplotlib/core.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ def _ensure_frame(self, data) -> DataFrame:
662662
return data
663663

664664
@final
665-
def _compute_plot_data(self):
665+
def _compute_plot_data(self) -> None:
666666
data = self.data
667667

668668
# GH15079 reconstruct data if by is defined
@@ -699,7 +699,7 @@ def _compute_plot_data(self):
699699

700700
self.data = numeric_data.apply(type(self)._convert_to_ndarray)
701701

702-
def _make_plot(self, fig: Figure):
702+
def _make_plot(self, fig: Figure) -> None:
703703
raise AbstractMethodError(self)
704704

705705
@final
@@ -745,7 +745,7 @@ def _post_plot_logic(self, ax: Axes, data) -> None:
745745
"""Post process for each axes. Overridden in child classes"""
746746

747747
@final
748-
def _adorn_subplots(self, fig: Figure):
748+
def _adorn_subplots(self, fig: Figure) -> None:
749749
"""Common post process unrelated to data"""
750750
if len(self.axes) > 0:
751751
all_axes = self._get_subplots(fig)
@@ -1323,7 +1323,7 @@ def __init__(
13231323
c = self.data.columns[c]
13241324
self.c = c
13251325

1326-
def _make_plot(self, fig: Figure):
1326+
def _make_plot(self, fig: Figure) -> None:
13271327
x, y, c, data = self.x, self.y, self.c, self.data
13281328
ax = self.axes[0]
13291329

pandas/util/_validators.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
BoolishNoneT = TypeVar("BoolishNoneT", bool, int, None)
2727

2828

29-
def _check_arg_length(fname, args, max_fname_arg_count, compat_args):
29+
def _check_arg_length(fname, args, max_fname_arg_count, compat_args) -> None:
3030
"""
3131
Checks whether 'args' has length of at most 'compat_args'. Raises
3232
a TypeError if that is not the case, similar to in Python when a
@@ -46,7 +46,7 @@ def _check_arg_length(fname, args, max_fname_arg_count, compat_args):
4646
)
4747

4848

49-
def _check_for_default_values(fname, arg_val_dict, compat_args):
49+
def _check_for_default_values(fname, arg_val_dict, compat_args) -> None:
5050
"""
5151
Check that the keys in `arg_val_dict` are mapped to their
5252
default values as specified in `compat_args`.
@@ -125,7 +125,7 @@ def validate_args(fname, args, max_fname_arg_count, compat_args) -> None:
125125
_check_for_default_values(fname, kwargs, compat_args)
126126

127127

128-
def _check_for_invalid_keys(fname, kwargs, compat_args):
128+
def _check_for_invalid_keys(fname, kwargs, compat_args) -> None:
129129
"""
130130
Checks whether 'kwargs' contains any keys that are not
131131
in 'compat_args' and raises a TypeError if there is one.

requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ moto
5353
flask
5454
asv>=0.6.1
5555
flake8==6.1.0
56-
mypy==1.7.1
56+
mypy==1.8.0
5757
tokenize-rt
5858
pre-commit>=3.6.0
5959
gitpython

0 commit comments

Comments
 (0)