From 220deb9a4ed9292b2a4c6f94c2ebba3dd2f2f41b Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 10 Nov 2022 21:37:22 +0100 Subject: [PATCH 1/3] CI: Upgrade mypy to 0.990 --- doc/source/whatsnew/v2.0.0.rst | 2 +- environment.yml | 2 +- pandas/core/apply.py | 2 +- pandas/core/arrays/base.py | 6 ++++-- pandas/core/base.py | 4 +--- pandas/core/frame.py | 12 +++++------- pandas/core/interchange/column.py | 3 ++- pandas/core/series.py | 18 ++++++------------ pandas/io/common.py | 6 ++---- pandas/plotting/_core.py | 3 +-- pandas/util/_decorators.py | 8 +------- requirements-dev.txt | 2 +- 12 files changed, 26 insertions(+), 42 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 151d853166563..82a8922b1b070 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -250,7 +250,7 @@ If installed, we now require: +-----------------+-----------------+----------+---------+ | Package | Minimum Version | Required | Changed | +=================+=================+==========+=========+ -| mypy (dev) | 0.981 | | X | +| mypy (dev) | 0.990 | | X | +-----------------+-----------------+----------+---------+ | python-dateutil | 2.8.2 | X | X | +-----------------+-----------------+----------+---------+ diff --git a/environment.yml b/environment.yml index f6ef6367800bd..3c1df67782132 100644 --- a/environment.yml +++ b/environment.yml @@ -88,7 +88,7 @@ dependencies: - flake8=5.0.4 - flake8-bugbear=22.7.1 # used by flake8, find likely bugs - isort>=5.2.1 # check that imports are in the right order - - mypy=0.981 + - mypy=0.990 - pre-commit>=2.15.0 - pycodestyle # used by flake8 - pyupgrade diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 725043616eaa7..77ef3611b7587 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -414,7 +414,7 @@ def agg_dict_like(self) -> DataFrame | Series: axis: AxisInt = 0 if isinstance(obj, ABCSeries) else 1 result = concat( - {k: results[k] for k in keys_to_use}, # type: ignore[misc] + {k: results[k] for k in keys_to_use}, axis=axis, keys=keys_to_use, ) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index a190817d0ccf6..a3c201b402b0f 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -1646,10 +1646,12 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs): class ExtensionArraySupportsAnyAll(ExtensionArray): - def any(self, *, skipna: bool = True) -> bool: + def any(self, *, skipna: bool = True) -> bool: # type: ignore[empty-body] + # error: Missing return statement pass - def all(self, *, skipna: bool = True) -> bool: + def all(self, *, skipna: bool = True) -> bool: # type: ignore[empty-body] + # error: Missing return statement pass diff --git a/pandas/core/base.py b/pandas/core/base.py index 46803e1f28975..466a57b7b9675 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1112,9 +1112,7 @@ def _memory_usage(self, deep: bool = False) -> int: are not components of the array if deep=False or if used on PyPy """ if hasattr(self.array, "memory_usage"): - # https://github.com/python/mypy/issues/1424 - # error: "ExtensionArray" has no attribute "memory_usage" - return self.array.memory_usage(deep=deep) # type: ignore[attr-defined] + return self.array.memory_usage(deep=deep) v = self.array.nbytes if deep and is_object_dtype(self) and not PYPY: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1627a7add25ed..27641c1b89399 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5446,9 +5446,8 @@ def fillna( ) -> DataFrame | None: ... - # error: Signature of "fillna" incompatible with supertype "NDFrame" @doc(NDFrame.fillna, **_shared_doc_kwargs) - def fillna( # type: ignore[override] + def fillna( self, value: Hashable | Mapping | Series | DataFrame = None, *, @@ -5537,9 +5536,8 @@ def replace( ) -> None: ... - # error: Signature of "replace" incompatible with supertype "NDFrame" @doc(NDFrame.replace, **_shared_doc_kwargs) - def replace( # type: ignore[override] + def replace( self, to_replace=None, value=lib.no_default, @@ -6684,10 +6682,9 @@ def sort_values( ... # TODO: Just move the sort_values doc here. - # error: Signature of "sort_values" incompatible with supertype "NDFrame" @Substitution(**_shared_doc_kwargs) @Appender(NDFrame.sort_values.__doc__) - def sort_values( # type: ignore[override] + def sort_values( self, by: IndexLabel, *, @@ -9204,8 +9201,9 @@ def any( ) -> DataFrame | Series: ... + # error: Missing return statement @doc(NDFrame.any, **_shared_doc_kwargs) - def any( + def any( # type: ignore[empty-body] self, axis: Axis = 0, bool_only: bool | None = None, diff --git a/pandas/core/interchange/column.py b/pandas/core/interchange/column.py index 75d07d1c2f7f6..c8675faec440c 100644 --- a/pandas/core/interchange/column.py +++ b/pandas/core/interchange/column.py @@ -81,10 +81,11 @@ def __init__(self, column: pd.Series, allow_copy: bool = True) -> None: self._col = column self._allow_copy = allow_copy - def size(self) -> int: + def size(self) -> int: # type: ignore[override] """ Size of the column, in elements. """ + # error: Signature of "size" incompatible with supertype "Column" [override] return self._col.size @property diff --git a/pandas/core/series.py b/pandas/core/series.py index 2664988a7b8d4..57c17ee3db480 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1655,14 +1655,9 @@ def to_string( return result else: if hasattr(buf, "write"): - # error: Item "str" of "Union[str, PathLike[str], WriteBuffer - # [str]]" has no attribute "write" - buf.write(result) # type: ignore[union-attr] + buf.write(result) else: - # error: Argument 1 to "open" has incompatible type "Union[str, - # PathLike[str], WriteBuffer[str]]"; expected "Union[Union[str, - # bytes, PathLike[str], PathLike[bytes]], int]" - with open(buf, "w") as f: # type: ignore[arg-type] + with open(buf, "w") as f: f.write(result) return None @@ -4447,8 +4442,9 @@ def any( ) -> Series | bool: ... + # error: Missing return statement @doc(NDFrame.any, **_shared_doc_kwargs) - def any( + def any( # type: ignore[empty-body] self, axis: Axis = 0, bool_only: bool | None = None, @@ -5061,9 +5057,8 @@ def fillna( ) -> Series | None: ... - # error: Signature of "fillna" incompatible with supertype "NDFrame" @doc(NDFrame.fillna, **_shared_doc_kwargs) - def fillna( # type: ignore[override] + def fillna( self, value: Hashable | Mapping | Series | DataFrame = None, *, @@ -5136,14 +5131,13 @@ def replace( ) -> None: ... - # error: Signature of "replace" incompatible with supertype "NDFrame" @doc( NDFrame.replace, klass=_shared_doc_kwargs["klass"], inplace=_shared_doc_kwargs["inplace"], replace_iloc=_shared_doc_kwargs["replace_iloc"], ) - def replace( # type: ignore[override] + def replace( self, to_replace=None, value=lib.no_default, diff --git a/pandas/io/common.py b/pandas/io/common.py index 265de02dd5d6b..4dae46c8f39f6 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -1053,8 +1053,7 @@ def __getattr__(self, name: str): def readable(self) -> bool: if hasattr(self.buffer, "readable"): - # error: "BaseBuffer" has no attribute "readable" - return self.buffer.readable() # type: ignore[attr-defined] + return self.buffer.readable() return True def seekable(self) -> bool: @@ -1064,8 +1063,7 @@ def seekable(self) -> bool: def writable(self) -> bool: if hasattr(self.buffer, "writable"): - # error: "BaseBuffer" has no attribute "writable" - return self.buffer.writable() # type: ignore[attr-defined] + return self.buffer.writable() return True diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 84947c4cfa4fc..0f19376cca86a 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1835,8 +1835,7 @@ def _load_backend(backend: str) -> types.ModuleType: # entry_points lost dict API ~ PY 3.10 # https://github.com/python/importlib_metadata/issues/298 if hasattr(eps, "select"): - # error: "Dict[str, Tuple[EntryPoint, ...]]" has no attribute "select" - entry = eps.select(group=key) # type: ignore[attr-defined] + entry = eps.select(group=key) else: entry = eps.get(key, ()) for entry_point in entry: diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index f18066769f214..e098127d89889 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -397,13 +397,7 @@ def decorator(decorated: F) -> F: if docstring is None: continue if hasattr(docstring, "_docstring_components"): - # error: Item "str" of "Union[str, Callable[..., Any]]" has no attribute - # "_docstring_components" - # error: Item "function" of "Union[str, Callable[..., Any]]" has no - # attribute "_docstring_components" - docstring_components.extend( - docstring._docstring_components # type: ignore[union-attr] - ) + docstring_components.extend(docstring._docstring_components) elif isinstance(docstring, str) or docstring.__doc__: docstring_components.append(docstring) diff --git a/requirements-dev.txt b/requirements-dev.txt index 5e98113625374..30ee35db05c50 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -67,7 +67,7 @@ cpplint flake8==5.0.4 flake8-bugbear==22.7.1 isort>=5.2.1 -mypy==0.981 +mypy==0.990 pre-commit>=2.15.0 pycodestyle pyupgrade From a2896f4bddbff40d3dc08b622822687019246ef7 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 16 Nov 2022 16:14:26 +0100 Subject: [PATCH 2/3] Fix stub error --- pandas/_libs/tslibs/offsets.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/offsets.pyi b/pandas/_libs/tslibs/offsets.pyi index 44e7e983b4038..eacdf17b0b4d3 100644 --- a/pandas/_libs/tslibs/offsets.pyi +++ b/pandas/_libs/tslibs/offsets.pyi @@ -253,7 +253,8 @@ class CustomBusinessHour(BusinessHour): class CustomBusinessMonthEnd(_CustomBusinessMonth): ... class CustomBusinessMonthBegin(_CustomBusinessMonth): ... -class DateOffset(RelativeDeltaOffset): ... +class OffsetMeta(type): ... +class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta): ... BDay = BusinessDay BMonthEnd = BusinessMonthEnd From 139b60563cd3ff81b4c73e8479bea09da9903475 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 16 Nov 2022 17:39:38 +0100 Subject: [PATCH 3/3] Fix pyright --- pandas/core/base.py | 4 +++- pandas/plotting/_core.py | 2 +- pandas/util/_decorators.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index ee7b34591adbb..4dbe755a20b0f 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1107,7 +1107,9 @@ def _memory_usage(self, deep: bool = False) -> int: are not components of the array if deep=False or if used on PyPy """ if hasattr(self.array, "memory_usage"): - return self.array.memory_usage(deep=deep) + return self.array.memory_usage( # pyright: ignore[reportGeneralTypeIssues] + deep=deep, + ) v = self.array.nbytes if deep and is_object_dtype(self) and not PYPY: diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0f19376cca86a..da6c1ad47d2dd 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1835,7 +1835,7 @@ def _load_backend(backend: str) -> types.ModuleType: # entry_points lost dict API ~ PY 3.10 # https://github.com/python/importlib_metadata/issues/298 if hasattr(eps, "select"): - entry = eps.select(group=key) + entry = eps.select(group=key) # pyright: ignore[reportGeneralTypeIssues] else: entry = eps.get(key, ()) for entry_point in entry: diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index e098127d89889..8fef3a231a748 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -397,7 +397,9 @@ def decorator(decorated: F) -> F: if docstring is None: continue if hasattr(docstring, "_docstring_components"): - docstring_components.extend(docstring._docstring_components) + docstring_components.extend( + docstring._docstring_components # pyright: ignore[reportGeneralTypeIssues] # noqa: E501 + ) elif isinstance(docstring, str) or docstring.__doc__: docstring_components.append(docstring)