Skip to content

Commit b09f001

Browse files
twoertweinnoatamir
authored andcommitted
TYP: Autotyping (pandas-dev#48191)
* annotate-magics * annotate-imprecise-magics * none-return * scalar-return * pyi files * ignore vendored file * manual changes * ignore pyright in pickle_compat (these errors would be legit if the current __new__ methods were called but I think these pickle tests call older __new__ methods which allowed providing multiple positional arguments) * run autotyping in pre-commit * remove final and expand safe (and add annotate-imprecise-magics)
1 parent 250d886 commit b09f001

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+310
-195
lines changed

.libcst.codemod.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# String that LibCST should look for in code which indicates that the
2+
# module is generated code.
3+
generated_code_marker: '@generated'
4+
# Command line and arguments for invoking a code formatter. Anything
5+
# specified here must be capable of taking code via stdin and returning
6+
# formatted code via stdout.
7+
formatter: ['black', '-']
8+
# List of regex patterns which LibCST will evaluate against filenames to
9+
# determine if the module should be touched.
10+
blacklist_patterns: []
11+
# List of modules that contain codemods inside of them.
12+
modules:
13+
- 'libcst.codemod.commands'
14+
- 'autotyping'
15+
# Absolute or relative path of the repository root, used for providing
16+
# full-repo metadata. Relative paths should be specified with this file
17+
# location as the base.
18+
repo_root: '.'

.pre-commit-config.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,14 @@ repos:
256256
/(__init__\.py)|(api\.py)|(_version\.py)|(testing\.py)|(conftest\.py)$
257257
|/tests/
258258
|/_testing/
259+
- id: autotyping
260+
name: autotyping
261+
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --none-return --scalar-return --annotate-magics --annotate-imprecise-magics
262+
types_or: [python, pyi]
263+
files: ^pandas
264+
exclude: ^(pandas/tests|pandas/io/clipboard)
265+
language: python
266+
additional_dependencies:
267+
- autotyping==22.9.0
268+
- black==22.6.0
269+
- libcst==0.4.7

pandas/_libs/arrays.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from pandas._typing import (
1010
class NDArrayBacked:
1111
_dtype: DtypeObj
1212
_ndarray: np.ndarray
13-
def __init__(self, values: np.ndarray, dtype: DtypeObj): ...
13+
def __init__(self, values: np.ndarray, dtype: DtypeObj) -> None: ...
1414
@classmethod
1515
def _simple_new(cls, values: np.ndarray, dtype: DtypeObj): ...
1616
def _from_backing_data(self, values: np.ndarray): ...

pandas/_libs/hashtable.pyi

+16-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def unique_label_indices(
1313

1414
class Factorizer:
1515
count: int
16-
def __init__(self, size_hint: int): ...
16+
def __init__(self, size_hint: int) -> None: ...
1717
def get_count(self) -> int: ...
1818

1919
class ObjectFactorizer(Factorizer):
@@ -39,80 +39,80 @@ class Int64Factorizer(Factorizer):
3939
) -> npt.NDArray[np.intp]: ...
4040

4141
class Int64Vector:
42-
def __init__(self, *args): ...
42+
def __init__(self, *args) -> None: ...
4343
def __len__(self) -> int: ...
4444
def to_array(self) -> npt.NDArray[np.int64]: ...
4545

4646
class Int32Vector:
47-
def __init__(self, *args): ...
47+
def __init__(self, *args) -> None: ...
4848
def __len__(self) -> int: ...
4949
def to_array(self) -> npt.NDArray[np.int32]: ...
5050

5151
class Int16Vector:
52-
def __init__(self, *args): ...
52+
def __init__(self, *args) -> None: ...
5353
def __len__(self) -> int: ...
5454
def to_array(self) -> npt.NDArray[np.int16]: ...
5555

5656
class Int8Vector:
57-
def __init__(self, *args): ...
57+
def __init__(self, *args) -> None: ...
5858
def __len__(self) -> int: ...
5959
def to_array(self) -> npt.NDArray[np.int8]: ...
6060

6161
class UInt64Vector:
62-
def __init__(self, *args): ...
62+
def __init__(self, *args) -> None: ...
6363
def __len__(self) -> int: ...
6464
def to_array(self) -> npt.NDArray[np.uint64]: ...
6565

6666
class UInt32Vector:
67-
def __init__(self, *args): ...
67+
def __init__(self, *args) -> None: ...
6868
def __len__(self) -> int: ...
6969
def to_array(self) -> npt.NDArray[np.uint32]: ...
7070

7171
class UInt16Vector:
72-
def __init__(self, *args): ...
72+
def __init__(self, *args) -> None: ...
7373
def __len__(self) -> int: ...
7474
def to_array(self) -> npt.NDArray[np.uint16]: ...
7575

7676
class UInt8Vector:
77-
def __init__(self, *args): ...
77+
def __init__(self, *args) -> None: ...
7878
def __len__(self) -> int: ...
7979
def to_array(self) -> npt.NDArray[np.uint8]: ...
8080

8181
class Float64Vector:
82-
def __init__(self, *args): ...
82+
def __init__(self, *args) -> None: ...
8383
def __len__(self) -> int: ...
8484
def to_array(self) -> npt.NDArray[np.float64]: ...
8585

8686
class Float32Vector:
87-
def __init__(self, *args): ...
87+
def __init__(self, *args) -> None: ...
8888
def __len__(self) -> int: ...
8989
def to_array(self) -> npt.NDArray[np.float32]: ...
9090

9191
class Complex128Vector:
92-
def __init__(self, *args): ...
92+
def __init__(self, *args) -> None: ...
9393
def __len__(self) -> int: ...
9494
def to_array(self) -> npt.NDArray[np.complex128]: ...
9595

9696
class Complex64Vector:
97-
def __init__(self, *args): ...
97+
def __init__(self, *args) -> None: ...
9898
def __len__(self) -> int: ...
9999
def to_array(self) -> npt.NDArray[np.complex64]: ...
100100

101101
class StringVector:
102-
def __init__(self, *args): ...
102+
def __init__(self, *args) -> None: ...
103103
def __len__(self) -> int: ...
104104
def to_array(self) -> npt.NDArray[np.object_]: ...
105105

106106
class ObjectVector:
107-
def __init__(self, *args): ...
107+
def __init__(self, *args) -> None: ...
108108
def __len__(self) -> int: ...
109109
def to_array(self) -> npt.NDArray[np.object_]: ...
110110

111111
class HashTable:
112112
# NB: The base HashTable class does _not_ actually have these methods;
113113
# we are putting the here for the sake of mypy to avoid
114114
# reproducing them in each subclass below.
115-
def __init__(self, size_hint: int = ...): ...
115+
def __init__(self, size_hint: int = ...) -> None: ...
116116
def __len__(self) -> int: ...
117117
def __contains__(self, key: Hashable) -> bool: ...
118118
def sizeof(self, deep: bool = ...) -> int: ...

pandas/_libs/index.pyi

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ from pandas.core.arrays import ExtensionArray
77

88
class IndexEngine:
99
over_size_threshold: bool
10-
def __init__(self, values: np.ndarray): ...
10+
def __init__(self, values: np.ndarray) -> None: ...
1111
def __contains__(self, val: object) -> bool: ...
12+
1213
# -> int | slice | np.ndarray[bool]
1314
def get_loc(self, val: object) -> int | slice | np.ndarray: ...
1415
def sizeof(self, deep: bool = ...) -> int: ...
@@ -49,12 +50,13 @@ class BoolEngine(UInt8Engine): ...
4950
class BaseMultiIndexCodesEngine:
5051
levels: list[np.ndarray]
5152
offsets: np.ndarray # ndarray[uint64_t, ndim=1]
53+
5254
def __init__(
5355
self,
5456
levels: list[np.ndarray], # all entries hashable
5557
labels: list[np.ndarray], # all entries integer-dtyped
5658
offsets: np.ndarray, # np.ndarray[np.uint64, ndim=1]
57-
): ...
59+
) -> None: ...
5860
def get_indexer(
5961
self,
6062
target: npt.NDArray[np.object_],
@@ -69,7 +71,7 @@ class BaseMultiIndexCodesEngine:
6971
) -> npt.NDArray[np.intp]: ...
7072

7173
class ExtensionEngine:
72-
def __init__(self, values: ExtensionArray): ...
74+
def __init__(self, values: ExtensionArray) -> None: ...
7375
def __contains__(self, val: object) -> bool: ...
7476
def get_loc(self, val: object) -> int | slice | np.ndarray: ...
7577
def get_indexer(self, values: np.ndarray) -> npt.NDArray[np.intp]: ...

pandas/_libs/internals.pyi

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def update_blklocs_and_blknos(
3434
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...
3535
@final
3636
class BlockPlacement:
37-
def __init__(self, val: int | slice | np.ndarray): ...
37+
def __init__(self, val: int | slice | np.ndarray) -> None: ...
3838
@property
3939
def indexer(self) -> np.ndarray | slice: ...
4040
@property
@@ -57,7 +57,9 @@ class SharedBlock:
5757
_mgr_locs: BlockPlacement
5858
ndim: int
5959
values: ArrayLike
60-
def __init__(self, values: ArrayLike, placement: BlockPlacement, ndim: int): ...
60+
def __init__(
61+
self, values: ArrayLike, placement: BlockPlacement, ndim: int
62+
) -> None: ...
6163

6264
class NumpyBlock(SharedBlock):
6365
values: np.ndarray
@@ -80,6 +82,6 @@ class BlockManager:
8082
_blklocs: np.ndarray
8183
def __init__(
8284
self, blocks: tuple[B, ...], axes: list[Index], verify_integrity=...
83-
): ...
85+
) -> None: ...
8486
def get_slice(self: T, slobj: slice, axis: int = ...) -> T: ...
8587
def _rebuild_blknos_and_blklocs(self) -> None: ...

pandas/_libs/parsers.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TextReader:
6262
float_precision: Literal["round_trip", "legacy", "high"] | None = ...,
6363
skip_blank_lines: bool = ...,
6464
encoding_errors: bytes | str = ...,
65-
): ...
65+
) -> None: ...
6666
def set_noconvert(self, i: int) -> None: ...
6767
def remove_noconvert(self, i: int) -> None: ...
6868
def close(self) -> None: ...

