From d02618d173cc0229eb85befd3efbf6132135304a Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 9 Feb 2024 10:48:05 +0200 Subject: [PATCH 1/3] Fix ruff deprecation warning warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `pyproject.toml`: - 'ignore' -> 'lint.ignore' - 'select' -> 'lint.select' --- pyproject.toml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index efbb9b6c2..cf3265efe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,6 +87,11 @@ target-version = ['py38'] [tool.ruff] src = ["src"] line-length = 88 + +[tool.ruff.format] +docstring-code-format = true + +[tool.ruff.lint] select = [ "B", # bugbear "D", # pydocstyle @@ -130,9 +135,6 @@ ignore = [ "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` ] -[tool.ruff.format] -docstring-code-format = true - [tool.ruff.lint.pycodestyle] # In order to be able to format for 88 char in ruff format max-line-length = 120 From d1ee6d154f0eef2c753ed6a123eec583d02ae00b Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 9 Feb 2024 11:08:33 +0200 Subject: [PATCH 2/3] Enable flake8-pie --- pyproject.toml | 1 + src/_pytest/_py/path.py | 2 +- testing/test_assertion.py | 2 +- testing/test_config.py | 6 ++---- testing/test_skipping.py | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cf3265efe..e17694723 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,6 +102,7 @@ select = [ "UP", # pyupgrade "RUF", # ruff "W", # pycodestyle + "PIE", # flake8-pie ] ignore = [ # bugbear ignore diff --git a/src/_pytest/_py/path.py b/src/_pytest/_py/path.py index 554cb4a67..7701561d9 100644 --- a/src/_pytest/_py/path.py +++ b/src/_pytest/_py/path.py @@ -452,7 +452,7 @@ class LocalPath: def ensure_dir(self, *args): """Ensure the path joined with args is a directory.""" - return self.ensure(*args, **{"dir": True}) + return self.ensure(*args, dir=True) def bestrelpath(self, dest): """Return a string which is a relative path from self diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 2d92128fb..ef4e36644 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -601,7 +601,7 @@ class TestAssert_reprcompare: def test_list_dont_wrap_strings(self) -> None: long_a = "a" * 10 - l1 = ["a"] + [long_a for _ in range(0, 7)] + l1 = ["a"] + [long_a for _ in range(7)] l2 = ["should not get wrapped"] diff = callequal(l1, l2, verbose=True) assert diff == [ diff --git a/testing/test_config.py b/testing/test_config.py index f2651dbeb..ead693cf4 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1231,8 +1231,7 @@ def test_plugin_loading_order(pytester: Pytester) -> None: import myplugin assert myplugin.terminal_plugin == [False, True] """, - **{ - "myplugin": """ + myplugin=""" terminal_plugin = [] def pytest_configure(config): @@ -1241,8 +1240,7 @@ def test_plugin_loading_order(pytester: Pytester) -> None: def pytest_sessionstart(session): config = session.config terminal_plugin.append(bool(config.pluginmanager.get_plugin("terminalreporter"))) - """ - }, + """, ) pytester.syspathinsert() result = pytester.runpytest("-p", "myplugin", str(p1)) diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 4391c0642..3f1c83f5b 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -74,7 +74,7 @@ class TestEvaluation: """@pytest.mark.skipif("not hasattr(os, 'murks')")""", """@pytest.mark.skipif(condition="hasattr(os, 'murks')")""", ] - for i in range(0, 2): + for i in range(2): item = pytester.getitem( f""" import pytest From a182e10b06e0efd91d78f33db5c7424a2afef258 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 9 Feb 2024 11:14:36 +0200 Subject: [PATCH 3/3] Enable lint PGH004 - Use specific rule codes when using noqa --- bench/bench.py | 2 +- doc/en/example/assertion/failure_demo.py | 4 ++-- pyproject.toml | 1 + src/_pytest/assertion/rewrite.py | 2 +- testing/python/fixtures.py | 2 +- testing/test_assertrewrite.py | 2 +- testing/test_collection.py | 2 +- testing/test_warnings.py | 2 +- 8 files changed, 9 insertions(+), 8 deletions(-) diff --git a/bench/bench.py b/bench/bench.py index 91e380a80..437d3259d 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -5,7 +5,7 @@ if __name__ == "__main__": import cProfile import pstats - import pytest # NOQA + import pytest # noqa: F401 script = sys.argv[1:] if len(sys.argv) > 1 else ["empty.py"] cProfile.run("pytest.cmdline.main(%r)" % script, "prof") diff --git a/doc/en/example/assertion/failure_demo.py b/doc/en/example/assertion/failure_demo.py index abb9bce50..f7a9c2794 100644 --- a/doc/en/example/assertion/failure_demo.py +++ b/doc/en/example/assertion/failure_demo.py @@ -172,7 +172,7 @@ class TestRaises: raise ValueError("demo error") def test_tupleerror(self): - a, b = [1] # NOQA + a, b = [1] # noqa: F841 def test_reinterpret_fails_with_print_for_the_fun_of_it(self): items = [1, 2, 3] @@ -180,7 +180,7 @@ class TestRaises: a, b = items.pop() def test_some_error(self): - if namenotexi: # NOQA + if namenotexi: # noqa: F821 pass def func1(self): diff --git a/pyproject.toml b/pyproject.toml index e17694723..0a83df1bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,6 +103,7 @@ select = [ "RUF", # ruff "W", # pycodestyle "PIE", # flake8-pie + "PGH004", # pygrep-hooks - Use specific rule codes when using noqa ] ignore = [ # bugbear ignore diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 9f3b65e8f..ddae34c73 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -1006,7 +1006,7 @@ class AssertionRewriter(ast.NodeVisitor): if i: fail_inner: List[ast.stmt] = [] # cond is set in a prior loop iteration below - self.expl_stmts.append(ast.If(cond, fail_inner, [])) # noqa + self.expl_stmts.append(ast.If(cond, fail_inner, [])) # noqa: F821 self.expl_stmts = fail_inner # Check if the left operand is a ast.NamedExpr and the value has already been visited if ( diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index d0e8723cb..35ed2efc5 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -712,7 +712,7 @@ class TestRequestBasic: ) def test_request_garbage(self, pytester: Pytester) -> None: try: - import xdist # noqa + import xdist # noqa: F401 except ImportError: pass else: diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 6e1a9d54e..b045b601e 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -200,7 +200,7 @@ class TestAssertionRewrite: assert getmsg(f2) == "assert False" def f3() -> None: - assert a_global # type: ignore[name-defined] # noqa + assert a_global # type: ignore[name-defined] # noqa: F821 assert getmsg(f3, {"a_global": False}) == "assert False" diff --git a/testing/test_collection.py b/testing/test_collection.py index 8e41e0fae..45a1ca9ae 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -535,7 +535,7 @@ class TestSession: newid = item.nodeid assert newid == id pprint.pprint(hookrec.calls) - topdir = pytester.path # noqa + topdir = pytester.path # noqa: F841 hookrec.assert_contains( [ ("pytest_collectstart", "collector.path == topdir"), diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 5b2f27139..3ef0cd3b5 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -794,7 +794,7 @@ def test_resource_warning(pytester: Pytester, monkeypatch: pytest.MonkeyPatch) - # available, using `importorskip("tracemalloc")` for example, # because we want to ensure the same code path does not break in those platforms. try: - import tracemalloc # noqa + import tracemalloc # noqa: F401 has_tracemalloc = True except ImportError: