hi everyone! I am struggling to run my custom pyth...
# general
n
hi everyone! I am struggling to run my custom python rule (to check python classes are "simple") by running pants -> I am seeing this:
Copy code
pants.base.exceptions.BackendConfigurationError: Failed to load the gp_checks.register backend: ModuleNotFoundError("No module named 'my_checker.register'")
After adding my new rule's module to
backend_packages
inside
pants.toml
. I went through the documentation on how to define a new pylint checker and have add a
register
function:
Copy code
def register(linter: "PyLinter") -> None:
    """
    This required method auto registers the checker during initialization.

    :param linter: The linter to register the checker to.
    """
    linter.register_checker(MyChecker(linter))
My
MyChecker
roughly looks like:
Copy code
from pylint import checkers


class MyChecker(checkers.BaseChecker):
    ...
Now, I've seen
@rule
too, but I am unsure how to proceed -> what should I put in the
my_checker/register.py
such that my new checker can be run?
đź‘‹ 1
c
n
thanks! I actually got a bit confused here, as I have a pylint checker... but how does this become a pants rule?
MyChecker
->
Rule
?
c
the code snippets seem to be pylint specific.. I don’t know about those…
there already is a pylint backend in pants, so if you want to register a custom pylint plugin, I don’t know how that works. https://github.com/pantsbuild/pants/tree/86628a1d9c74da84992aa9b6be8a843dcdc2b808/src/python/pants/backend/python/lint/pylint
i.e. it doesn’t make sense to register your pylint plugin with pants, it should be registered with pylint, somehow…
or perhaps more relevantly using in-repo plugin, but for flake8: https://github.com/pantsbuild/pants/blob/86628a1d9c74da84992aa9b6be8a843dcdc2b808/pants.toml#L165 just showing that it will be a tool specific configuration for it to pick up your plugin.
n
okay so maybe I just register the pylint plugin with pylint itself?
👆 1
how would I turn it on for only 1 module e.g. using pants config?
(thanks btw!)
c
not sure it would be possible to selectively enable this for a single module. Perhaps you can have some conditional logic in the plugin itself if it only should apply to a specific set of modules..?
n
yea that's also def. possible, thanks!
👍 2
So, I have now written a working plugin for pylint... But when I want to be able to run it with pants, after generating the lock file I get this error:
Copy code
./pants lint app::

[...]

stderr:
A distribution for babel could not be resolved for /home/admin/.pyenv/versions/3.10.5/bin/python3.10.

09:20:53.41 [INFO] Canceled: Building 2 requirements for pylint.pex from the tools/python/pylint/pylint-resolve.lock resolve: astroid, pylint<3,>=2.13.0
I am unsure how to continue now... I added this to my `pants.toml`:
Copy code
[pylint]
args = [
    "--disable=all",
    "--enable=gp-check-simple-dtos",
]
config = "pyproject.toml"
lockfile = "tools/python/pylint/pylint-resolve.lock"
install_from_resolve = "pylint"
source_plugins = [
  "pylint_plugins:pylint-plugins-sources",
]
version = "pylint>=2.13.0,<2.15"

[source]
root_patterns = [
  ...
  "/pylint_plugins",
]

[python]
...
enable_resolves = true


[python.resolves.add]
...
pylint = "tools/python/pylint/pylint-resolve.lock"

backend_packages = [
   ...
  "pants.backend.python.lint.pylint",
]
And `pyproject.toml`:
Copy code
[tool.pylint]
load-plugins = [
    "check_simple_dtos",
]
This is how my
pylint_plugins/BUILD
looks like:
Copy code
python_requirement(
    requirements=["astroid", "pylint>=2.13.0,<3"],
    resolve="pylint",
)

python_sources(
    name="pylint-plugins-sources",
    sources=["**/*.py", "!**/test_*.py"],
    resolve="pylint",
)
I ran
pants generate-lockfiles --resolve=pylint
to create the lock file in
tools/python/pylint/pylint-resolve.lock
. Any help would be very welcome! Also, as a side note, I got some deprecated warnings about using
version
and
lockfile
and that I should use
install_from_resolve
instead. But, if I leave out
lockfile
then it complains that no
lockfile
is found when running the
generate-lockfiles --resolve=pylint
command.