From 14be337554f978f30f2e87ba206bf122a2044d7d Mon Sep 17 00:00:00 2001 From: phofl Date: Tue, 26 Jan 2021 00:27:55 +0100 Subject: [PATCH 1/2] TYP: Make mypy 0.800 compatible --- environment.yml | 2 +- pandas/io/common.py | 2 +- pandas/io/stata.py | 6 +++--- pandas/plotting/_matplotlib/boxplot.py | 2 +- pandas/tests/arrays/string_/test_string.py | 10 +--------- pandas/tests/dtypes/test_inference.py | 2 +- pandas/tests/io/parser/common/test_file_buffer_url.py | 4 +++- pandas/tests/io/parser/test_header.py | 2 +- pandas/tests/window/conftest.py | 9 ++------- pandas/util/_decorators.py | 2 +- requirements-dev.txt | 2 +- 11 files changed, 16 insertions(+), 27 deletions(-) diff --git a/environment.yml b/environment.yml index be6fb9a4cac7d..aa0091531f237 100644 --- a/environment.yml +++ b/environment.yml @@ -23,7 +23,7 @@ dependencies: - flake8 - flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions - isort>=5.2.1 # check that imports are in the right order - - mypy=0.790 + - mypy - pre-commit>=2.9.2 - pycodestyle # used by flake8 - pyupgrade diff --git a/pandas/io/common.py b/pandas/io/common.py index 57ae46a421fbb..0603d5665eabb 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -182,7 +182,7 @@ def stringify_path( return cast(FileOrBuffer[AnyStr], filepath_or_buffer) # Only @runtime_checkable protocols can be used with instance and class checks - if isinstance(filepath_or_buffer, os.PathLike): # type: ignore[misc] + if isinstance(filepath_or_buffer, os.PathLike): filepath_or_buffer = filepath_or_buffer.__fspath__() return _expand_user(filepath_or_buffer) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 0f7137041710b..30f232c23f487 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -2491,9 +2491,9 @@ def write_file(self) -> None: self.handles.close() # Only @runtime_checkable protocols can be used with instance and class # checks - if isinstance( - self._fname, (str, os.PathLike) # type: ignore[misc] - ) and os.path.isfile(self._fname): + if isinstance(self._fname, (str, os.PathLike)) and os.path.isfile( + self._fname + ): try: os.unlink(self._fname) except OSError: diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index c1f3b3ef36260..1fb75cd9d13e4 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -28,7 +28,7 @@ class BoxPlot(LinePlot): _valid_return_types = (None, "axes", "dict", "both") # namedtuple to hold results - BP = namedtuple("Boxplot", ["ax", "lines"]) + BP = namedtuple("BP", ["ax", "lines"]) def __init__(self, data, return_type="axes", **kwargs): # Do not call LinePlot.__init__ which may fill nan diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index d14de990d8268..8c766b96ecb9a 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -15,15 +15,7 @@ @pytest.fixture( - params=[ - # pandas\tests\arrays\string_\test_string.py:16: error: List item 1 has - # incompatible type "ParameterSet"; expected - # "Sequence[Collection[object]]" [list-item] - "string", - pytest.param( - "arrow_string", marks=skip_if_no_pyarrow - ), # type:ignore[list-item] - ] + params=["string", pytest.param("arrow_string", marks=skip_if_no_pyarrow)] ) def dtype(request): return request.param diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index e1f339e360c77..415fe0309b073 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -300,7 +300,7 @@ class MockFile: assert not is_file(data) -test_tuple = collections.namedtuple("Test", ["a", "b", "c"]) +test_tuple = collections.namedtuple("test_tuple", ["a", "b", "c"]) @pytest.mark.parametrize("ll", [test_tuple(1, 2, 3)]) diff --git a/pandas/tests/io/parser/common/test_file_buffer_url.py b/pandas/tests/io/parser/common/test_file_buffer_url.py index d0f1d63f88b3e..2ae16ba1fbee6 100644 --- a/pandas/tests/io/parser/common/test_file_buffer_url.py +++ b/pandas/tests/io/parser/common/test_file_buffer_url.py @@ -5,6 +5,7 @@ from io import BytesIO, StringIO import os import platform +from typing import Union from urllib.error import URLError import pytest @@ -330,8 +331,9 @@ def test_read_csv_file_handle(all_parsers, io_class, encoding): parser = all_parsers expected = DataFrame({"a": [1], "b": [2]}) - content = "a,b\n1,2" + content: Union[str, bytes] = "a,b\n1,2" if io_class == BytesIO: + assert not isinstance(content, bytes) content = content.encode("utf-8") handle = io_class(content) diff --git a/pandas/tests/io/parser/test_header.py b/pandas/tests/io/parser/test_header.py index ae2808f494118..0b8dc1900ebb4 100644 --- a/pandas/tests/io/parser/test_header.py +++ b/pandas/tests/io/parser/test_header.py @@ -181,7 +181,7 @@ def test_header_multi_index_invalid(all_parsers, kwargs, msg): parser.read_csv(StringIO(data), header=[0, 1, 2, 3], **kwargs) -_TestTuple = namedtuple("names", ["first", "second"]) +_TestTuple = namedtuple("_TestTuple", ["first", "second"]) @pytest.mark.parametrize( diff --git a/pandas/tests/window/conftest.py b/pandas/tests/window/conftest.py index 70bead489d2c6..7ac033244fae7 100644 --- a/pandas/tests/window/conftest.py +++ b/pandas/tests/window/conftest.py @@ -113,12 +113,7 @@ def ignore_na(request): @pytest.fixture( - params=[ - pytest.param( - "numba", marks=td.skip_if_no("numba", "0.46.0") - ), # type: ignore[list-item] - "cython", - ] + params=[pytest.param("numba", marks=td.skip_if_no("numba", "0.46.0")), "cython"] ) def engine(request): """engine keyword argument for rolling.apply""" @@ -332,7 +327,7 @@ def halflife_with_times(request): "float64", "m8[ns]", "M8[ns]", - pytest.param( # type: ignore[list-item] + pytest.param( "datetime64[ns, UTC]", marks=pytest.mark.skip( "direct creation of extension dtype datetime64[ns, UTC] " diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index d002e8a4ebd43..f568e78703dd5 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -79,7 +79,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: {dedent(doc)}""" ) - return wrapper + return wrapper # type: ignore[return-value] def deprecate_kwarg( diff --git a/requirements-dev.txt b/requirements-dev.txt index 054c924daa81c..1e03f08acda4e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,7 +11,7 @@ cpplint flake8 flake8-comprehensions>=3.1.0 isort>=5.2.1 -mypy==0.790 +mypy pre-commit>=2.9.2 pycodestyle pyupgrade From beb14e7e15700f558511c26288f13b52a171160d Mon Sep 17 00:00:00 2001 From: phofl Date: Tue, 26 Jan 2021 20:13:45 +0100 Subject: [PATCH 2/2] Adress review --- environment.yml | 2 +- pandas/io/common.py | 1 - pandas/io/stata.py | 2 -- pandas/tests/io/parser/common/test_file_buffer_url.py | 8 ++------ pandas/util/_decorators.py | 3 ++- requirements-dev.txt | 2 +- 6 files changed, 6 insertions(+), 12 deletions(-) diff --git a/environment.yml b/environment.yml index aa0091531f237..cac705bff72ea 100644 --- a/environment.yml +++ b/environment.yml @@ -23,7 +23,7 @@ dependencies: - flake8 - flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions - isort>=5.2.1 # check that imports are in the right order - - mypy + - mypy=0.800 - pre-commit>=2.9.2 - pycodestyle # used by flake8 - pyupgrade diff --git a/pandas/io/common.py b/pandas/io/common.py index 0603d5665eabb..f71522b5c9555 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -181,7 +181,6 @@ def stringify_path( # this function with convert_file_like=True to infer the compression. return cast(FileOrBuffer[AnyStr], filepath_or_buffer) - # Only @runtime_checkable protocols can be used with instance and class checks if isinstance(filepath_or_buffer, os.PathLike): filepath_or_buffer = filepath_or_buffer.__fspath__() return _expand_user(filepath_or_buffer) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 30f232c23f487..9a5c9e4a2e2b2 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -2489,8 +2489,6 @@ def write_file(self) -> None: self._close() except Exception as exc: self.handles.close() - # Only @runtime_checkable protocols can be used with instance and class - # checks if isinstance(self._fname, (str, os.PathLike)) and os.path.isfile( self._fname ): diff --git a/pandas/tests/io/parser/common/test_file_buffer_url.py b/pandas/tests/io/parser/common/test_file_buffer_url.py index 2ae16ba1fbee6..018f93f1eb06b 100644 --- a/pandas/tests/io/parser/common/test_file_buffer_url.py +++ b/pandas/tests/io/parser/common/test_file_buffer_url.py @@ -5,7 +5,6 @@ from io import BytesIO, StringIO import os import platform -from typing import Union from urllib.error import URLError import pytest @@ -331,11 +330,8 @@ def test_read_csv_file_handle(all_parsers, io_class, encoding): parser = all_parsers expected = DataFrame({"a": [1], "b": [2]}) - content: Union[str, bytes] = "a,b\n1,2" - if io_class == BytesIO: - assert not isinstance(content, bytes) - content = content.encode("utf-8") - handle = io_class(content) + content = "a,b\n1,2" + handle = io_class(content.encode("utf-8") if io_class == BytesIO else content) tm.assert_frame_equal(parser.read_csv(handle, encoding=encoding), expected) assert not handle.closed diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index f568e78703dd5..a13fb1ce57f6c 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -78,7 +78,8 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]: {dedent(doc)}""" ) - + # error: Incompatible return value type (got "Callable[[VarArg(Any), + # KwArg(Any)], Callable[...,Any]]", expected "Callable[[F], F]") return wrapper # type: ignore[return-value] diff --git a/requirements-dev.txt b/requirements-dev.txt index 1e03f08acda4e..9bae344adc520 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,7 +11,7 @@ cpplint flake8 flake8-comprehensions>=3.1.0 isort>=5.2.1 -mypy +mypy==0.800 pre-commit>=2.9.2 pycodestyle pyupgrade