From 7e55eae18a7c20449f4df6f226d6427a8b48aa41 Mon Sep 17 00:00:00 2001 From: Levi Ob Date: Sun, 2 Oct 2022 15:00:22 -0700 Subject: [PATCH 1/5] STYLE: Fix Pylint Style Checks For used-before-assignment --- pandas/_version.py | 2 +- pandas/core/dtypes/common.py | 2 +- pandas/core/groupby/indexing.py | 4 +++- pandas/core/tools/datetimes.py | 3 ++- pandas/io/formats/style.py | 3 +-- pandas/tests/io/excel/test_writers.py | 3 +-- pandas/tests/io/pytables/test_store.py | 10 +++++----- pyproject.toml | 1 - versioneer.py | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/_version.py b/pandas/_version.py index a5eec4222ca41..074e12b7849b4 100644 --- a/pandas/_version.py +++ b/pandas/_version.py @@ -72,8 +72,8 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env= assert isinstance(commands, list) p = None for c in commands: + dispcmd = str([c] + args) try: - dispcmd = str([c] + args) # remember shell=False, so use git.cmd on windows, not just git p = subprocess.Popen( [c] + args, diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index f8752e1c16c38..3bbd5370bea15 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -266,7 +266,7 @@ def is_scipy_sparse(arr) -> bool: """ global _is_scipy_sparse - if _is_scipy_sparse is None: + if _is_scipy_sparse is None: # pylint: disable=used-before-assignment try: from scipy.sparse import issparse as _is_scipy_sparse except ImportError: diff --git a/pandas/core/groupby/indexing.py b/pandas/core/groupby/indexing.py index be7b7b3369e89..37a35183d6e06 100644 --- a/pandas/core/groupby/indexing.py +++ b/pandas/core/groupby/indexing.py @@ -112,7 +112,9 @@ def _positional_selector(self) -> GroupByPositionalSelector: 4 b 5 """ if TYPE_CHECKING: - groupby_self = cast(groupby.GroupBy, self) + groupby_self = cast( + groupby.GroupBy, self # pylint: disable=used-before-assignment + ) else: groupby_self = self diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index dd4b21ade5cf8..4db240985d1d2 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -1074,8 +1074,9 @@ def to_datetime( exact=exact, infer_datetime_format=infer_datetime_format, ) - + # pylint: disable=used-before-assignment result: Timestamp | NaTType | Series | Index + # pylint: enable=used-before-assignment if isinstance(arg, Timestamp): result = arg diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 471e77b56608d..a3f66d9d1fbfd 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -82,7 +82,6 @@ has_mpl = True except ImportError: has_mpl = False - no_mpl_message = "{0} requires matplotlib." @contextmanager @@ -90,7 +89,7 @@ def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]: if has_mpl: yield plt, mpl else: - raise ImportError(no_mpl_message.format(func.__name__)) + raise ImportError(f"{func.__name__} requires matplotlib.") #### diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 3f9ab78e720b9..e63d2e724bedf 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -883,10 +883,9 @@ def test_to_excel_unicode_filename(self, ext): with tm.ensure_clean("\u0192u." + ext) as filename: try: f = open(filename, "wb") + f.close() except UnicodeEncodeError: pytest.skip("No unicode file names on this system") - finally: - f.close() df = DataFrame( [[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]], diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index db87c8facbfdb..6fc86013aa51c 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -877,13 +877,13 @@ def test_copy(): with catch_warnings(record=True): def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs): - try: - store = HDFStore(f, "r") + if new_f is None: + import tempfile - if new_f is None: - import tempfile + fd, new_f = tempfile.mkstemp() - fd, new_f = tempfile.mkstemp() + try: + store = HDFStore(f, "r") tstore = store.copy(new_f, keys=keys, propindexes=propindexes, **kwargs) # check keys diff --git a/pyproject.toml b/pyproject.toml index 3e87d237170aa..62aa156756966 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,7 +63,6 @@ disable = [ "unsubscriptable-object", "unsupported-assignment-operation", "unsupported-membership-test", - "used-before-assignment", ] [tool.pytest.ini_options] diff --git a/versioneer.py b/versioneer.py index c98dbd83271d7..bd9d974498547 100644 --- a/versioneer.py +++ b/versioneer.py @@ -385,8 +385,8 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env= assert isinstance(commands, list) p = None for c in commands: + dispcmd = str([c] + args) try: - dispcmd = str([c] + args) # remember shell=False, so use git.cmd on windows, not just git p = subprocess.Popen( [c] + args, From 7fe2c29101acb00554e170b0e6dedc21ed61ae51 Mon Sep 17 00:00:00 2001 From: Levi Ob Date: Wed, 5 Oct 2022 11:18:44 -0700 Subject: [PATCH 2/5] Refactor: Use 'with open' instead of open/close --- pandas/tests/io/excel/test_writers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index e63d2e724bedf..a01f912508717 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -882,8 +882,8 @@ def test_to_excel_output_encoding(self, ext): def test_to_excel_unicode_filename(self, ext): with tm.ensure_clean("\u0192u." + ext) as filename: try: - f = open(filename, "wb") - f.close() + with open(filename, "wb"): + pass except UnicodeEncodeError: pytest.skip("No unicode file names on this system") From 1a6df65d49744ef91cca3380a7dac2e9f45bde5c Mon Sep 17 00:00:00 2001 From: Levi Ob Date: Mon, 14 Nov 2022 22:32:19 -0800 Subject: [PATCH 3/5] CLN: disable used-before-assignment warning --- pandas/core/groupby/indexing.py | 5 ++--- pandas/core/tools/datetimes.py | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/groupby/indexing.py b/pandas/core/groupby/indexing.py index 37a35183d6e06..d41c3de6be338 100644 --- a/pandas/core/groupby/indexing.py +++ b/pandas/core/groupby/indexing.py @@ -112,9 +112,8 @@ def _positional_selector(self) -> GroupByPositionalSelector: 4 b 5 """ if TYPE_CHECKING: - groupby_self = cast( - groupby.GroupBy, self # pylint: disable=used-before-assignment - ) + # pylint: disable-next=used-before-assignment + groupby_self = cast(groupby.GroupBy, self) else: groupby_self = self diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 4db240985d1d2..9133644a6bcd6 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -1074,9 +1074,8 @@ def to_datetime( exact=exact, infer_datetime_format=infer_datetime_format, ) - # pylint: disable=used-before-assignment + # pylint: disable-next=used-before-assignment result: Timestamp | NaTType | Series | Index - # pylint: enable=used-before-assignment if isinstance(arg, Timestamp): result = arg From cd788e5486a7321ed25b641835216ca7c7f1f28d Mon Sep 17 00:00:00 2001 From: Levi Ob Date: Mon, 14 Nov 2022 22:52:33 -0800 Subject: [PATCH 4/5] CLN: Remove used-before-assignment from pyproject.toml --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index eca46b9417fbe..d30f700a54f80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,7 +75,6 @@ disable = [ "unsubscriptable-object", "unsupported-assignment-operation", "unsupported-membership-test", - "used-before-assignment", # pylint type "C": convention, for programming standard violation "import-outside-toplevel", From 0082734b4b4f42c9db3be427b71257e5ab182089 Mon Sep 17 00:00:00 2001 From: Levi Ob Date: Mon, 14 Nov 2022 23:35:00 -0800 Subject: [PATCH 5/5] CLN: disable used-before-assignment warning in-line --- pandas/core/arrays/sparse/array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index b2fe02321ee0c..4c5dbeb6eedf9 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1309,6 +1309,7 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True): values = np.array(self) return astype_nansafe(values, dtype=future_dtype) else: + # pylint: disable-next=used-before-assignment dtype = cast(ExtensionDtype, dtype) cls = dtype.construct_array_type() return cls._from_sequence(self, dtype=dtype, copy=copy)