silly-queen-7197
05/02/2024, 6:48 PMcode_quality_tool
I'm trying
code_quality_tool(
name="doctest",
runnable="//:3rdparty/python:reqs-dev#pytest",
args=["--doctest-modules"],
file_glob_include=["**/*.py"],
)
def add(a, b):
"""
>>> add(1, 2)
4
"""
return a + b
and expecting pants lint demo/:doctest
to fail but it succeeds. What am I doing wrong here?silly-queen-7197
05/02/2024, 6:55 PMpytest --doctest-modules demo/
works as expectedsilly-queen-7197
05/02/2024, 6:56 PMdoctest
is part of the python standard library so I don't think I need to include any other dependenciessquare-psychiatrist-19087
05/02/2024, 7:18 PM:doctest
is the target of the tool. You don't need to lint the tool itself
2) lint
usually runs a linter per file, but doctest might import other modules, so you might need check
, but I'm not suresilly-queen-7197
05/02/2024, 8:22 PMpants check demo/::
20:22:10.18 [ERROR] No relevant backends activate the `check` goal, so the goal would do nothing.
silly-queen-7197
05/02/2024, 8:22 PMpants lint demo/::
also doesn't return anythingsilly-queen-7197
05/02/2024, 8:35 PMpants run 3rdparty/python:reqs-dev#pytest -- --doctest-modules demo/
works as expectedsilly-queen-7197
05/02/2024, 8:36 PMcode_quality_tool
runnable or args it doesn't complain. Just returns 0 silentlysilly-queen-7197
05/02/2024, 8:39 PM--keep-sandboxes=always
sometimes not do anything?
Maybe it's not creating a sandbox? In which case how do I inspect what pants is actually doingsquare-psychiatrist-19087
05/02/2024, 8:42 PMsilly-queen-7197
05/02/2024, 8:43 PMpants.backend.experimental.adhoc
to my backend_packages in pants.toml?silly-queen-7197
05/02/2024, 8:51 PMsilly-queen-7197
05/02/2024, 8:52 PMsilly-queen-7197
05/02/2024, 8:52 PMpants lint ::
to fail but it doesn't.square-psychiatrist-19087
05/02/2024, 9:01 PMpants.backend.experimental.adhoc
to my backend_packages in pants.toml?
No, this will only add the target code_quality_tool
, but then you need to actually configure and register a new mini pluginsquare-psychiatrist-19087
05/02/2024, 9:01 PMsquare-psychiatrist-19087
05/02/2024, 9:01 PMsilly-queen-7197
05/02/2024, 9:05 PMcfg = CodeQualityToolRuleBuilder(
goal="lint", target="build-support:no_badcode_tool", name="No Bad Code", scope="nobadcode"
)
in the testsilly-queen-7197
05/02/2024, 9:06 PMsilly-queen-7197
05/02/2024, 9:07 PMadhoc_tool
I didn't need to do anything like that. (I suppose that was runnable though)square-psychiatrist-19087
05/02/2024, 9:14 PMadhoc_tool
is a single target with concrete graph rules, inputs and outputs, but code_quality_tool
is supposed to be run over other targets and it needs to be added to lint
goal, which means adding a new rule to the computation graph and pants doesn't support adding rules to computation graph from BUILD
files. At least, this is how I understand itsilly-queen-7197
05/02/2024, 9:26 PMsilly-queen-7197
05/02/2024, 9:41 PM╰❱ pants lint ::
14:41:26.53 [INFO] Completed: Running code quality tool
14:41:26.53 [ERROR] Completed: pants.backend.adhoc.code_quality_tool.CodeQualityToolRuleBuilder._build_lint_rules.run_code_quality_doctest - doctest failed (exit code 1).
============================= test session starts ==============================
platform darwin -- Python 3.10.13, pytest-8.2.0, pluggy-1.5.0
rootdir: /private/var/folders/m8/cmsvrd417n39w5xjzjhmnznm0000gq/T/pants-sandbox-9DIFNd
collected 1 item
example.py F [100%]
=================================== FAILURES ===================================
____________________________ [doctest] example.add _____________________________
002
003 Add two numbers together
004
005 >>> add(1, 2)
Expected:
4
Got:
3
example.py:5: DocTestFailure
=========================== short test summary info ============================
FAILED example.py::example.add
============================== 1 failed in 0.01s ===============================
yaysilly-queen-7197
05/02/2024, 9:56 PM╰❱ pants lint ::
14:55:43.83 [INFO] Initializing scheduler...
14:55:47.30 [INFO] Scheduler initialized.
14:55:48.82 [INFO] Completed: Running code quality tool
14:55:48.82 [ERROR] Completed: pants.backend.adhoc.code_quality_tool.CodeQualityToolRuleBuilder._build_lint_rules.run_code_quality_doctest - doctest failed (exit code 2).
============================= test session starts ==============================
platform darwin -- Python 3.10.13, pytest-8.2.0, pluggy-1.5.0
rootdir: /private/var/folders/m8/cmsvrd417n39w5xjzjhmnznm0000gq/T/pants-sandbox-WhRI4d
collected 0 items / 2 errors
==================================== ERRORS ====================================
___________________ ERROR collecting proj1/src/proj1/foo.py ____________________
ImportError while importing test module 'proj1/src/proj1/foo.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
proj1/src/proj1/foo.py:1: in <module>
from proj2.bar import message
E ModuleNotFoundError: No module named 'proj2'
___________________ ERROR collecting proj1/src/proj1/foo.py ____________________
ImportError while importing test module 'proj1/src/proj1/foo.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.10/3.10.13/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
proj1/src/proj1/foo.py:1: in <module>
from proj2.bar import message
E ModuleNotFoundError: No module named 'proj2'
=========================== short test summary info ============================
ERROR proj1/src/proj1/foo.py
ERROR proj1/src/proj1/foo.py
!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!
============================== 2 errors in 0.05s ===============================
silly-queen-7197
05/02/2024, 9:57 PMsquare-psychiatrist-19087
05/02/2024, 10:27 PM2)usually runs a linter per file, but doctest might import other modules, so you might needlint
, but I'm not surecheck
square-psychiatrist-19087
05/02/2024, 10:29 PMThe docs for https://www.pantsbuild.org/2.20/reference/targets/code_quality_tool make it looks like a regular ol targetyeah, that's a hot new feature in 2.20 and it doesn't have a dedicated docs page yet
silly-queen-7197
05/02/2024, 11:29 PMcheck
and test
.square-psychiatrist-19087
05/03/2024, 10:28 AMpython_test
like this:
def test_docs() -> None:
failure_count, test_count = doctest.testmod(my_module)
assert test_count > 0
assert failure_count == 0
square-psychiatrist-19087
05/03/2024, 10:29 AMsilly-queen-7197
05/03/2024, 11:31 PM