From d1534181c0bd165f354179d1ac131d874b71a81b Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 24 Apr 2020 21:51:32 +0300 Subject: [PATCH 1/4] pre-commit: upgrade flake8 3.7.7 -> 3.8.1 New errors: testing/test_setupplan.py:104:15: E741 ambiguous variable name 'l' testing/test_setupplan.py:107:15: E741 ambiguous variable name 'l' extra/get_issues.py:48:29: E741 ambiguous variable name 'l' testing/test_error_diffs.py:270:32: E741 ambiguous variable name 'l' Not so sure about it but easier to just fix. But more importantly, is a large amount of typing-related issues there were fixed which necessitated noqa's which can now be removed. --- .pre-commit-config.yaml | 4 ++-- extra/get_issues.py | 2 +- testing/test_error_diffs.py | 2 +- testing/test_setupplan.py | 8 ++++++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fd909dd5d..81e30ecc1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,11 +21,11 @@ repos: exclude: _pytest/debugging.py language_version: python3 - repo: https://gitlab.com/pycqa/flake8 - rev: 3.7.7 + rev: 3.8.1 hooks: - id: flake8 language_version: python3 - additional_dependencies: [flake8-typing-imports==1.3.0] + additional_dependencies: [flake8-typing-imports==1.9.0] - repo: https://github.com/asottile/reorder_python_imports rev: v1.4.0 hooks: diff --git a/extra/get_issues.py b/extra/get_issues.py index 9407aeded..c264b2644 100644 --- a/extra/get_issues.py +++ b/extra/get_issues.py @@ -45,7 +45,7 @@ def main(args): def _get_kind(issue): - labels = [l["name"] for l in issue["labels"]] + labels = [label["name"] for label in issue["labels"]] for key in ("bug", "enhancement", "proposal"): if key in labels: return key diff --git a/testing/test_error_diffs.py b/testing/test_error_diffs.py index c7198bde0..473c62a75 100644 --- a/testing/test_error_diffs.py +++ b/testing/test_error_diffs.py @@ -267,7 +267,7 @@ if sys.version_info[:2] >= (3, 7): @pytest.mark.parametrize("code, expected", TESTCASES) def test_error_diff(code, expected, testdir): - expected = [l.lstrip() for l in expected.splitlines()] + expected = [line.lstrip() for line in expected.splitlines()] p = testdir.makepyfile(code) result = testdir.runpytest(p, "-vv") result.stdout.fnmatch_lines(expected) diff --git a/testing/test_setupplan.py b/testing/test_setupplan.py index a44474dd1..64b464b32 100644 --- a/testing/test_setupplan.py +++ b/testing/test_setupplan.py @@ -101,10 +101,14 @@ def test_show_multi_test_fixture_setup_and_teardown_same_as_setup_show(testdir): # the number and text of these lines should be identical plan_lines = [ - l for l in plan_result.stdout.lines if "SETUP" in l or "TEARDOWN" in l + line + for line in plan_result.stdout.lines + if "SETUP" in line or "TEARDOWN" in line ] show_lines = [ - l for l in show_result.stdout.lines if "SETUP" in l or "TEARDOWN" in l + line + for line in show_result.stdout.lines + if "SETUP" in line or "TEARDOWN" in line ] assert plan_lines == show_lines From 59a12e9ab3990496940b79eba8dde6fcd481c8c3 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 24 Apr 2020 22:15:45 +0300 Subject: [PATCH 2/4] Replace bare `except`s with `except BaseException` Mostly I wanted to remove uses of `noqa`. In Python 3 the two are the same. --- src/_pytest/_code/code.py | 6 +++--- src/_pytest/main.py | 2 +- src/_pytest/python_api.py | 2 +- src/_pytest/runner.py | 2 +- src/_pytest/unittest.py | 2 +- testing/code/test_excinfo.py | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 6102084f0..bc0e36693 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -277,7 +277,7 @@ class TracebackEntry: line = str(self.statement).lstrip() except KeyboardInterrupt: raise - except: # noqa + except BaseException: line = "???" return " File %r:%d in %s\n %s\n" % (fn, self.lineno + 1, name, line) @@ -667,12 +667,12 @@ class FormattedExcinfo: s = str(source.getstatement(len(source) - 1)) except KeyboardInterrupt: raise - except: # noqa + except BaseException: try: s = str(source[-1]) except KeyboardInterrupt: raise - except: # noqa + except BaseException: return 0 return 4 + (len(s) - len(s.lstrip())) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 61eb7ca74..de7e16744 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -206,7 +206,7 @@ def wrap_session( ) config.hook.pytest_keyboard_interrupt(excinfo=excinfo) session.exitstatus = exitstatus - except: # noqa + except BaseException: session.exitstatus = ExitCode.INTERNAL_ERROR excinfo = _pytest._code.ExceptionInfo.from_current() try: diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 7f52778b9..78051172d 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -123,7 +123,7 @@ class ApproxNumpy(ApproxBase): if not np.isscalar(actual): try: actual = np.asarray(actual) - except: # noqa + except BaseException: raise TypeError("cannot compare '{}' to numpy.ndarray".format(actual)) if not np.isscalar(actual) and actual.shape != self.expected.shape: diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 76785ada7..e7211369c 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -258,7 +258,7 @@ class CallInfo: precise_start = perf_counter() try: result = func() - except: # noqa + except BaseException: excinfo = ExceptionInfo.from_current() if reraise is not None and excinfo.errisinstance(reraise): raise diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index fc3d1a515..773f545af 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -151,7 +151,7 @@ class TestCaseFunction(Function): fail("".join(values), pytrace=False) except (fail.Exception, KeyboardInterrupt): raise - except: # noqa + except BaseException: fail( "ERROR: Unknown Incompatible Exception " "representation:\n%r" % (rawexcinfo,), diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index f0c7146c7..08c0619e3 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -239,7 +239,7 @@ class TestTraceback_f_g_h: def f(n: int) -> None: try: do_stuff() - except: # noqa + except BaseException: reraise_me() excinfo = pytest.raises(RuntimeError, f, 8) @@ -445,7 +445,7 @@ class TestFormattedExcinfo: exec(source.compile()) except KeyboardInterrupt: raise - except: # noqa + except BaseException: return _pytest._code.ExceptionInfo.from_current() assert 0, "did not raise" From 23c9856857b4fad351491b064a30a1e1eddc0589 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 24 Apr 2020 22:24:40 +0300 Subject: [PATCH 3/4] Remove no longer needed noqa's --- src/_pytest/_code/code.py | 2 +- src/_pytest/capture.py | 2 +- src/_pytest/compat.py | 2 +- src/_pytest/config/argparsing.py | 2 +- src/_pytest/config/findpaths.py | 2 +- src/_pytest/logging.py | 14 +++++++------- src/_pytest/mark/legacy.py | 2 +- src/_pytest/nodes.py | 2 +- src/_pytest/outcomes.py | 2 +- src/_pytest/python.py | 4 ++-- src/_pytest/python_api.py | 2 +- 11 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index bc0e36693..2075fd0eb 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -44,7 +44,7 @@ from _pytest.compat import TYPE_CHECKING if TYPE_CHECKING: from typing import Type from typing_extensions import Literal - from weakref import ReferenceType # noqa: F401 + from weakref import ReferenceType _TracebackStyle = Literal["long", "short", "line", "no", "native"] diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index d34bf23f8..7eafeb3e4 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -669,7 +669,7 @@ class SysCaptureBinary: class SysCapture(SysCaptureBinary): - EMPTY_BUFFER = "" # type: ignore[assignment] # noqa: F821 + EMPTY_BUFFER = "" # type: ignore[assignment] def snap(self): res = self.tmpfile.getvalue() diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index 6a0614f01..4cc22ba4a 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -32,7 +32,7 @@ else: if TYPE_CHECKING: - from typing import Type # noqa: F401 (used in type string) + from typing import Type _T = TypeVar("_T") diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index 940eaa6a7..b57db92ca 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -21,7 +21,7 @@ from _pytest.config.exceptions import UsageError if TYPE_CHECKING: from typing import NoReturn - from typing_extensions import Literal # noqa: F401 + from typing_extensions import Literal FILE_OR_DIR = "file_or_dir" diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 101fdf66f..f4f62e06b 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -12,7 +12,7 @@ from _pytest.compat import TYPE_CHECKING from _pytest.outcomes import fail if TYPE_CHECKING: - from . import Config # noqa: F401 + from . import Config def exists(path, ignore=OSError): diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 5e60a2321..681fdee62 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -338,7 +338,7 @@ class LogCaptureFixture: """ :rtype: LogCaptureHandler """ - return self._item.catch_log_handler # type: ignore[no-any-return] # noqa: F723 + return self._item.catch_log_handler # type: ignore[no-any-return] def get_records(self, when: str) -> List[logging.LogRecord]: """ @@ -354,7 +354,7 @@ class LogCaptureFixture: """ handler = self._item.catch_log_handlers.get(when) if handler: - return handler.records # type: ignore[no-any-return] # noqa: F723 + return handler.records # type: ignore[no-any-return] else: return [] @@ -640,15 +640,15 @@ class LoggingPlugin: return if not hasattr(item, "catch_log_handlers"): - item.catch_log_handlers = {} # type: ignore[attr-defined] # noqa: F821 - item.catch_log_handlers[when] = log_handler # type: ignore[attr-defined] # noqa: F821 - item.catch_log_handler = log_handler # type: ignore[attr-defined] # noqa: F821 + item.catch_log_handlers = {} # type: ignore[attr-defined] + item.catch_log_handlers[when] = log_handler # type: ignore[attr-defined] + item.catch_log_handler = log_handler # type: ignore[attr-defined] try: yield # run test finally: if when == "teardown": - del item.catch_log_handler # type: ignore[attr-defined] # noqa: F821 - del item.catch_log_handlers # type: ignore[attr-defined] # noqa: F821 + del item.catch_log_handler # type: ignore[attr-defined] + del item.catch_log_handlers # type: ignore[attr-defined] if self.print_logs: # Add a captured log section to the report. diff --git a/src/_pytest/mark/legacy.py b/src/_pytest/mark/legacy.py index a9461d4ce..1a9fdee8d 100644 --- a/src/_pytest/mark/legacy.py +++ b/src/_pytest/mark/legacy.py @@ -12,7 +12,7 @@ from _pytest.mark.expression import evaluate from _pytest.mark.expression import ParseError if TYPE_CHECKING: - from _pytest.nodes import Item # noqa: F401 (used in type string) + from _pytest.nodes import Item @attr.s diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index d55e00034..448e67127 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -33,7 +33,7 @@ from _pytest.store import Store if TYPE_CHECKING: # Imported here due to circular import. - from _pytest.main import Session # noqa: F401 + from _pytest.main import Session SEP = "/" diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index bed73c94d..7d7e9df7a 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -15,7 +15,7 @@ TYPE_CHECKING = False # avoid circular import through compat if TYPE_CHECKING: from typing import NoReturn - from typing import Type # noqa: F401 (Used in string type annotation.) + from typing import Type # noqa: F401 (used in type string) from typing_extensions import Protocol else: # typing.Protocol is only available starting from Python 3.8. It is also diff --git a/src/_pytest/python.py b/src/_pytest/python.py index ef3ebf791..6ae57cb37 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -136,7 +136,7 @@ def pytest_cmdline_main(config): def pytest_generate_tests(metafunc: "Metafunc") -> None: for marker in metafunc.definition.iter_markers(name="parametrize"): # TODO: Fix this type-ignore (overlapping kwargs). - metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker) # type: ignore[misc] # noqa: F821 + metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker) # type: ignore[misc] def pytest_configure(config): @@ -1013,7 +1013,7 @@ class Metafunc: func_name: str, ) -> List[Union[None, str]]: try: - num_ids = len(ids) # type: ignore[arg-type] # noqa: F821 + num_ids = len(ids) # type: ignore[arg-type] except TypeError: try: iter(ids) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 78051172d..a523caae5 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -27,7 +27,7 @@ from _pytest.compat import TYPE_CHECKING from _pytest.outcomes import fail if TYPE_CHECKING: - from typing import Type # noqa: F401 (used in type string) + from typing import Type BASE_TYPE = (type, STRING_TYPES) From 645aaa728d515558725eb4d9bf8414612dfe4513 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 12 May 2020 12:07:56 +0300 Subject: [PATCH 4/4] python_api: reduce scope of a `except BaseException` in ApproxNumpy I'm not sure if it can even raise at all, but catching BaseException would swallow ctrl-C and such and is definitely inappropriate here. --- src/_pytest/python_api.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index a523caae5..29c8af7e2 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -123,8 +123,10 @@ class ApproxNumpy(ApproxBase): if not np.isscalar(actual): try: actual = np.asarray(actual) - except BaseException: - raise TypeError("cannot compare '{}' to numpy.ndarray".format(actual)) + except Exception as e: + raise TypeError( + "cannot compare '{}' to numpy.ndarray".format(actual) + ) from e if not np.isscalar(actual) and actual.shape != self.expected.shape: return False