From b3d90446aa340de967e5fd1368358adc8e07c4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Tue, 25 Jul 2023 16:25:31 -0400 Subject: [PATCH 1/4] STYL: replace autotyping with ruff's ANN autofixes --- .libcst.codemod.yaml | 18 ---------- .pre-commit-config.yaml | 14 ++------ pandas/core/arrays/sparse/array.py | 2 +- pandas/core/arrays/string_.py | 2 +- pandas/core/frame.py | 2 +- pandas/core/indexes/base.py | 2 +- pandas/core/indexing.py | 2 +- pandas/io/clipboard/__init__.py | 2 +- pandas/tests/extension/date/array.py | 2 +- pandas/tests/extension/decimal/array.py | 2 +- pandas/tests/extension/json/array.py | 2 +- scripts/run_autotyping.py | 48 ------------------------- 12 files changed, 12 insertions(+), 86 deletions(-) delete mode 100644 .libcst.codemod.yaml delete mode 100644 scripts/run_autotyping.py diff --git a/.libcst.codemod.yaml b/.libcst.codemod.yaml deleted file mode 100644 index 985e69b772e52..0000000000000 --- a/.libcst.codemod.yaml +++ /dev/null @@ -1,18 +0,0 @@ -# String that LibCST should look for in code which indicates that the -# module is generated code. -generated_code_marker: '@generated' -# Command line and arguments for invoking a code formatter. Anything -# specified here must be capable of taking code via stdin and returning -# formatted code via stdout. -formatter: ['black', '-'] -# List of regex patterns which LibCST will evaluate against filenames to -# determine if the module should be touched. -blacklist_patterns: [] -# List of modules that contain codemods inside of them. -modules: -- 'libcst.codemod.commands' -- 'autotyping' -# Absolute or relative path of the repository root, used for providing -# full-repo metadata. Relative paths should be specified with this file -# location as the base. -repo_root: '.' diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 366db4337b0e1..df81a37f6892f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,6 +25,9 @@ repos: hooks: - id: ruff args: [--exit-non-zero-on-fix] + - id: ruff + name: ruff's autotyping/flake8-annotations autofixes + args: [--select, ANN, --fix-only, --exit-non-zero-on-fix] - repo: https://github.com/jendrikseipp/vulture rev: 'v2.7' hooks: @@ -371,17 +374,6 @@ repos: /(__init__\.py)|(api\.py)|(_version\.py)|(testing\.py)|(conftest\.py)$ |/tests/ |/_testing/ - - id: autotyping - name: autotyping - entry: python -m scripts.run_autotyping - types_or: [python, pyi] - files: ^pandas - exclude: ^(pandas/tests|pandas/_version.py|pandas/io/clipboard) - language: python - stages: [manual] - additional_dependencies: - - autotyping==23.3.0 - - libcst==0.4.9 - id: check-test-naming name: check that test names start with 'test' entry: python -m scripts.check_test_naming diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index f666f4f8459d7..d832a9f772f45 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -575,7 +575,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray: out[self.sp_index.indices] = self.sp_values return out - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: # I suppose we could allow setting of non-fill_value elements. # TODO(SparseArray.__setitem__): remove special cases in # ExtensionBlock.where diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index 7c834d2e26b3a..25f1c2ec6ce4f 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -401,7 +401,7 @@ def _values_for_factorize(self): arr[mask] = None return arr, None - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: value = extract_array(value, extract_numpy=True) if isinstance(value, type(self)): # extract_array doesn't extract NumpyExtensionArray subclasses diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 75a177f0969ca..ff83e9223b267 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4031,7 +4031,7 @@ def isetitem(self, loc, value) -> None: arraylike, refs = self._sanitize_column(value) self._iset_item_mgr(loc, arraylike, inplace=False, refs=refs) - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: if not PYPY and using_copy_on_write(): if sys.getrefcount(self) <= 3: warnings.warn( diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2f044905d33f9..9758a30ad6fe7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5328,7 +5328,7 @@ def __contains__(self, key: Any) -> bool: __hash__: ClassVar[None] # type: ignore[assignment] @final - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: raise TypeError("Index does not support mutable operations") def __getitem__(self, key): diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index ebe37d605ecbb..ca0b1705b5c38 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2486,7 +2486,7 @@ def __getitem__(self, key): return super().__getitem__(key) - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: if self.ndim == 2 and not self._axes_are_unique: # GH#33041 fall back to .loc if not isinstance(key, tuple) or not all(is_scalar(x) for x in key): diff --git a/pandas/io/clipboard/__init__.py b/pandas/io/clipboard/__init__.py index c07f51d875d4d..c35d4a44ee62c 100644 --- a/pandas/io/clipboard/__init__.py +++ b/pandas/io/clipboard/__init__.py @@ -315,7 +315,7 @@ def __call__(self, *args): raise PyperclipWindowsException("Error calling " + self.f.__name__) return ret - def __setattr__(self, key, value): + def __setattr__(self, key, value) -> None: setattr(self.f, key, value) diff --git a/pandas/tests/extension/date/array.py b/pandas/tests/extension/date/array.py index 8c7b17cd8e3b8..39accd6d223a7 100644 --- a/pandas/tests/extension/date/array.py +++ b/pandas/tests/extension/date/array.py @@ -148,7 +148,7 @@ def __getitem__(self, item: PositionalIndexer): else: raise NotImplementedError("only ints are supported as indexes") - def __setitem__(self, key: int | slice | np.ndarray, value: Any): + def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: if not isinstance(key, int): raise NotImplementedError("only ints are supported as indexes") diff --git a/pandas/tests/extension/decimal/array.py b/pandas/tests/extension/decimal/array.py index 3101bbd171f75..5762bc9ce485c 100644 --- a/pandas/tests/extension/decimal/array.py +++ b/pandas/tests/extension/decimal/array.py @@ -190,7 +190,7 @@ def astype(self, dtype, copy=True): return super().astype(dtype, copy=copy) - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: if is_list_like(value): if is_scalar(key): raise ValueError("setting an array element with a sequence.") diff --git a/pandas/tests/extension/json/array.py b/pandas/tests/extension/json/array.py index 8495ffbbbe70d..ed1855fdf6fbb 100644 --- a/pandas/tests/extension/json/array.py +++ b/pandas/tests/extension/json/array.py @@ -117,7 +117,7 @@ def __getitem__(self, item): # integer return type(self)([self.data[i] for i in item]) - def __setitem__(self, key, value): + def __setitem__(self, key, value) -> None: if isinstance(key, numbers.Integral): self.data[key] = value else: diff --git a/scripts/run_autotyping.py b/scripts/run_autotyping.py deleted file mode 100644 index 8729085f0dd4b..0000000000000 --- a/scripts/run_autotyping.py +++ /dev/null @@ -1,48 +0,0 @@ -""" -Script to run ``autotyping``, to get around the fact that -pre-commit puts ``args`` before the list of files, whereas -``autotyping`` wants the files to come after, see -https://github.com/pandas-dev/pandas/issues/48808#issuecomment-1259711679. -""" -from __future__ import annotations - -import argparse -import subprocess -import sys -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from collections.abc import Sequence - - -def main(argv: Sequence[str] | None = None) -> None: - parser = argparse.ArgumentParser() - parser.add_argument("paths", nargs="*") - args = parser.parse_args(argv) - if not args.paths: - sys.exit(0) - output = subprocess.run( - [ - "python", - "-m", - "libcst.tool", - "codemod", - "autotyping.AutotypeCommand", - *args.paths, - "--no-format", - "--safe", - # all except 'guess-common-names' from 'aggresive' - "--bool-param", - "--int-param", - "--float-param", - "--str-param", - "--bytes-param", - "--annotate-imprecise-magics", - ], - check=True, - ) - sys.exit(output.returncode) - - -if __name__ == "__main__": - main() From 45b40967c7c2c15d1c0a8b134699b41643aa5afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 26 Jul 2023 11:26:43 -0400 Subject: [PATCH 2/4] ignore vendored clipboard file from ruff --- pandas/io/clipboard/__init__.py | 2 +- pyproject.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/io/clipboard/__init__.py b/pandas/io/clipboard/__init__.py index c35d4a44ee62c..c07f51d875d4d 100644 --- a/pandas/io/clipboard/__init__.py +++ b/pandas/io/clipboard/__init__.py @@ -315,7 +315,7 @@ def __call__(self, *args): raise PyperclipWindowsException("Error calling " + self.f.__name__) return ret - def __setattr__(self, key, value) -> None: + def __setattr__(self, key, value): setattr(self.f, key, value) diff --git a/pyproject.toml b/pyproject.toml index 36fb58ee396a2..f5e903d0b5c8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -349,6 +349,7 @@ exclude = [ ".eggs/*.py", # vendored files "pandas/util/version/*", + "pandas/io/clipboard/__init__.py", # exclude asv benchmark environments from linting "env", ] From 9d46f4df187c58de883c7e98848a91857faa6352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Thu, 27 Jul 2023 09:22:34 -0400 Subject: [PATCH 3/4] comment to remove it in the future --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index df81a37f6892f..f5c149f05eca6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,6 +26,7 @@ repos: - id: ruff args: [--exit-non-zero-on-fix] - id: ruff + # TODO: remove this when ANN204 is enabled (ANN001 might have autofixes in the future) name: ruff's autotyping/flake8-annotations autofixes args: [--select, ANN, --fix-only, --exit-non-zero-on-fix] - repo: https://github.com/jendrikseipp/vulture From febbf1a03a84009640679ad17939d51584284846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 28 Jul 2023 10:29:41 -0400 Subject: [PATCH 4/4] alias --- .pre-commit-config.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f5c149f05eca6..c41840cf78399 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,9 +26,10 @@ repos: - id: ruff args: [--exit-non-zero-on-fix] - id: ruff - # TODO: remove this when ANN204 is enabled (ANN001 might have autofixes in the future) - name: ruff's autotyping/flake8-annotations autofixes - args: [--select, ANN, --fix-only, --exit-non-zero-on-fix] + # TODO: remove autofixe-only rules when they are checked by ruff + name: ruff-selected-autofixes + alias: ruff-selected-autofixes + args: [--select, "ANN001,ANN204", --fix-only, --exit-non-zero-on-fix] - repo: https://github.com/jendrikseipp/vulture rev: 'v2.7' hooks: