Skip to content

Commit eb95e06

Browse files
mgornyarthurzam
authored andcommitted
PythonCheck: check for redundant PYTEST_DISABLE_PLUGIN_AUTOLOAD
Signed-off-by: Michał Górny <mgorny@gentoo.org> Closes: #753 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent 3462a81 commit eb95e06

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

src/pkgcheck/checks/python.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ def desc(self):
279279
)
280280

281281

282+
class RedundantPyTestDisablePluginAutoload(results.LineResult, results.Warning):
283+
"""Redundant ``PYTEST_DISABLE_PLUGIN_AUTOLOAD``
284+
285+
The package uses ``EPYTEST_PLUGINS`` to disable plugin autoloading already,
286+
so ``PYTEST_DISABLE_PLUGIN_AUTOLOAD`` is redundant.
287+
"""
288+
289+
@property
290+
def desc(self):
291+
return (
292+
f"line {self.lineno}: PYTEST_DISABLE_PLUGIN_AUTOLOAD is redundant, "
293+
"autoloading disabled via EPYTEST_PLUGINS already"
294+
)
295+
296+
282297
class PythonCheck(Check):
283298
"""Python eclass checks.
284299
@@ -302,6 +317,7 @@ class PythonCheck(Check):
302317
PythonMissingSCMDependency,
303318
MisplacedEPyTestVar,
304319
ShadowedEPyTestTimeout,
320+
RedundantPyTestDisablePluginAutoload,
305321
}
306322
)
307323

@@ -472,12 +488,34 @@ def check_epytest_vars(self, pkg):
472488
line = pkg.node_str(var_node)
473489
yield MisplacedEPyTestVar(var_name, line=line, lineno=lineno + 1, pkg=pkg)
474490

491+
have_epytest_plugins = False
492+
have_epytest_plugin_autoload = False
493+
found_pytest_disable_plugin_autoload = []
494+
475495
for var_node in bash.var_assign_query.captures(pkg.tree.root_node).get("assign", ()):
476496
var_name = pkg.node_str(var_node.child_by_field_name("name"))
477497
if var_name == "EPYTEST_TIMEOUT":
478498
lineno, _ = var_node.start_point
479499
line = pkg.node_str(var_node)
480500
yield ShadowedEPyTestTimeout(line=line, lineno=lineno + 1, pkg=pkg)
501+
elif var_name == "EPYTEST_PLUGINS":
502+
have_epytest_plugins = True
503+
elif var_name == "EPYTEST_PLUGIN_AUTOLOAD":
504+
if value_node := var_node.child_by_field_name("value"):
505+
value = pkg.node_str(value_node)
506+
if value not in ('""', "''"):
507+
have_epytest_plugin_autoload = True
508+
elif var_name == "PYTEST_DISABLE_PLUGIN_AUTOLOAD":
509+
lineno, _ = var_node.start_point
510+
line = pkg.node_str(var_node)
511+
found_pytest_disable_plugin_autoload.append((line, lineno))
512+
513+
# EAPI 9+ defaults to disabled autoloading, in earlier EAPIs EPYTEST_PLUGINS does that.
514+
if (
515+
str(pkg.eapi) not in ("7", "8") or have_epytest_plugins
516+
) and not have_epytest_plugin_autoload:
517+
for line, lineno in found_pytest_disable_plugin_autoload:
518+
yield RedundantPyTestDisablePluginAutoload(line=line, lineno=lineno + 1, pkg=pkg)
481519

482520
@staticmethod
483521
def _prepare_deps(deps: str):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"__class__": "RedundantPyTestDisablePluginAutoload", "category": "PythonCheck", "package": "RedundantPyTestDisablePluginAutoload", "version": "0", "line": "PYTEST_DISABLE_PLUGIN_AUTOLOAD=1", "lineno": 18}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff '--color=auto' -Naur python/PythonCheck/RedundantPyTestDisablePluginAutoload/RedundantPyTestDisablePluginAutoload-0.ebuild fixed/PythonCheck/RedundantPyTestDisablePluginAutoload/RedundantPyTestDisablePluginAutoload-0.ebuild
2+
--- python/PythonCheck/RedundantPyTestDisablePluginAutoload/RedundantPyTestDisablePluginAutoload-0.ebuild 2025-07-26 07:27:39.236976885 +0200
3+
+++ fixed/PythonCheck/RedundantPyTestDisablePluginAutoload/RedundantPyTestDisablePluginAutoload-0.ebuild 2025-07-26 07:32:32.758757644 +0200
4+
@@ -13,8 +13,3 @@
5+
EPYTEST_PLUGINS=()
6+
7+
distutils_enable_tests pytest
8+
-
9+
-python_test() {
10+
- local -x PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
11+
- epytest
12+
-}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
EAPI=8
2+
3+
DISTUTILS_USE_PEP517=flit
4+
PYTHON_COMPAT=( python3_10 )
5+
6+
inherit distutils-r1
7+
8+
DESCRIPTION="Ebuild with redundant disable-autoload"
9+
HOMEPAGE="https://github.com/pkgcore/pkgcheck"
10+
LICENSE="BSD"
11+
SLOT="0"
12+
13+
EPYTEST_PLUGINS=()
14+
15+
distutils_enable_tests pytest
16+
17+
python_test() {
18+
local -x PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
19+
epytest
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
EAPI=8
2+
3+
DISTUTILS_USE_PEP517=flit
4+
PYTHON_COMPAT=( python3_10 )
5+
6+
inherit distutils-r1
7+
8+
DESCRIPTION="Ebuild with non-redundant disable-autoload"
9+
HOMEPAGE="https://github.com/pkgcore/pkgcheck"
10+
LICENSE="BSD"
11+
SLOT="0"
12+
13+
EPYTEST_PLUGINS=()
14+
EPYTEST_PLUGIN_AUTOLOAD=1
15+
16+
distutils_enable_tests pytest
17+
18+
python_test() {
19+
epytest test_one.py
20+
21+
local -x PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
22+
epytest test_two.py
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
EAPI=8
2+
3+
DISTUTILS_USE_PEP517=flit
4+
PYTHON_COMPAT=( python3_10 )
5+
6+
inherit distutils-r1
7+
8+
DESCRIPTION="Ebuild reenabling autoload"
9+
HOMEPAGE="https://github.com/pkgcore/pkgcheck"
10+
LICENSE="BSD"
11+
SLOT="0"
12+
13+
EPYTEST_PLUGINS=()
14+
15+
distutils_enable_tests pytest
16+
17+
python_test() {
18+
epytest test_one.py
19+
20+
unset PYTEST_DISABLE_PLUGIN_AUTOLOAD
21+
epytest test_two.py
22+
}

0 commit comments

Comments
 (0)