From 5eb45771bc81fbcfc7f55e2265a49605b4093d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Mar 2023 22:59:28 -0400 Subject: [PATCH 1/2] CI: update autotyping --- .pre-commit-config.yaml | 4 ++-- pandas/_config/config.py | 2 +- pandas/_testing/_random.py | 4 +++- pandas/_testing/contexts.py | 2 +- pandas/compat/numpy/function.py | 2 +- pandas/core/accessor.py | 18 +++++++-------- pandas/core/arrays/_mixins.py | 4 ++-- pandas/core/arrays/arrow/array.py | 2 +- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/categorical.py | 10 +++++--- pandas/core/arrays/datetimes.py | 2 +- pandas/core/arrays/interval.py | 2 +- pandas/core/arrays/masked.py | 2 +- pandas/core/arrays/period.py | 4 ++-- pandas/core/arrays/sparse/accessor.py | 4 ++-- pandas/core/frame.py | 8 ++++--- pandas/core/generic.py | 12 +++++----- pandas/core/groupby/generic.py | 2 +- pandas/core/groupby/groupby.py | 8 +++---- pandas/core/indexes/accessors.py | 8 ++++--- pandas/core/indexes/base.py | 7 +++++- pandas/core/indexes/category.py | 2 +- pandas/core/indexes/period.py | 2 +- pandas/core/interchange/dataframe.py | 2 +- pandas/core/internals/array_manager.py | 2 +- pandas/core/internals/managers.py | 2 +- pandas/core/missing.py | 14 +++++++---- pandas/core/resample.py | 20 ++++++++-------- pandas/core/reshape/concat.py | 14 +++++------ pandas/core/reshape/encoding.py | 2 +- pandas/core/reshape/merge.py | 4 ++-- pandas/core/series.py | 4 ++-- pandas/core/sorting.py | 2 +- pandas/core/strings/accessor.py | 22 +++++++++--------- pandas/core/strings/base.py | 10 ++++---- pandas/core/strings/object_array.py | 10 ++++---- pandas/core/tools/datetimes.py | 2 +- pandas/io/formats/excel.py | 2 +- pandas/io/html.py | 2 +- pandas/io/json/_json.py | 3 ++- pandas/io/parsers/base_parser.py | 2 +- pandas/io/parsers/readers.py | 10 ++++---- pandas/io/pytables.py | 10 ++++---- pandas/io/sas/sas_xport.py | 4 ++-- pandas/io/sql.py | 32 +++++++++++++------------- pandas/plotting/_core.py | 10 ++++---- pandas/plotting/_matplotlib/boxplot.py | 16 ++++++------- pandas/plotting/_matplotlib/core.py | 10 ++++---- pandas/plotting/_matplotlib/hist.py | 24 ++++++++++--------- pandas/plotting/_matplotlib/misc.py | 2 +- pandas/plotting/_matplotlib/tools.py | 4 ++-- pandas/tseries/holiday.py | 14 +++++------ pandas/util/_validators.py | 5 +++- scripts/run_autotyping.py | 9 +++++++- 54 files changed, 210 insertions(+), 172 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d4baa638bdda2..de36bf2d441c5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -413,8 +413,8 @@ repos: language: python stages: [manual] additional_dependencies: - - autotyping==22.9.0 - - libcst==0.4.7 + - 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/_config/config.py b/pandas/_config/config.py index 56d505d024949..e14f51df24a8a 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -737,7 +737,7 @@ def pp(name: str, ks: Iterable[str]) -> list[str]: @contextmanager -def config_prefix(prefix) -> Generator[None, None, None]: +def config_prefix(prefix: str) -> Generator[None, None, None]: """ contextmanager for multiple invocations of API with a common prefix diff --git a/pandas/_testing/_random.py b/pandas/_testing/_random.py index f0a0437a5bfc6..af8c7b4870f4c 100644 --- a/pandas/_testing/_random.py +++ b/pandas/_testing/_random.py @@ -10,7 +10,9 @@ RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1)) -def rands_array(nchars, size, dtype: NpDtype = "O", replace: bool = True) -> np.ndarray: +def rands_array( + nchars, size: int, dtype: NpDtype = "O", replace: bool = True +) -> np.ndarray: """ Generate an array of byte strings. """ diff --git a/pandas/_testing/contexts.py b/pandas/_testing/contexts.py index db4ddd45db955..fb5b7b967f6bf 100644 --- a/pandas/_testing/contexts.py +++ b/pandas/_testing/contexts.py @@ -154,7 +154,7 @@ def ensure_safe_environment_variables() -> Generator[None, None, None]: @contextmanager -def with_csv_dialect(name, **kwargs) -> Generator[None, None, None]: +def with_csv_dialect(name: str, **kwargs) -> Generator[None, None, None]: """ Context manager to temporarily register a CSV dialect for parsing CSV. diff --git a/pandas/compat/numpy/function.py b/pandas/compat/numpy/function.py index 8b2916bf1ded9..f6e80aba0c34f 100644 --- a/pandas/compat/numpy/function.py +++ b/pandas/compat/numpy/function.py @@ -342,7 +342,7 @@ def validate_take_with_convert(convert: ndarray | bool | None, args, kwargs) -> ) -def validate_groupby_func(name, args, kwargs, allowed=None) -> None: +def validate_groupby_func(name: str, args, kwargs, allowed=None) -> None: """ 'args' and 'kwargs' should be empty, except for allowed kwargs because all of their necessary parameters are explicitly listed in the function diff --git a/pandas/core/accessor.py b/pandas/core/accessor.py index 58da2cd994777..1b36659944561 100644 --- a/pandas/core/accessor.py +++ b/pandas/core/accessor.py @@ -51,13 +51,13 @@ class PandasDelegate: Abstract base class for delegating methods/properties. """ - def _delegate_property_get(self, name, *args, **kwargs): + def _delegate_property_get(self, name: str, *args, **kwargs): raise TypeError(f"You cannot access the property {name}") - def _delegate_property_set(self, name, value, *args, **kwargs): + def _delegate_property_set(self, name: str, value, *args, **kwargs): raise TypeError(f"The property {name} cannot be set") - def _delegate_method(self, name, *args, **kwargs): + def _delegate_method(self, name: str, *args, **kwargs): raise TypeError(f"You cannot call method {name}") @classmethod @@ -91,7 +91,7 @@ def _add_delegate_accessors( False skips the missing accessor. """ - def _create_delegator_property(name): + def _create_delegator_property(name: str): def _getter(self): return self._delegate_property_get(name) @@ -107,7 +107,7 @@ def _setter(self, new_values): doc=getattr(delegate, accessor_mapping(name)).__doc__, ) - def _create_delegator_method(name): + def _create_delegator_method(name: str): def f(self, *args, **kwargs): return self._delegate_method(name, *args, **kwargs) @@ -231,7 +231,7 @@ def __get__(self, obj, cls): @doc(klass="", others="") -def _register_accessor(name, cls): +def _register_accessor(name: str, cls): """ Register a custom accessor on {klass} objects. @@ -320,21 +320,21 @@ def decorator(accessor): @doc(_register_accessor, klass="DataFrame") -def register_dataframe_accessor(name): +def register_dataframe_accessor(name: str): from pandas import DataFrame return _register_accessor(name, DataFrame) @doc(_register_accessor, klass="Series") -def register_series_accessor(name): +def register_series_accessor(name: str): from pandas import Series return _register_accessor(name, Series) @doc(_register_accessor, klass="Index") -def register_index_accessor(name): +def register_index_accessor(name: str): from pandas import Index return _register_accessor(name, Index) diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index c0cca1852b446..acf8cbc8fd545 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -291,14 +291,14 @@ def __getitem__( return result def _fill_mask_inplace( - self, method: str, limit, mask: npt.NDArray[np.bool_] + self, method: str, limit: int | None, mask: npt.NDArray[np.bool_] ) -> None: # (for now) when self.ndim == 2, we assume axis=0 func = missing.get_fill_func(method, ndim=self.ndim) func(self._ndarray.T, limit=limit, mask=mask.T) @doc(ExtensionArray.fillna) - def fillna(self, value=None, method=None, limit=None) -> Self: + def fillna(self, value=None, method=None, limit: int | None = None) -> Self: value, method = validate_fillna_kwargs( value, method, validate_scalar_dict_value=False ) diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py index 6b722d800519c..fc303536b337b 100644 --- a/pandas/core/arrays/arrow/array.py +++ b/pandas/core/arrays/arrow/array.py @@ -1952,7 +1952,7 @@ def _str_translate(self, table): "str.translate not supported with pd.ArrowDtype(pa.string())." ) - def _str_wrap(self, width, **kwargs): + def _str_wrap(self, width: int, **kwargs): raise NotImplementedError( "str.wrap not supported with pd.ArrowDtype(pa.string())." ) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 20489654d7700..a5032c590300c 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -1570,7 +1570,7 @@ def _where(self, mask: npt.NDArray[np.bool_], value) -> Self: return result def _fill_mask_inplace( - self, method: str, limit, mask: npt.NDArray[np.bool_] + self, method: str, limit: int | None, mask: npt.NDArray[np.bool_] ) -> None: """ Replace values in locations specified by 'mask' using pad or backfill. diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 0a79004871f5f..f8befdbc6ca9c 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2500,10 +2500,14 @@ def _validate(data): if not is_categorical_dtype(data.dtype): raise AttributeError("Can only use .cat accessor with a 'category' dtype") - def _delegate_property_get(self, name): + # error: Signature of "_delegate_property_get" incompatible with supertype + # "PandasDelegate" + def _delegate_property_get(self, name: str): # type: ignore[override] return getattr(self._parent, name) - def _delegate_property_set(self, name, new_values): + # error: Signature of "_delegate_property_set" incompatible with supertype + # "PandasDelegate" + def _delegate_property_set(self, name: str, new_values): # type: ignore[override] return setattr(self._parent, name, new_values) @property @@ -2515,7 +2519,7 @@ def codes(self) -> Series: return Series(self._parent.codes, index=self._index) - def _delegate_method(self, name, *args, **kwargs): + def _delegate_method(self, name: str, *args, **kwargs): from pandas import Series method = getattr(self._parent, name) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 296e8e0784e38..deccfaf12c036 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -113,7 +113,7 @@ def tz_to_dtype(tz: tzinfo | None, unit: str = "ns"): return DatetimeTZDtype(tz=tz, unit=unit) -def _field_accessor(name: str, field: str, docstring=None): +def _field_accessor(name: str, field: str, docstring: str | None = None): def f(self): values = self._local_timestamps() diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index abc5606798cd9..3e32598cc6b11 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -889,7 +889,7 @@ def max(self, *, axis: AxisInt | None = None, skipna: bool = True) -> IntervalOr indexer = obj.argsort()[-1] return obj[indexer] - def fillna(self, value=None, method=None, limit=None) -> Self: + def fillna(self, value=None, method=None, limit: int | None = None) -> Self: """ Fill NA/NaN values using the specified method. diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index 8591cf2d3a4c5..aa3516c3ecb4f 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -162,7 +162,7 @@ def __getitem__(self, item: PositionalIndexer) -> Self | Any: @doc(ExtensionArray.fillna) @doc(ExtensionArray.fillna) - def fillna(self, value=None, method=None, limit=None) -> Self: + def fillna(self, value=None, method=None, limit: int | None = None) -> Self: value, method = validate_fillna_kwargs(value, method) mask = self._mask diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 64ce896077fc1..6557a4b674b4f 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -98,7 +98,7 @@ } -def _field_accessor(name: str, docstring=None): +def _field_accessor(name: str, docstring: str | None = None): def f(self): base = self.freq._period_dtype_code result = get_period_field_arr(name, self.asi8, base) @@ -658,7 +658,7 @@ def searchsorted( m8arr = self._ndarray.view("M8[ns]") return m8arr.searchsorted(npvalue, side=side, sorter=sorter) - def fillna(self, value=None, method=None, limit=None) -> PeriodArray: + def fillna(self, value=None, method=None, limit: int | None = None) -> PeriodArray: if method is not None: # view as dt64 so we get treated as timelike in core.missing, # similar to dtl._period_dispatch diff --git a/pandas/core/arrays/sparse/accessor.py b/pandas/core/arrays/sparse/accessor.py index ca1e73d3e6865..24ae13c12ad32 100644 --- a/pandas/core/arrays/sparse/accessor.py +++ b/pandas/core/arrays/sparse/accessor.py @@ -46,10 +46,10 @@ def _validate(self, data): if not isinstance(data.dtype, SparseDtype): raise AttributeError(self._validation_msg) - def _delegate_property_get(self, name, *args, **kwargs): + def _delegate_property_get(self, name: str, *args, **kwargs): return getattr(self._parent.array, name) - def _delegate_method(self, name, *args, **kwargs): + def _delegate_method(self, name: str, *args, **kwargs): if name == "from_coo": return self.from_coo(*args, **kwargs) elif name == "to_coo": diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bef7022a7d10f..0e5c86dcaf2e0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4936,7 +4936,9 @@ def _series(self): # ---------------------------------------------------------------------- # Reindexing and alignment - def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy): + def _reindex_axes( + self, axes, level, limit: int | None, tolerance, method, fill_value, copy + ): frame = self columns = axes["columns"] @@ -4960,7 +4962,7 @@ def _reindex_index( copy: bool, level: Level, fill_value=np.nan, - limit=None, + limit: int | None = None, tolerance=None, ): new_index, indexer = self.index.reindex( @@ -4980,7 +4982,7 @@ def _reindex_columns( copy: bool, level: Level, fill_value=None, - limit=None, + limit: int | None = None, tolerance=None, ): new_columns, indexer = self.columns.reindex( diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 80ac49d460d3d..40e25e550a8c5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -255,7 +255,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin): _accessors: set[str] = set() _hidden_attrs: frozenset[str] = frozenset([]) _metadata: list[str] = [] - _is_copy: weakref.ReferenceType[NDFrame] | None = None + _is_copy: weakref.ReferenceType[NDFrame] | str | None = None _mgr: Manager _attrs: dict[Hashable, Any] _typ: str @@ -4306,7 +4306,7 @@ def __delitem__(self, key) -> None: # Unsorted @final - def _check_inplace_and_allows_duplicate_labels(self, inplace): + def _check_inplace_and_allows_duplicate_labels(self, inplace: bool_t): if inplace and not self.flags.allows_duplicate_labels: raise ValueError( "Cannot specify 'inplace=True' when " @@ -4384,7 +4384,7 @@ def reindex_like( other, method: Literal["backfill", "bfill", "pad", "ffill", "nearest"] | None = None, copy: bool_t | None = None, - limit=None, + limit: int | None = None, tolerance=None, ) -> Self: """ @@ -9628,7 +9628,7 @@ def _align_frame( copy: bool_t | None = None, fill_value=None, method=None, - limit=None, + limit: int | None = None, fill_axis: Axis = 0, ) -> tuple[Self, DataFrame, Index | None]: # defaults @@ -9684,7 +9684,7 @@ def _align_series( copy: bool_t | None = None, fill_value=None, method=None, - limit=None, + limit: int | None = None, fill_axis: Axis = 0, ) -> tuple[Self, Series, Index | None]: is_series = isinstance(self, ABCSeries) @@ -10983,7 +10983,7 @@ def pct_change( self, periods: int = 1, fill_method: Literal["backfill", "bfill", "pad", "ffill"] | None = "pad", - limit=None, + limit: int | None = None, freq=None, **kwargs, ) -> Self: diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index a9df4237601db..e87a74e5885b3 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -2249,7 +2249,7 @@ def fillna( method: FillnaOptions | None = None, axis: Axis | None | lib.NoDefault = lib.no_default, inplace: bool = False, - limit=None, + limit: int | None = None, downcast=None, ) -> DataFrame | None: """ diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index e84a23be6c5bb..e591298e2a58e 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2781,7 +2781,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: ) @final - def _fill(self, direction: Literal["ffill", "bfill"], limit=None): + def _fill(self, direction: Literal["ffill", "bfill"], limit: int | None = None): """ Shared function for `pad` and `backfill` to call Cython method. @@ -2868,7 +2868,7 @@ def blk_func(values: ArrayLike) -> ArrayLike: @final @Substitution(name="groupby") - def ffill(self, limit=None): + def ffill(self, limit: int | None = None): """ Forward fill the values. @@ -2893,7 +2893,7 @@ def ffill(self, limit=None): @final @Substitution(name="groupby") - def bfill(self, limit=None): + def bfill(self, limit: int | None = None): """ Backward fill the values. @@ -3789,7 +3789,7 @@ def pct_change( self, periods: int = 1, fill_method: FillnaOptions = "ffill", - limit=None, + limit: int | None = None, freq=None, axis: Axis | lib.NoDefault = lib.no_default, ): diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 85460a04298e6..9f5dd5bec41ef 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -81,7 +81,9 @@ def _get_values(self): f"cannot convert an object of type {type(data)} to a datetimelike index" ) - def _delegate_property_get(self, name): + # error: Signature of "_delegate_property_get" incompatible with supertype + # "PandasDelegate" + def _delegate_property_get(self, name: str): # type: ignore[override] from pandas import Series values = self._get_values() @@ -113,13 +115,13 @@ def _delegate_property_get(self, name): return result - def _delegate_property_set(self, name, value, *args, **kwargs): + def _delegate_property_set(self, name: str, value, *args, **kwargs): raise ValueError( "modifications to a property of a datetimelike object are not supported. " "Change values on the original." ) - def _delegate_method(self, name, *args, **kwargs): + def _delegate_method(self, name: str, *args, **kwargs): from pandas import Series values = self._get_values() diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c93eb0fe3def6..cd7469ca3234b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4188,7 +4188,12 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None: raise ValueError("cannot reindex on an axis with duplicate labels") def reindex( - self, target, method=None, level=None, limit=None, tolerance=None + self, + target, + method=None, + level=None, + limit: int | None = None, + tolerance: float | None = None, ) -> tuple[Index, npt.NDArray[np.intp] | None]: """ Create index with target's values. diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 75ce22bd91f41..b740f58097509 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -342,7 +342,7 @@ def __contains__(self, key: Any) -> bool: return contains(self, key, container=self._engine) def reindex( - self, target, method=None, level=None, limit=None, tolerance=None + self, target, method=None, level=None, limit: int | None = None, tolerance=None ) -> tuple[Index, npt.NDArray[np.intp] | None]: """ Create index with target's values (move/add/delete values as necessary) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 3b4a6b2e5dfde..eae70d50e7f95 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -485,7 +485,7 @@ def shift(self, periods: int = 1, freq=None): def period_range( - start=None, end=None, periods: int | None = None, freq=None, name=None + start=None, end=None, periods: int | None = None, freq=None, name: Hashable = None ) -> PeriodIndex: """ Return a fixed frequency PeriodIndex. diff --git a/pandas/core/interchange/dataframe.py b/pandas/core/interchange/dataframe.py index 0de9b130f0aab..51b6cebabc2d5 100644 --- a/pandas/core/interchange/dataframe.py +++ b/pandas/core/interchange/dataframe.py @@ -92,7 +92,7 @@ def select_columns_by_name(self, names) -> PandasDataFrameXchg: self._df.loc[:, names], self._nan_as_null, self._allow_copy ) - def get_chunks(self, n_chunks=None): + def get_chunks(self, n_chunks: int | None = None): """ Return an iterator yielding the chunks. """ diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index 0408dfd83fedc..407e16e1fa187 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -363,7 +363,7 @@ def shift(self, periods: int, axis: AxisInt, fill_value) -> Self: "shift", periods=periods, axis=axis, fill_value=fill_value ) - def fillna(self, value, limit, inplace: bool, downcast) -> Self: + def fillna(self, value, limit: int | None, inplace: bool, downcast) -> Self: if limit is not None: # Do this validation even if we go through one of the no-op paths limit = libalgos.validate_limit(None, limit=limit) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 70d7920ac5bb2..cb644c8329179 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -425,7 +425,7 @@ def shift(self, periods: int, axis: AxisInt, fill_value) -> Self: return self.apply("shift", periods=periods, axis=axis, fill_value=fill_value) - def fillna(self, value, limit, inplace: bool, downcast) -> Self: + def fillna(self, value, limit: int | None, inplace: bool, downcast) -> Self: if limit is not None: # Do this validation even if we go through one of the no-op paths limit = libalgos.validate_limit(None, limit=limit) diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 2fb323002292a..521ced16e9e97 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -873,7 +873,7 @@ def _datetimelike_compat(func: F) -> F: """ @wraps(func) - def new_func(values, limit=None, mask=None): + def new_func(values, limit: int | None = None, mask=None): if needs_i8_conversion(values.dtype): if mask is None: # This needs to occur before casting to int64 @@ -910,7 +910,11 @@ def _backfill_1d( @_datetimelike_compat -def _pad_2d(values: np.ndarray, limit=None, mask: npt.NDArray[np.bool_] | None = None): +def _pad_2d( + values: np.ndarray, + limit: int | None = None, + mask: npt.NDArray[np.bool_] | None = None, +): mask = _fillna_prep(values, mask) if np.all(values.shape): @@ -922,7 +926,9 @@ def _pad_2d(values: np.ndarray, limit=None, mask: npt.NDArray[np.bool_] | None = @_datetimelike_compat -def _backfill_2d(values, limit=None, mask: npt.NDArray[np.bool_] | None = None): +def _backfill_2d( + values, limit: int | None = None, mask: npt.NDArray[np.bool_] | None = None +): mask = _fillna_prep(values, mask) if np.all(values.shape): @@ -982,7 +988,7 @@ def _interp_limit(invalid, fw_limit, bw_limit): f_idx = set() b_idx = set() - def inner(invalid, limit): + def inner(invalid, limit: int): limit = min(limit, N) windowed = _rolling_window(invalid, limit + 1).all(1) idx = set(np.where(windowed)[0] + limit) | set( diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 3b31932952867..e8864deaaca4d 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -376,7 +376,7 @@ def transform(self, arg, *args, **kwargs): def _downsample(self, f, **kwargs): raise AbstractMethodError(self) - def _upsample(self, f, limit=None, fill_value=None): + def _upsample(self, f, limit: int | None = None, fill_value=None): raise AbstractMethodError(self) def _gotitem(self, key, ndim: int, subset=None): @@ -483,7 +483,7 @@ def _wrap_result(self, result): return result - def ffill(self, limit=None): + def ffill(self, limit: int | None = None): """ Forward fill the values. @@ -503,7 +503,7 @@ def ffill(self, limit=None): """ return self._upsample("ffill", limit=limit) - def nearest(self, limit=None): + def nearest(self, limit: int | None = None): """ Resample by using the nearest value. @@ -563,7 +563,7 @@ def nearest(self, limit=None): """ return self._upsample("nearest", limit=limit) - def bfill(self, limit=None): + def bfill(self, limit: int | None = None): """ Backward fill the new missing values in the resampled data. @@ -665,7 +665,7 @@ def bfill(self, limit=None): """ return self._upsample("bfill", limit=limit) - def fillna(self, method, limit=None): + def fillna(self, method, limit: int | None = None): """ Fill missing values introduced by upsampling. @@ -831,7 +831,7 @@ def interpolate( method: QuantileInterpolation = "linear", *, axis: Axis = 0, - limit=None, + limit: int | None = None, inplace: bool = False, limit_direction: Literal["forward", "backward", "both"] = "forward", limit_area=None, @@ -1311,7 +1311,7 @@ def _adjust_binner_for_upsample(self, binner): binner = binner[:-1] return binner - def _upsample(self, method, limit=None, fill_value=None): + def _upsample(self, method, limit: int | None = None, fill_value=None): """ Parameters ---------- @@ -1440,7 +1440,7 @@ def _downsample(self, how, **kwargs): "as they are not sub or super periods" ) - def _upsample(self, method, limit=None, fill_value=None): + def _upsample(self, method, limit: int | None = None, fill_value=None): """ Parameters ---------- @@ -1532,7 +1532,7 @@ def get_resampler_for_grouping( rule, how=None, fill_method=None, - limit=None, + limit: int | None = None, kind=None, on=None, **kwargs, @@ -1579,7 +1579,7 @@ def __init__( how: str = "mean", axis: Axis = 0, fill_method=None, - limit=None, + limit: int | None = None, kind: str | None = None, convention: Literal["start", "end", "e", "s"] | None = None, origin: Literal["epoch", "start", "start_day", "end", "end_day"] diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 395db8060ce0e..d3806c6850b7a 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -71,7 +71,7 @@ def concat( ignore_index: bool = ..., keys=..., levels=..., - names=..., + names: list[HashableT] | None = ..., verify_integrity: bool = ..., sort: bool = ..., copy: bool | None = ..., @@ -88,7 +88,7 @@ def concat( ignore_index: bool = ..., keys=..., levels=..., - names=..., + names: list[HashableT] | None = ..., verify_integrity: bool = ..., sort: bool = ..., copy: bool | None = ..., @@ -105,7 +105,7 @@ def concat( ignore_index: bool = ..., keys=..., levels=..., - names=..., + names: list[HashableT] | None = ..., verify_integrity: bool = ..., sort: bool = ..., copy: bool | None = ..., @@ -122,7 +122,7 @@ def concat( ignore_index: bool = ..., keys=..., levels=..., - names=..., + names: list[HashableT] | None = ..., verify_integrity: bool = ..., sort: bool = ..., copy: bool | None = ..., @@ -139,7 +139,7 @@ def concat( ignore_index: bool = ..., keys=..., levels=..., - names=..., + names: list[HashableT] | None = ..., verify_integrity: bool = ..., sort: bool = ..., copy: bool | None = ..., @@ -155,7 +155,7 @@ def concat( ignore_index: bool = False, keys=None, levels=None, - names=None, + names: list[HashableT] | None = None, verify_integrity: bool = False, sort: bool = False, copy: bool | None = None, @@ -400,7 +400,7 @@ def __init__( join: str = "outer", keys=None, levels=None, - names=None, + names: list[HashableT] | None = None, ignore_index: bool = False, verify_integrity: bool = False, copy: bool = True, diff --git a/pandas/core/reshape/encoding.py b/pandas/core/reshape/encoding.py index 92d556a582262..320f441972bd8 100644 --- a/pandas/core/reshape/encoding.py +++ b/pandas/core/reshape/encoding.py @@ -161,7 +161,7 @@ def get_dummies( data_to_encode = data[columns] # validate prefixes and separator to avoid silently dropping cols - def check_len(item, name): + def check_len(item, name: str): if is_list_like(item): if not len(item) == data_to_encode.shape[1]: len_msg = ( diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index d2b022214167f..c3dacc2172aa7 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -364,7 +364,7 @@ def merge_asof( left_by=None, right_by=None, suffixes: Suffixes = ("_x", "_y"), - tolerance=None, + tolerance: int | Timedelta | None = None, allow_exact_matches: bool = True, direction: str = "backward", ) -> DataFrame: @@ -2554,7 +2554,7 @@ def _items_overlap_with_suffix( if not lsuffix and not rsuffix: raise ValueError(f"columns overlap but no suffix specified: {to_rename}") - def renamer(x, suffix): + def renamer(x, suffix: str | None): """ Rename the left and right indices. diff --git a/pandas/core/series.py b/pandas/core/series.py index 06c744c3e36fa..d6bd35639253a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1600,7 +1600,7 @@ def to_string( float_format: str | None = ..., header: bool = ..., index: bool = ..., - length=..., + length: bool = ..., dtype=..., name=..., max_rows: int | None = ..., @@ -1616,7 +1616,7 @@ def to_string( float_format: str | None = ..., header: bool = ..., index: bool = ..., - length=..., + length: bool = ..., dtype=..., name=..., max_rows: int | None = ..., diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index d6adf01f4e12b..a9ce262c356db 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -153,7 +153,7 @@ def _int64_cut_off(shape) -> int: return i return len(shape) - def maybe_lift(lab, size) -> tuple[np.ndarray, int]: + def maybe_lift(lab, size: int) -> tuple[np.ndarray, int]: # promote nan values (assigned -1 label in lab array) # so that all output values are non-negative return (lab + 1, size + 1) if (lab == -1).any() else (lab, size) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 1c4727fda4e64..85c5b089b3582 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -133,7 +133,7 @@ def wrapper(self, *args, **kwargs): return _forbid_nonstring_types -def _map_and_wrap(name, docstring): +def _map_and_wrap(name: str | None, docstring: str | None): @forbid_nonstring_types(["bytes"], name=name) def wrapper(self): result = getattr(self._data.array, f"_str_{name}")() @@ -413,7 +413,7 @@ def _get_series_list(self, others): def cat( self, others=None, - sep=None, + sep: str | None = None, na_rep=None, join: AlignJoin = "left", ) -> str | Series | Index: @@ -1043,7 +1043,7 @@ def get(self, i): return self._wrap_result(result) @forbid_nonstring_types(["bytes"]) - def join(self, sep): + def join(self, sep: str): """ Join lists contained as elements in the Series/Index with passed delimiter. @@ -1511,7 +1511,7 @@ def repeat(self, repeats): @forbid_nonstring_types(["bytes"]) def pad( self, - width, + width: int, side: Literal["left", "right", "both"] = "left", fillchar: str = " ", ): @@ -1603,21 +1603,21 @@ def pad( @Appender(_shared_docs["str_pad"] % {"side": "left and right", "method": "center"}) @forbid_nonstring_types(["bytes"]) - def center(self, width, fillchar: str = " "): + def center(self, width: int, fillchar: str = " "): return self.pad(width, side="both", fillchar=fillchar) @Appender(_shared_docs["str_pad"] % {"side": "right", "method": "ljust"}) @forbid_nonstring_types(["bytes"]) - def ljust(self, width, fillchar: str = " "): + def ljust(self, width: int, fillchar: str = " "): return self.pad(width, side="right", fillchar=fillchar) @Appender(_shared_docs["str_pad"] % {"side": "left", "method": "rjust"}) @forbid_nonstring_types(["bytes"]) - def rjust(self, width, fillchar: str = " "): + def rjust(self, width: int, fillchar: str = " "): return self.pad(width, side="left", fillchar=fillchar) @forbid_nonstring_types(["bytes"]) - def zfill(self, width): + def zfill(self, width: int): """ Pad strings in the Series/Index by prepending '0' characters. @@ -2041,7 +2041,7 @@ def rstrip(self, to_strip=None): _shared_docs["str_removefix"] % {"side": "prefix", "other_side": "suffix"} ) @forbid_nonstring_types(["bytes"]) - def removeprefix(self, prefix): + def removeprefix(self, prefix: str): result = self._data.array._str_removeprefix(prefix) return self._wrap_result(result) @@ -2049,12 +2049,12 @@ def removeprefix(self, prefix): _shared_docs["str_removefix"] % {"side": "suffix", "other_side": "prefix"} ) @forbid_nonstring_types(["bytes"]) - def removesuffix(self, suffix): + def removesuffix(self, suffix: str): result = self._data.array._str_removesuffix(suffix) return self._wrap_result(result) @forbid_nonstring_types(["bytes"]) - def wrap(self, width, **kwargs): + def wrap(self, width: int, **kwargs): r""" Wrap strings in Series/Index at specified line width. diff --git a/pandas/core/strings/base.py b/pandas/core/strings/base.py index 10d8e94972725..2672d22935d72 100644 --- a/pandas/core/strings/base.py +++ b/pandas/core/strings/base.py @@ -46,7 +46,7 @@ def _str_count(self, pat, flags: int = 0): @abc.abstractmethod def _str_pad( self, - width, + width: int, side: Literal["left", "right", "both"] = "left", fillchar: str = " ", ): @@ -127,15 +127,15 @@ def _str_rindex(self, sub, start: int = 0, end=None): pass @abc.abstractmethod - def _str_join(self, sep): + def _str_join(self, sep: str): pass @abc.abstractmethod - def _str_partition(self, sep, expand): + def _str_partition(self, sep: str, expand): pass @abc.abstractmethod - def _str_rpartition(self, sep, expand): + def _str_rpartition(self, sep: str, expand): pass @abc.abstractmethod @@ -155,7 +155,7 @@ def _str_translate(self, table): pass @abc.abstractmethod - def _str_wrap(self, width, **kwargs): + def _str_wrap(self, width: int, **kwargs): pass @abc.abstractmethod diff --git a/pandas/core/strings/object_array.py b/pandas/core/strings/object_array.py index f8e3f0756dfbd..777233d3c55f1 100644 --- a/pandas/core/strings/object_array.py +++ b/pandas/core/strings/object_array.py @@ -111,7 +111,7 @@ def _str_count(self, pat, flags: int = 0): def _str_pad( self, - width, + width: int, side: Literal["left", "right", "both"] = "left", fillchar: str = " ", ): @@ -283,14 +283,14 @@ def _str_rindex(self, sub, start: int = 0, end=None): f = lambda x: x.rindex(sub, start, end) return self._str_map(f, dtype="int64") - def _str_join(self, sep): + def _str_join(self, sep: str): return self._str_map(sep.join) - def _str_partition(self, sep, expand): + def _str_partition(self, sep: str, expand): result = self._str_map(lambda x: x.partition(sep), dtype="object") return result - def _str_rpartition(self, sep, expand): + def _str_rpartition(self, sep: str, expand): return self._str_map(lambda x: x.rpartition(sep), dtype="object") def _str_len(self): @@ -362,7 +362,7 @@ def _str_rsplit(self, pat=None, n=-1): def _str_translate(self, table): return self._str_map(lambda x: x.translate(table)) - def _str_wrap(self, width, **kwargs): + def _str_wrap(self, width: int, **kwargs): kwargs["width"] = width tw = textwrap.TextWrapper(**kwargs) return self._str_map(lambda s: "\n".join(tw.wrap(s))) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 0265b4404d6ab..70c4af2ed7949 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -312,7 +312,7 @@ def _convert_and_box_cache( def _return_parsed_timezone_results( - result: np.ndarray, timezones, utc: bool, name + result: np.ndarray, timezones, utc: bool, name: str ) -> Index: """ Return results from array_strptime if a %z or %Z directive was passed. diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 98f7b64d2cda0..da35716a5b239 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -450,7 +450,7 @@ def _get_font_size(self, props: Mapping[str, str]) -> float | None: return size return self._pt_to_float(size) - def _select_font_family(self, font_names) -> int | None: + def _select_font_family(self, font_names: Sequence[str]) -> int | None: family = None for name in font_names: family = self.FAMILY_MAP.get(name) diff --git a/pandas/io/html.py b/pandas/io/html.py index ce95c2be8581f..45bbddd72e51f 100644 --- a/pandas/io/html.py +++ b/pandas/io/html.py @@ -531,7 +531,7 @@ def _expand_colspan_rowspan( return all_texts - def _handle_hidden_tables(self, tbl_list, attr_name): + def _handle_hidden_tables(self, tbl_list, attr_name: str): """ Return list of tables, potentially removing hidden elements diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 588ec639bc2fd..5b8cb1fe4246b 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -10,6 +10,7 @@ from typing import ( TYPE_CHECKING, Any, + Hashable, Callable, Generic, Literal, @@ -1167,7 +1168,7 @@ def _try_convert_types(self): def _try_convert_data( self, - name, + name: Hashable, data, use_dtypes: bool = True, convert_dates: bool | list[str] = True, diff --git a/pandas/io/parsers/base_parser.py b/pandas/io/parsers/base_parser.py index 9ac31a3e46cd8..35d9d91fb025b 100644 --- a/pandas/io/parsers/base_parser.py +++ b/pandas/io/parsers/base_parser.py @@ -927,7 +927,7 @@ def _evaluate_usecols( return {i for i, name in enumerate(names) if usecols(name)} return usecols - def _validate_usecols_names(self, usecols, names): + def _validate_usecols_names(self, usecols, names: Sequence): """ Validates that all usecols are present in a given list of names. If not, raise a ValueError that diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index df675a0a3a6cc..2a6c43bff5047 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -489,21 +489,23 @@ class _DeprecationConfig(NamedTuple): @overload -def validate_integer(name, val: None, min_val: int = ...) -> None: +def validate_integer(name: str, val: None, min_val: int = ...) -> None: ... @overload -def validate_integer(name, val: float, min_val: int = ...) -> int: +def validate_integer(name: str, val: float, min_val: int = ...) -> int: ... @overload -def validate_integer(name, val: int | None, min_val: int = ...) -> int | None: +def validate_integer(name: str, val: int | None, min_val: int = ...) -> int | None: ... -def validate_integer(name, val: int | float | None, min_val: int = 0) -> int | None: +def validate_integer( + name: str, val: int | float | None, min_val: int = 0 +) -> int | None: """ Checks whether the 'name' parameter for parsing is either an integer OR float that can SAFELY be cast to an integer diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index f083ca792c456..bdf469b1f1d38 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -800,7 +800,7 @@ def select( stop=None, columns=None, iterator: bool = False, - chunksize=None, + chunksize: int | None = None, auto_close: bool = False, ): """ @@ -948,7 +948,7 @@ def select_as_multiple( start=None, stop=None, iterator: bool = False, - chunksize=None, + chunksize: int | None = None, auto_close: bool = False, ): """ @@ -1202,7 +1202,7 @@ def append( columns=None, min_itemsize: int | dict[str, int] | None = None, nan_rep=None, - chunksize=None, + chunksize: int | None = None, expectedrows=None, dropna: bool | None = None, data_columns: Literal[True] | list[str] | None = None, @@ -1734,7 +1734,7 @@ def _write_to_group( complevel: int | None = None, fletcher32=None, min_itemsize: int | dict[str, int] | None = None, - chunksize=None, + chunksize: int | None = None, expectedrows=None, dropna: bool = False, nan_rep=None, @@ -4271,7 +4271,7 @@ def write( # type: ignore[override] complevel=None, fletcher32=None, min_itemsize=None, - chunksize=None, + chunksize: int | None = None, expectedrows=None, dropna: bool = False, nan_rep=None, diff --git a/pandas/io/sas/sas_xport.py b/pandas/io/sas/sas_xport.py index f23b18fdcb584..e68f4789f0a06 100644 --- a/pandas/io/sas/sas_xport.py +++ b/pandas/io/sas/sas_xport.py @@ -259,7 +259,7 @@ def __init__( filepath_or_buffer: FilePath | ReadBuffer[bytes], index=None, encoding: str | None = "ISO-8859-1", - chunksize=None, + chunksize: int | None = None, compression: CompressionOptions = "infer", ) -> None: self._encoding = encoding @@ -439,7 +439,7 @@ def _record_count(self) -> int: return (total_records_length - tail_pad) // self.record_length - def get_chunk(self, size=None) -> pd.DataFrame: + def get_chunk(self, size: int | None = None) -> pd.DataFrame: """ Reads lines from Xport file and returns as dataframe diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 044fd9806d921..894ab110ef012 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -223,7 +223,7 @@ def execute(sql, con, params=None): @overload def read_sql_table( - table_name, + table_name: str, con, schema=..., index_col: str | list[str] | None = ..., @@ -238,7 +238,7 @@ def read_sql_table( @overload def read_sql_table( - table_name, + table_name: str, con, schema=..., index_col: str | list[str] | None = ..., @@ -1034,7 +1034,7 @@ def _query_iterator( self, result, exit_stack: ExitStack, - chunksize: str | None, + chunksize: int | None, columns, coerce_float: bool = True, parse_dates=None, @@ -1072,7 +1072,7 @@ def read( coerce_float: bool = True, parse_dates=None, columns=None, - chunksize=None, + chunksize: int | None = None, dtype_backend: DtypeBackend | Literal["numpy"] = "numpy", ) -> DataFrame | Iterator[DataFrame]: from sqlalchemy import select @@ -1386,12 +1386,12 @@ def read_query( def to_sql( self, frame, - name, + name: str, if_exists: Literal["fail", "replace", "append"] = "fail", index: bool = True, index_label=None, schema=None, - chunksize=None, + chunksize: int | None = None, dtype: DtypeArg | None = None, method=None, engine: str = "auto", @@ -1425,10 +1425,10 @@ def insert_records( table: SQLTable, con, frame, - name, + name: str, index: bool | str | list[str] | None = True, schema=None, - chunksize=None, + chunksize: int | None = None, method=None, **engine_kwargs, ) -> int | None: @@ -1449,10 +1449,10 @@ def insert_records( table: SQLTable, con, frame, - name, + name: str, index: bool | str | list[str] | None = True, schema=None, - chunksize=None, + chunksize: int | None = None, method=None, **engine_kwargs, ) -> int | None: @@ -1770,7 +1770,7 @@ def read_query( def prep_table( self, frame, - name, + name: str, if_exists: Literal["fail", "replace", "append"] = "fail", index: bool | str | list[str] | None = True, index_label=None, @@ -1852,7 +1852,7 @@ def to_sql( index: bool = True, index_label=None, schema: str | None = None, - chunksize=None, + chunksize: int | None = None, dtype: DtypeArg | None = None, method=None, engine: str = "auto", @@ -1998,7 +1998,7 @@ def _create_sql_schema( } -def _get_unicode_name(name): +def _get_unicode_name(name: object): try: uname = str(name).encode("utf-8", "strict").decode("utf-8") except UnicodeError as err: @@ -2006,7 +2006,7 @@ def _get_unicode_name(name): return uname -def _get_valid_sqlite_name(name): +def _get_valid_sqlite_name(name: object): # See https://stackoverflow.com/questions/6514274/how-do-you-escape-strings\ # -for-sqlite-table-column-names-in-python # Ensure the string can be encoded as UTF-8. @@ -2302,12 +2302,12 @@ def _fetchall_as_list(self, cur): def to_sql( self, frame, - name, + name: str, if_exists: str = "fail", index: bool = True, index_label=None, schema=None, - chunksize=None, + chunksize: int | None = None, dtype: DtypeArg | None = None, method=None, engine: str = "auto", diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 119a71ddc6943..75af0c7bdae79 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -500,10 +500,10 @@ def boxplot_frame( column=None, by=None, ax=None, - fontsize=None, + fontsize: int | None = None, rot: int = 0, grid: bool = True, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, return_type=None, backend=None, @@ -529,11 +529,11 @@ def boxplot_frame_groupby( grouped, subplots: bool = True, column=None, - fontsize=None, + fontsize: int | None = None, rot: int = 0, grid: bool = True, ax=None, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, sharex: bool = False, sharey: bool = True, @@ -797,7 +797,7 @@ def __init__(self, data) -> None: self._parent = data @staticmethod - def _get_call_args(backend_name, data, args, kwargs): + def _get_call_args(backend_name: str, data, args, kwargs): """ This function makes calls to this accessor `__call__` method compatible with the previous `SeriesPlotMethods.__call__` and diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index b39fc93f4f024..d15da170682d3 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -210,7 +210,7 @@ def _make_plot(self) -> None: labels = [pprint_thing(key) for key in range(len(labels))] self._set_ticklabels(ax, labels) - def _set_ticklabels(self, ax: Axes, labels) -> None: + def _set_ticklabels(self, ax: Axes, labels: list[str]) -> None: if self.orientation == "vertical": ax.set_xticklabels(labels) else: @@ -248,7 +248,7 @@ def _grouped_plot_by_column( by=None, numeric_only: bool = True, grid: bool = False, - figsize=None, + figsize: tuple[float, float] | None = None, ax=None, layout=None, return_type=None, @@ -307,10 +307,10 @@ def boxplot( column=None, by=None, ax=None, - fontsize=None, + fontsize: int | None = None, rot: int = 0, grid: bool = True, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, return_type=None, **kwds, @@ -456,10 +456,10 @@ def boxplot_frame( column=None, by=None, ax=None, - fontsize=None, + fontsize: int | None = None, rot: int = 0, grid: bool = True, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, return_type=None, **kwds, @@ -487,11 +487,11 @@ def boxplot_frame_groupby( grouped, subplots: bool = True, column=None, - fontsize=None, + fontsize: int | None = None, rot: int = 0, grid: bool = True, ax=None, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, sharex: bool = False, sharey: bool = True, diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 83e3ea8905e1a..cfea83a7740fe 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -127,7 +127,7 @@ def __init__( sharex=None, sharey: bool = False, use_index: bool = True, - figsize=None, + figsize: tuple[float, float] | None = None, grid=None, legend: bool | str = True, rot=None, @@ -140,7 +140,7 @@ def __init__( yticks=None, xlabel: Hashable | None = None, ylabel: Hashable | None = None, - fontsize=None, + fontsize: int | None = None, secondary_y: bool | tuple | list | np.ndarray = False, colormap=None, table: bool = False, @@ -729,7 +729,9 @@ def _adorn_subplots(self): raise ValueError(msg) self.axes[0].set_title(self.title) - def _apply_axis_properties(self, axis: Axis, rot=None, fontsize=None) -> None: + def _apply_axis_properties( + self, axis: Axis, rot=None, fontsize: int | None = None + ) -> None: """ Tick creation within matplotlib is reasonably expensive and is internally deferred until accessed as Ticks are created/destroyed @@ -958,7 +960,7 @@ def on_right(self, i): if isinstance(self.secondary_y, (tuple, list, np.ndarray, ABCIndex)): return self.data.columns[i] in self.secondary_y - def _apply_style_colors(self, colors, kwds, col_num, label): + def _apply_style_colors(self, colors, kwds, col_num, label: str): """ Manage style and color based on column number and its label. Returns tuple of appropriate style and kwds which "color" may be added. diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py index 710c20db0526e..076b95a885d5e 100644 --- a/pandas/plotting/_matplotlib/hist.py +++ b/pandas/plotting/_matplotlib/hist.py @@ -269,7 +269,7 @@ def _grouped_plot( column=None, by=None, numeric_only: bool = True, - figsize=None, + figsize: tuple[float, float] | None = None, sharex: bool = True, sharey: bool = True, layout=None, @@ -277,7 +277,9 @@ def _grouped_plot( ax=None, **kwargs, ): - if figsize == "default": + # error: Non-overlapping equality check (left operand type: "Optional[Tuple[float, + # float]]", right operand type: "Literal['default']") + if figsize == "default": # type: ignore[comparison-overlap] # allowed to specify mpl default with 'default' raise ValueError( "figsize='default' is no longer supported. " @@ -311,15 +313,15 @@ def _grouped_hist( by=None, ax=None, bins: int = 50, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, sharex: bool = False, sharey: bool = False, rot: float = 90, grid: bool = True, - xlabelsize=None, + xlabelsize: int | None = None, xrot=None, - ylabelsize=None, + ylabelsize: int | None = None, yrot=None, legend: bool = False, **kwargs, @@ -392,11 +394,11 @@ def hist_series( by=None, ax=None, grid: bool = True, - xlabelsize=None, + xlabelsize: int | None = None, xrot=None, - ylabelsize=None, + ylabelsize: int | None = None, yrot=None, - figsize=None, + figsize: tuple[float, float] | None = None, bins: int = 10, legend: bool = False, **kwds, @@ -464,14 +466,14 @@ def hist_frame( column=None, by=None, grid: bool = True, - xlabelsize=None, + xlabelsize: int | None = None, xrot=None, - ylabelsize=None, + ylabelsize: int | None = None, yrot=None, ax=None, sharex: bool = False, sharey: bool = False, - figsize=None, + figsize: tuple[float, float] | None = None, layout=None, bins: int = 10, legend: bool = False, diff --git a/pandas/plotting/_matplotlib/misc.py b/pandas/plotting/_matplotlib/misc.py index 291a6dff9650d..7db9acdc68d51 100644 --- a/pandas/plotting/_matplotlib/misc.py +++ b/pandas/plotting/_matplotlib/misc.py @@ -35,7 +35,7 @@ def scatter_matrix( frame: DataFrame, alpha: float = 0.5, - figsize=None, + figsize: tuple[float, float] | None = None, ax=None, grid: bool = False, diagonal: str = "hist", diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py index 7d3c857eea2dd..414a20cde62b6 100644 --- a/pandas/plotting/_matplotlib/tools.py +++ b/pandas/plotting/_matplotlib/tools.py @@ -443,9 +443,9 @@ def flatten_axes(axes: Axes | Sequence[Axes]) -> np.ndarray: def set_ticks_props( axes: Axes | Sequence[Axes], - xlabelsize=None, + xlabelsize: int | None = None, xrot=None, - ylabelsize=None, + ylabelsize: int | None = None, yrot=None, ): import matplotlib.pyplot as plt diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index 9f1e166cd6afb..34812aa491cd3 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -151,7 +151,7 @@ class Holiday: def __init__( self, - name, + name: str, year=None, month=None, day=None, @@ -366,7 +366,7 @@ def register(cls) -> None: holiday_calendars[name] = cls -def get_calendar(name): +def get_calendar(name: str): """ Return an instance of a calendar based on its name. @@ -379,7 +379,7 @@ def get_calendar(name): class HolidayCalendarMetaClass(type): - def __new__(cls, clsname, bases, attrs): + def __new__(cls, clsname: str, bases, attrs): calendar_class = super().__new__(cls, clsname, bases, attrs) register(calendar_class) return calendar_class @@ -395,7 +395,7 @@ class AbstractHolidayCalendar(metaclass=HolidayCalendarMetaClass): end_date = Timestamp(datetime(2200, 12, 31)) _cache = None - def __init__(self, name=None, rules=None) -> None: + def __init__(self, name: str = "", rules=None) -> None: """ Initializes holiday object with a given set a rules. Normally classes just have the rules defined within them. @@ -408,14 +408,14 @@ def __init__(self, name=None, rules=None) -> None: A set of rules used to create the holidays. """ super().__init__() - if name is None: + if not name: name = type(self).__name__ self.name = name if rules is not None: self.rules = rules - def rule_from_name(self, name): + def rule_from_name(self, name: str): for rule in self.rules: if rule.name == name: return rule @@ -579,7 +579,7 @@ class USFederalHolidayCalendar(AbstractHolidayCalendar): ] -def HolidayCalendarFactory(name, base, other, base_class=AbstractHolidayCalendar): +def HolidayCalendarFactory(name: str, base, other, base_class=AbstractHolidayCalendar): rules = AbstractHolidayCalendar.merge_class(base, other) calendar_class = type(name, (base_class,), {"rules": rules, "name": name}) return calendar_class diff --git a/pandas/util/_validators.py b/pandas/util/_validators.py index f03d1ceb507fd..2af68dc3d6df0 100644 --- a/pandas/util/_validators.py +++ b/pandas/util/_validators.py @@ -222,7 +222,10 @@ def validate_args_and_kwargs( def validate_bool_kwarg( - value: BoolishNoneT, arg_name, none_allowed: bool = True, int_allowed: bool = False + value: BoolishNoneT, + arg_name: str, + none_allowed: bool = True, + int_allowed: bool = False, ) -> BoolishNoneT: """ Ensure that argument passed in arg_name can be interpreted as boolean. diff --git a/scripts/run_autotyping.py b/scripts/run_autotyping.py index 0a1156399734d..4c0a3a9cf985f 100644 --- a/scripts/run_autotyping.py +++ b/scripts/run_autotyping.py @@ -26,8 +26,15 @@ def main(argv: Sequence[str] | None = None) -> None: "codemod", "autotyping.AutotypeCommand", *args.paths, - "--aggressive", "--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, ) From 038584eef232e37b091151afcbbb957481a3f6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Mar 2023 23:21:47 -0400 Subject: [PATCH 2/2] isort --- pandas/io/json/_json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 5b8cb1fe4246b..944642bbfe8d3 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -10,9 +10,9 @@ from typing import ( TYPE_CHECKING, Any, - Hashable, Callable, Generic, + Hashable, Literal, Mapping, TypeVar,