Skip to content

STYL: replace autotyping with ruff's ANN autofixes #54260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 0 additions & 18 deletions .libcst.codemod.yaml

This file was deleted.

15 changes: 4 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ repos:
hooks:
- 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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just include ANN as a rule in pyproject.toml?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish, but there are way too many violations (ANN wants everything to be annotated) and only some of them have in some cases an autofix. I would like only to use the autofixes. If there is a better way to achieve that, I'm all for that - but I didn't find one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm since it appears ANN204 is the only auto-fix rule so far, could we just include that code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me put this back to draft and wait until #54271 is merged

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 191 methods without return annotations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I currently do not have time to add 191 return annotations but I will start working on it in the future.

ruff might have autofixes for ANN001 in the future astral-sh/ruff#1640

- repo: https://github.com/jendrikseipp/vulture
rev: 'v2.7'
hooks:
Expand Down Expand Up @@ -371,17 +375,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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/date/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ exclude = [
".eggs/*.py",
# vendored files
"pandas/util/version/*",
"pandas/io/clipboard/__init__.py",
# exclude asv benchmark environments from linting
"env",
]
Expand Down
48 changes: 0 additions & 48 deletions scripts/run_autotyping.py

This file was deleted.