From 155892114fa5bacd1f0a46c70ee5b119ee26a873 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:01:37 +0000 Subject: [PATCH 1/6] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/astral-sh/ruff-pre-commit: v0.6.9 → v0.7.0](https://github.com/astral-sh/ruff-pre-commit/compare/v0.6.9...v0.7.0) - [github.com/pre-commit/mirrors-mypy: v1.11.2 → v1.12.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.11.2...v1.12.1) - [github.com/tox-dev/pyproject-fmt: 2.3.1 → 2.4.3](https://github.com/tox-dev/pyproject-fmt/compare/2.3.1...2.4.3) --- .pre-commit-config.yaml | 11 ++++++----- pyproject.toml | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 096228acf9f..c058b6a699c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.9" + rev: "v0.7.1" hooks: - id: ruff args: ["--fix"] @@ -12,7 +12,7 @@ repos: - id: end-of-file-fixer - id: check-yaml - repo: https://github.com/adamchainz/blacken-docs - rev: 1.19.0 + rev: 1.19.1 hooks: - id: blacken-docs additional_dependencies: [black==24.1.1] @@ -28,7 +28,7 @@ repos: hooks: - id: python-use-type-annotations - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.11.2 + rev: v1.13.0 hooks: - id: mypy files: ^(src/|testing/|scripts/) @@ -44,13 +44,13 @@ repos: # on <3.11 - exceptiongroup>=1.0.0rc8 - repo: https://github.com/tox-dev/pyproject-fmt - rev: "2.3.1" + rev: "v2.4.3" hooks: - id: pyproject-fmt # https://pyproject-fmt.readthedocs.io/en/latest/#calculating-max-supported-python-version additional_dependencies: ["tox>=4.9"] - repo: https://github.com/asottile/pyupgrade - rev: v3.18.0 + rev: v3.19.0 hooks: - id: pyupgrade stages: [manual] @@ -62,6 +62,7 @@ repos: language: system types: [python] args: ["-rn", "-sn", "--fail-on=I"] + require_serial: true stages: [manual] - id: rst name: rst diff --git a/pyproject.toml b/pyproject.toml index caeb4bf7f83..0f610f5825d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -275,6 +275,7 @@ disable = [ "too-many-lines", "too-many-locals", "too-many-nested-blocks", + "too-many-positional-arguments", "too-many-public-methods", "too-many-return-statements", # disabled in ruff "too-many-statements", # disabled in ruff From e20d748c1e8f02bbbebc72dd68ed7af9d747ff3b Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 22 Oct 2024 19:50:42 +0200 Subject: [PATCH 2/6] [mypy] Remove useless noqa, add noqa for new false positives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) --- src/_pytest/_io/pprint.py | 6 +++--- src/_pytest/capture.py | 6 ++++-- testing/test_runner.py | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/_pytest/_io/pprint.py b/src/_pytest/_io/pprint.py index fc29989be0b..7a6433d9128 100644 --- a/src/_pytest/_io/pprint.py +++ b/src/_pytest/_io/pprint.py @@ -111,15 +111,15 @@ def _format( p(self, object, stream, indent, allowance, context, level + 1) context.remove(objid) elif ( - _dataclasses.is_dataclass(object) # type:ignore[unreachable] + _dataclasses.is_dataclass(object) and not isinstance(object, type) - and object.__dataclass_params__.repr + and object.__dataclass_params__.repr # type:ignore[attr-defined] and # Check dataclass has generated repr method. hasattr(object.__repr__, "__wrapped__") and "__create_fn__" in object.__repr__.__wrapped__.__qualname__ ): - context.add(objid) # type:ignore[unreachable] + context.add(objid) self._pprint_dataclass( object, stream, indent, allowance, context, level + 1 ) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 506c0b3d287..2ac3b6bbc7f 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -15,6 +15,7 @@ from typing import Any from typing import AnyStr from typing import BinaryIO +from typing import cast from typing import Final from typing import final from typing import Generator @@ -177,7 +178,8 @@ def name(self) -> str: def mode(self) -> str: # TextIOWrapper doesn't expose a mode, but at least some of our # tests check it. - return self.buffer.mode.replace("b", "") + assert hasattr(self.buffer, "mode") + return cast(str, self.buffer.mode.replace("b", "")) class CaptureIO(io.TextIOWrapper): @@ -550,7 +552,7 @@ def snap(self) -> bytes: res = self.tmpfile.buffer.read() self.tmpfile.seek(0) self.tmpfile.truncate() - return res + return res # type: ignore[return-value] def writeorg(self, data: bytes) -> None: """Write to original file descriptor.""" diff --git a/testing/test_runner.py b/testing/test_runner.py index 0d9facdcd71..0245438a47d 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -137,8 +137,8 @@ def raiser(exc): ss.teardown_exact(None) mod, func = e.value.exceptions assert isinstance(mod, KeyError) - assert isinstance(func.exceptions[0], TypeError) # type: ignore - assert isinstance(func.exceptions[1], ValueError) # type: ignore + assert isinstance(func.exceptions[0], TypeError) + assert isinstance(func.exceptions[1], ValueError) def test_cached_exception_doesnt_get_longer(self, pytester: Pytester) -> None: """Regression test for #12204 (the "BTW" case).""" From d92ad29cdc08a9e358772f5190830bbe4450a360 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 29 Oct 2024 10:08:27 +0100 Subject: [PATCH 3/6] Upgrade pylint version, activate all extensions --- .pre-commit-config.yaml | 2 +- pyproject.toml | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c058b6a699c..f5feb4ef112 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,7 +61,7 @@ repos: entry: pylint language: system types: [python] - args: ["-rn", "-sn", "--fail-on=I"] + args: ["-rn", "-sn", "--fail-on=I", "--enable-all-extentions"] require_serial: true stages: [manual] - id: rst diff --git a/pyproject.toml b/pyproject.toml index 0f610f5825d..8ef320e0ca0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -200,7 +200,9 @@ disable = [ "arguments-renamed", "assigning-non-slot", "attribute-defined-outside-init", + "bad-builtin", "bad-classmethod-argument", + "bad-dunder-name", "bad-mcs-method-argument", "broad-exception-caught", "broad-exception-raised", @@ -209,25 +211,41 @@ disable = [ "comparison-with-callable", "comparison-with-itself", # PLR0124 from ruff "condition-evals-to-constant", + "consider-alternative-union-syntax", + "confusing-consecutive-elif", + "consider-using-any-or-all", + "consider-using-assignment-expr", "consider-using-dict-items", "consider-using-from-import", "consider-using-f-string", "consider-using-in", + "consider-using-namedtuple-or-dataclass", "consider-using-ternary", + "consider-using-tuple", "consider-using-with", "consider-using-from-import", # not activated by default, PLR0402 disabled in ruff + "consider-ternary-expression", "cyclic-import", + "differing-param-doc", + "docstring-first-line-empty", + "deprecated-argument", + "deprecated-attribute", + "deprecated-class", + "deprecated-typing-alias", + "dict-init-mutate", "disallowed-name", # foo / bar are used often in tests "duplicate-code", "else-if-used", # not activated by default, PLR5501 disabled in ruff "empty-comment", # not activated by default, PLR2044 disabled in ruff "eval-used", + "eq-without-hash", "exec-used", "expression-not-assigned", "fixme", "global-statement", # PLW0603 disabled in ruff "import-error", "import-outside-toplevel", + "import-private-name", "inconsistent-return-statements", "invalid-bool-returned", "invalid-name", @@ -238,8 +256,12 @@ disable = [ "magic-value-comparison", # not activated by default, PLR2004 disabled in ruff "method-hidden", "missing-docstring", + "missing-param-doc", + "missing-raises-doc", "missing-timeout", + "missing-type-doc", "misplaced-bare-raise", # PLE0704 from ruff + "misplaced-comparison-constant", "multiple-statements", # multiple-statements-on-one-line-colon (E701) from ruff "no-else-break", "no-else-continue", @@ -248,6 +270,7 @@ disable = [ "no-member", "no-name-in-module", "no-self-argument", + "no-self-use", "not-an-iterable", "not-callable", "pointless-exception-statement", # https://github.com/pytest-dev/pytest/pull/12379 @@ -260,12 +283,14 @@ disable = [ "redefined-builtin", "redefined-loop-name", # PLW2901 disabled in ruff "redefined-outer-name", + "redefined-variable-type", "reimported", "simplifiable-condition", "simplifiable-if-expression", "singleton-comparison", "superfluous-parens", "super-init-not-called", + "too-complex", "too-few-public-methods", "too-many-ancestors", "too-many-arguments", # disabled in ruff @@ -279,6 +304,7 @@ disable = [ "too-many-public-methods", "too-many-return-statements", # disabled in ruff "too-many-statements", # disabled in ruff + "too-many-try-statements", "try-except-raise", "typevar-name-incorrect-variance", # PLC0105 disabled in ruff "unbalanced-tuple-unpacking", @@ -300,10 +326,12 @@ disable = [ "use-dict-literal", "use-implicit-booleaness-not-comparison", "use-implicit-booleaness-not-len", + "use-set-for-membership", "useless-else-on-loop", # PLC0414 disabled in ruff "useless-import-alias", "useless-return", "using-constant-test", + "while-used", "wrong-import-order", # handled by isort / ruff "wrong-import-position", # handled by isort / ruff ] From 05b72ee0f7b97e3f2cd2688844b0dbfae238b256 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 29 Oct 2024 10:09:09 +0100 Subject: [PATCH 4/6] [pylint dict-init-mutate] Initialize a dict right off for speed --- pyproject.toml | 1 - src/_pytest/junitxml.py | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8ef320e0ca0..ac00977e497 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -232,7 +232,6 @@ disable = [ "deprecated-attribute", "deprecated-class", "deprecated-typing-alias", - "dict-init-mutate", "disallowed-name", # foo / bar are used often in tests "duplicate-code", "else-if-used", # not activated by default, PLR5501 disabled in ruff diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 3a2cb59a6c1..efe6f489b48 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -74,10 +74,10 @@ def merge_family(left, right) -> None: left.update(result) -families = {} -families["_base"] = {"testcase": ["classname", "name"]} -families["_base_legacy"] = {"testcase": ["file", "line", "url"]} - +families = { # pylint: disable=dict-init-mutate + "_base": {"testcase": ["classname", "name"]}, + "_base_legacy": {"testcase": ["file", "line", "url"]}, +} # xUnit 1.x inherits legacy attributes. families["xunit1"] = families["_base"].copy() merge_family(families["xunit1"], families["_base_legacy"]) From 27b1dd68fb35f4d9ee7899bc41061a707a1ab976 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 29 Oct 2024 12:45:54 +0100 Subject: [PATCH 5/6] [pylint consider-using-any-or-all] Avoid constructing a list of names --- src/_pytest/mark/__init__.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/_pytest/mark/__init__.py b/src/_pytest/mark/__init__.py index f76e5212057..99ad3cfcee4 100644 --- a/src/_pytest/mark/__init__.py +++ b/src/_pytest/mark/__init__.py @@ -193,12 +193,7 @@ def __call__(self, subname: str, /, **kwargs: str | int | bool | None) -> bool: if kwargs: raise UsageError("Keyword expressions do not support call parameters.") subname = subname.lower() - names = (name.lower() for name in self._names) - - for name in names: - if subname in name: - return True - return False + return any(subname in name.lower() for name in self._names) def deselect_by_keyword(items: list[Item], config: Config) -> None: From 602addece814d75a2db75e5441bce59a542751e1 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 29 Oct 2024 14:04:51 +0100 Subject: [PATCH 6/6] [pylint consider-using-any-or-all] noqa a a suggestion in already nested for --- pyproject.toml | 1 - src/_pytest/mark/__init__.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ac00977e497..c13c8a82849 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -213,7 +213,6 @@ disable = [ "condition-evals-to-constant", "consider-alternative-union-syntax", "confusing-consecutive-elif", - "consider-using-any-or-all", "consider-using-assignment-expr", "consider-using-dict-items", "consider-using-from-import", diff --git a/src/_pytest/mark/__init__.py b/src/_pytest/mark/__init__.py index 99ad3cfcee4..a6f0155751a 100644 --- a/src/_pytest/mark/__init__.py +++ b/src/_pytest/mark/__init__.py @@ -238,10 +238,9 @@ def __call__(self, name: str, /, **kwargs: str | int | bool | None) -> bool: if not (matches := self.own_mark_name_mapping.get(name, [])): return False - for mark in matches: + for mark in matches: # pylint: disable=consider-using-any-or-all if all(mark.kwargs.get(k, NOT_SET) == v for k, v in kwargs.items()): return True - return False