pandas/_libs/sparse.pyi

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ _SparseIndexT = TypeVar("_SparseIndexT", bound=SparseIndex)
1212
class SparseIndex:
1313
length: int
1414
npoints: int
15-
def __init__(self): ...
15+
def __init__(self) -> None: ...
1616
@property
1717
def ngaps(self) -> int: ...
1818
@property
@@ -31,13 +31,15 @@ class IntIndex(SparseIndex):
3131
indices: npt.NDArray[np.int32]
3232
def __init__(
3333
self, length: int, indices: Sequence[int], check_integrity: bool = ...
34-
): ...
34+
) -> None: ...
3535

3636
class BlockIndex(SparseIndex):
3737
nblocks: int
3838
blocs: np.ndarray
3939
blengths: np.ndarray
40-
def __init__(self, length: int, blocs: np.ndarray, blengths: np.ndarray): ...
40+
def __init__(
41+
self, length: int, blocs: np.ndarray, blengths: np.ndarray
42+
) -> None: ...
4143

4244
def make_mask_object_ndarray(
4345
arr: npt.NDArray[np.object_], fill_value

pandas/_testing/contexts.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from shutil import rmtree
77
import tempfile
8+
from types import TracebackType
89
from typing import (
910
IO,
1011
Any,
@@ -65,7 +66,7 @@ def set_timezone(tz: str) -> Generator[None, None, None]:
6566
import os
6667
import time
6768

68-
def setTZ(tz):
69+
def setTZ(tz) -> None:
6970
if tz is None:
7071
try:
7172
del os.environ["TZ"]
@@ -239,6 +240,11 @@ def __enter__(self) -> None:
239240
self.start_state = np.random.get_state()
240241
np.random.seed(self.seed)
241242

242-
def __exit__(self, exc_type, exc_value, traceback) -> None:
243+
def __exit__(
244+
self,
245+
exc_type: type[BaseException] | None,
246+
exc_value: BaseException | None,
247+
traceback: TracebackType | None,
248+
) -> None:
243249

244250
np.random.set_state(self.start_state)

pandas/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
334334
return pieces
335335

336336

337-
def plus_or_dot(pieces):
337+
def plus_or_dot(pieces) -> str:
338338
"""Return a + if we don't already have one, else return a ."""
339339
if "+" in pieces.get("closest-tag", ""):
340340
return "."

pandas/compat/pickle_compat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def find_class(self, module, name):
210210
Unpickler.dispatch[pkl.REDUCE[0]] = load_reduce
211211

212212

213-
def load_newobj(self):
213+
def load_newobj(self) -> None:
214214
args = self.stack.pop()
215215
cls = self.stack[-1]
216216

@@ -234,7 +234,7 @@ def load_newobj(self):
234234
Unpickler.dispatch[pkl.NEWOBJ[0]] = load_newobj
235235

236236

237-
def load_newobj_ex(self):
237+
def load_newobj_ex(self) -> None:
238238
kwargs = self.stack.pop()
239239
args = self.stack.pop()
240240
cls = self.stack.pop()

pandas/conftest.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
from decimal import Decimal
3030
import operator
3131
import os
32-
from typing import Callable
32+
from typing import (
33+
Callable,
34+
Iterator,
35+
)
3336

3437
from dateutil.tz import (
3538
tzlocal,
@@ -132,7 +135,7 @@ def ignore_doctest_warning(item: pytest.Item, path: str, message: str) -> None:
132135
item.add_marker(pytest.mark.filterwarnings(f"ignore:{message}"))
133136

134137

135-
def pytest_collection_modifyitems(items, config):
138+
def pytest_collection_modifyitems(items, config) -> None:
136139
skip_slow = config.getoption("--skip-slow")
137140
only_slow = config.getoption("--only-slow")
138141
skip_network = config.getoption("--skip-network")
@@ -512,10 +515,10 @@ def __init__(self, underlying_dict) -> None:
512515
def __getitem__(self, key):
513516
return self._data.__getitem__(key)
514517

515-
def __iter__(self):
518+
def __iter__(self) -> Iterator:
516519
return self._data.__iter__()
517520

518-
def __len__(self):
521+
def __len__(self) -> int:
519522
return self._data.__len__()
520523

521524
return TestNonDictMapping

pandas/core/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _delegate_method(self, name, *args, **kwargs):
5959
@classmethod
6060
def _add_delegate_accessors(
6161
cls, delegate, accessors, typ: str, overwrite: bool = False
62-
):
62+
) -> None:
6363
"""
6464
Add accessors to cls from the delegate class.
6565

pandas/core/array_algos/take.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def _get_take_nd_function(
337337

338338
if func is None:
339339

340-
def func(arr, indexer, out, fill_value=np.nan):
340+
def func(arr, indexer, out, fill_value=np.nan) -> None:
341341
indexer = ensure_platform_int(indexer)
342342
_take_nd_object(
343343
arr, indexer, out, axis=axis, fill_value=fill_value, mask_info=mask_info
@@ -349,7 +349,7 @@ def func(arr, indexer, out, fill_value=np.nan):
349349
def _view_wrapper(f, arr_dtype=None, out_dtype=None, fill_wrap=None):
350350
def wrapper(
351351
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
352-
):
352+
) -> None:
353353
if arr_dtype is not None:
354354
arr = arr.view(arr_dtype)
355355
if out_dtype is not None:
@@ -364,7 +364,7 @@ def wrapper(
364364
def _convert_wrapper(f, conv_dtype):
365365
def wrapper(
366366
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
367-
):
367+
) -> None:
368368
if conv_dtype == object:
369369
# GH#39755 avoid casting dt64/td64 to integers
370370
arr = ensure_wrapped_if_datetimelike(arr)
@@ -506,7 +506,7 @@ def _take_nd_object(
506506
axis: int,
507507
fill_value,
508508
mask_info,
509-
):
509+
) -> None:
510510
if mask_info is not None:
511511
mask, needs_masking = mask_info
512512
else:

0 commit comments

Comments
 (0)