From 79cdcda99057856dd1cea7e9fa056cb4d24d241c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 6 Feb 2023 23:42:07 +0100 Subject: [PATCH] TYP: Upgrade mypy to 1.0 --- doc/source/whatsnew/v2.0.0.rst | 2 +- environment.yml | 2 +- pandas/core/arrays/arrow/array.py | 5 +---- pandas/core/arrays/boolean.py | 6 ++++-- pandas/core/arrays/datetimes.py | 8 ++------ pandas/core/arrays/floating.py | 6 ++++-- pandas/core/arrays/integer.py | 6 ++++-- pandas/core/arrays/sparse/array.py | 6 +----- pandas/core/dtypes/cast.py | 8 ++------ pandas/core/frame.py | 13 +++++++------ pandas/core/reshape/merge.py | 8 ++------ pandas/core/series.py | 6 ++---- pandas/io/formats/format.py | 4 +++- pandas/io/json/_json.py | 7 +------ requirements-dev.txt | 2 +- 15 files changed, 36 insertions(+), 53 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index df30a31889a99..353ae4455caba 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -650,7 +650,7 @@ If installed, we now require: +-------------------+-----------------+----------+---------+ | Package | Minimum Version | Required | Changed | +===================+=================+==========+=========+ -| mypy (dev) | 0.991 | | X | +| mypy (dev) | 1.0 | | X | +-------------------+-----------------+----------+---------+ | pytest (dev) | 7.0.0 | | X | +-------------------+-----------------+----------+---------+ diff --git a/environment.yml b/environment.yml index 076e6fa727332..05251001d8e86 100644 --- a/environment.yml +++ b/environment.yml @@ -79,7 +79,7 @@ dependencies: - cpplint - flake8=6.0.0 - isort>=5.2.1 # check that imports are in the right order - - mypy=0.991 + - mypy=1.0 - pre-commit>=2.15.0 - pyupgrade - ruff=0.0.215 diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py index 9247d26fc846d..254f7ad28f324 100644 --- a/pandas/core/arrays/arrow/array.py +++ b/pandas/core/arrays/arrow/array.py @@ -330,10 +330,7 @@ def __getitem__(self, item: PositionalIndexer): elif isinstance(item, tuple): item = unpack_tuple_and_ellipses(item) - # error: Non-overlapping identity check (left operand type: - # "Union[Union[int, integer[Any]], Union[slice, List[int], - # ndarray[Any, Any]]]", right operand type: "ellipsis") - if item is Ellipsis: # type: ignore[comparison-overlap] + if item is Ellipsis: # TODO: should be handled by pyarrow? item = slice(None) diff --git a/pandas/core/arrays/boolean.py b/pandas/core/arrays/boolean.py index 5f8169829ab6b..543e39d25f030 100644 --- a/pandas/core/arrays/boolean.py +++ b/pandas/core/arrays/boolean.py @@ -288,8 +288,10 @@ class BooleanArray(BaseMaskedArray): # The value used to fill '_data' to avoid upcasting _internal_fill_value = False # Fill values used for any/all - _truthy_value = True - _falsey_value = False + # Incompatible types in assignment (expression has type "bool", base class + # "BaseMaskedArray" defined the type as "") + _truthy_value = True # type: ignore[assignment] + _falsey_value = False # type: ignore[assignment] _TRUE_VALUES = {"True", "TRUE", "true", "1", "1.0"} _FALSE_VALUES = {"False", "FALSE", "false", "0", "0.0"} diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 0766b1c6a5262..1bf43f61a67a7 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2500,9 +2500,7 @@ def _generate_range( # Argument 1 to "Timestamp" has incompatible type "Optional[Timestamp]"; # expected "Union[integer[Any], float, str, date, datetime64]" start = Timestamp(start) # type: ignore[arg-type] - # Non-overlapping identity check (left operand type: "Timestamp", right - # operand type: "NaTType") - if start is not NaT: # type: ignore[comparison-overlap] + if start is not NaT: start = start.as_unit(unit) else: start = None @@ -2510,9 +2508,7 @@ def _generate_range( # Argument 1 to "Timestamp" has incompatible type "Optional[Timestamp]"; # expected "Union[integer[Any], float, str, date, datetime64]" end = Timestamp(end) # type: ignore[arg-type] - # Non-overlapping identity check (left operand type: "Timestamp", right - # operand type: "NaTType") - if end is not NaT: # type: ignore[comparison-overlap] + if end is not NaT: end = end.as_unit(unit) else: end = None diff --git a/pandas/core/arrays/floating.py b/pandas/core/arrays/floating.py index ccf8431660acb..e08e99f7eab94 100644 --- a/pandas/core/arrays/floating.py +++ b/pandas/core/arrays/floating.py @@ -116,8 +116,10 @@ class FloatingArray(NumericArray): # The value used to fill '_data' to avoid upcasting _internal_fill_value = np.nan # Fill values used for any/all - _truthy_value = 1.0 - _falsey_value = 0.0 + # Incompatible types in assignment (expression has type "float", base class + # "BaseMaskedArray" defined the type as "") + _truthy_value = 1.0 # type: ignore[assignment] + _falsey_value = 0.0 # type: ignore[assignment] _dtype_docstring = """ diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 24e5fa1bef552..af0f80eb0c85d 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -132,8 +132,10 @@ class IntegerArray(NumericArray): # The value used to fill '_data' to avoid upcasting _internal_fill_value = 1 # Fill values used for any/all - _truthy_value = 1 - _falsey_value = 0 + # Incompatible types in assignment (expression has type "int", base class + # "BaseMaskedArray" defined the type as "") + _truthy_value = 1 # type: ignore[assignment] + _falsey_value = 0 # type: ignore[assignment] _dtype_docstring = """ diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index c8c33d3f52102..38f97e6f12501 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -912,11 +912,7 @@ def __getitem__( if isinstance(key, tuple): key = unpack_tuple_and_ellipses(key) - # Non-overlapping identity check (left operand type: - # "Union[Union[Union[int, integer[Any]], Union[slice, List[int], - # ndarray[Any, Any]]], Tuple[Union[int, ellipsis], ...]]", - # right operand type: "ellipsis") - if key is Ellipsis: # type: ignore[comparison-overlap] + if key is Ellipsis: raise ValueError("Cannot slice with Ellipsis") if is_integer(key): diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 3be89f6da2bd8..84e27d256e83d 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -787,16 +787,12 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj, elif isinstance(val, (np.datetime64, dt.datetime)): try: val = Timestamp(val) - # error: Non-overlapping identity check (left operand type: - # "Timestamp", right operand type: "NaTType") - if val is not NaT: # type: ignore[comparison-overlap] + if val is not NaT: val = val.as_unit("ns") except OutOfBoundsDatetime: return _dtype_obj, val - # error: Non-overlapping identity check (left operand type: "Timestamp", - # right operand type: "NaTType") - if val is NaT or val.tz is None: # type: ignore[comparison-overlap] + if val is NaT or val.tz is None: val = val.to_datetime64() dtype = val.dtype # TODO: test with datetime(2920, 10, 1) based on test_replace_dtypes diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4f13ead4005e7..2361c254f5161 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2633,13 +2633,15 @@ def to_stata( raise ValueError("strl is not supported in format 114") from pandas.io.stata import StataWriter as statawriter elif version == 117: - # mypy: Name 'statawriter' already defined (possibly by an import) - from pandas.io.stata import ( # type: ignore[no-redef] + # Incompatible import of "statawriter" (imported name has type + # "Type[StataWriter117]", local name has type "Type[StataWriter]") + from pandas.io.stata import ( # type: ignore[assignment] StataWriter117 as statawriter, ) else: # versions 118 and 119 - # mypy: Name 'statawriter' already defined (possibly by an import) - from pandas.io.stata import ( # type: ignore[no-redef] + # Incompatible import of "statawriter" (imported name has type + # "Type[StataWriter117]", local name has type "Type[StataWriter]") + from pandas.io.stata import ( # type: ignore[assignment] StataWriterUTF8 as statawriter, ) @@ -5514,8 +5516,7 @@ def pop(self, item: Hashable) -> Series: """ return super().pop(item=item) - # error: Signature of "replace" incompatible with supertype "NDFrame" - @overload # type: ignore[override] + @overload def replace( self, to_replace=..., diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 7d8d7a37ff7e7..8c4576ee554ec 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1325,9 +1325,7 @@ def _maybe_coerce_merge_keys(self) -> None: mask = ~np.isnan(lk) match = lk == casted - # error: Item "ExtensionArray" of "Union[ExtensionArray, - # ndarray[Any, Any], Any]" has no attribute "all" - if not match[mask].all(): # type: ignore[union-attr] + if not match[mask].all(): warnings.warn( "You are merging on int and float " "columns where the float values " @@ -1347,9 +1345,7 @@ def _maybe_coerce_merge_keys(self) -> None: mask = ~np.isnan(rk) match = rk == casted - # error: Item "ExtensionArray" of "Union[ExtensionArray, - # ndarray[Any, Any], Any]" has no attribute "all" - if not match[mask].all(): # type: ignore[union-attr] + if not match[mask].all(): warnings.warn( "You are merging on int and float " "columns where the float values " diff --git a/pandas/core/series.py b/pandas/core/series.py index 9a5412145366f..80dd0dd19f96f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3370,8 +3370,7 @@ def update(self, other: Series | Sequence | Mapping) -> None: # ---------------------------------------------------------------------- # Reindexing, sorting - # error: Signature of "sort_values" incompatible with supertype "NDFrame" - @overload # type: ignore[override] + @overload def sort_values( self, *, @@ -5131,8 +5130,7 @@ def pop(self, item: Hashable) -> Any: """ return super().pop(item=item) - # error: Signature of "replace" incompatible with supertype "NDFrame" - @overload # type: ignore[override] + @overload def replace( self, to_replace=..., diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 946c22f47596e..afe6a405afbb8 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1229,7 +1229,9 @@ def get_buffer( raise ValueError("buf is not a file name and encoding is specified.") if hasattr(buf, "write"): - yield buf + # Incompatible types in "yield" (actual type "Union[str, WriteBuffer[str], + # StringIO]", expected type "Union[WriteBuffer[str], StringIO]") + yield buf # type: ignore[misc] elif isinstance(buf, str): check_parent_directory(str(buf)) with open(buf, "w", encoding=encoding, newline="") as f: diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 66991eab54671..338e831ed184f 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -1138,12 +1138,7 @@ def _try_convert_data( return data, False return data.fillna(np.nan), True - # error: Non-overlapping identity check (left operand type: - # "Union[ExtensionDtype, str, dtype[Any], Type[object], - # Dict[Hashable, Union[ExtensionDtype, Union[str, dtype[Any]], - # Type[str], Type[float], Type[int], Type[complex], Type[bool], - # Type[object]]]]", right operand type: "Literal[True]") - elif self.dtype is True: # type: ignore[comparison-overlap] + elif self.dtype is True: pass else: # dtype to force diff --git a/requirements-dev.txt b/requirements-dev.txt index 04d8b176dffae..3783c7c2aeb5f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -56,7 +56,7 @@ black==22.10.0 cpplint flake8==6.0.0 isort>=5.2.1 -mypy==0.991 +mypy==1.0 pre-commit>=2.15.0 pyupgrade ruff==0.0.215