From b9fe59736d1c272b2e722a352f281d6686cc40ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 30 Dec 2021 22:11:30 +0100 Subject: Allow passing arguments when entry points are used as functions (#5613) --- ChangeLog | 5 +++++ doc/user_guide/run.rst | 14 ++++++++++++++ doc/whatsnew/2.13.rst | 5 +++++ pylint/__init__.py | 27 +++++++++++++++++++-------- tests/test_pylint_runners.py | 11 +++++++++++ 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1e8d79d43..3dc350fb2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -90,6 +90,11 @@ Release date: TBA Closes #5504 +* When invoking ``pylint``, ``symilar`` or ``pyreverse`` by importing them in a python file + you can now pass an ``arguments`` keyword besides patching ``sys.argv``. + + Closes #5320 + * The ``PyLinter`` class will now be initialized with a ``TextReporter`` as its reporter if none is provided. diff --git a/doc/user_guide/run.rst b/doc/user_guide/run.rst index 5f280a4e7..de28a4413 100644 --- a/doc/user_guide/run.rst +++ b/doc/user_guide/run.rst @@ -45,6 +45,20 @@ thanks to the ``Run()`` function in the ``pylint.lint`` module pylint_opts = ['--disable=line-too-long', 'myfile.py'] pylint.lint.Run(pylint_opts) +Another option would be to use the ``run_pylint`` function, which is the same function +called by the command line. You can either patch ``sys.argv`` or supply arguments yourself: + +.. sourcecode:: python + + import pylint + + sys.argv = ["pylint", "your_file"] + pylint.run_pylint() + + # Or: + + pylint.run_pylint(arguments=["your_file"]) + To silently run Pylint on a ``module_name.py`` module, and get its standard output and error: diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index df1a99757..5b16243cf 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -121,6 +121,11 @@ Other Changes Closes #5557 +* When invoking ``pylint``, ``symilar`` or ``pyreverse`` by importing them in a python file + you can now pass an ``arguments`` keyword besides patching ``sys.argv``. + + Closes #5320 + * The ``PyLinter`` class will now be initialized with a ``TextReporter`` as its reporter if none is provided. diff --git a/pylint/__init__.py b/pylint/__init__.py index ddf794f86..53ea308bd 100644 --- a/pylint/__init__.py +++ b/pylint/__init__.py @@ -11,17 +11,22 @@ import os import sys +from typing import List, Optional from pylint.__pkginfo__ import __version__ # pylint: disable=import-outside-toplevel -def run_pylint(): +def run_pylint(*, arguments: Optional[List[str]] = None): + """Run pylint + + Arguments can be a list of strings normally supplied as arguments on the command line + """ from pylint.lint import Run as PylintRun try: - PylintRun(sys.argv[1:]) + PylintRun(arguments or sys.argv[1:]) except KeyboardInterrupt: sys.exit(1) @@ -32,18 +37,24 @@ def run_epylint(): EpylintRun() -def run_pyreverse(): - """run pyreverse""" +def run_pyreverse(*, arguments: Optional[List[str]] = None): + """Run pyreverse + + Arguments can be a list of strings normally supplied as arguments on the command line + """ from pylint.pyreverse.main import Run as PyreverseRun - PyreverseRun(sys.argv[1:]) + PyreverseRun(arguments or sys.argv[1:]) + +def run_symilar(*, arguments: Optional[List[str]] = None): + """Run symilar -def run_symilar(): - """run symilar""" + Arguments can be a list of strings normally supplied as arguments on the command line + """ from pylint.checkers.similar import Run as SimilarRun - SimilarRun(sys.argv[1:]) + SimilarRun(arguments or sys.argv[1:]) def modify_sys_path() -> None: diff --git a/tests/test_pylint_runners.py b/tests/test_pylint_runners.py index 6c4430892..a60a3c825 100644 --- a/tests/test_pylint_runners.py +++ b/tests/test_pylint_runners.py @@ -21,3 +21,14 @@ def test_runner(runner: Callable, tmpdir: LocalPath) -> None: with pytest.raises(SystemExit) as err: runner() assert err.value.code == 0 + + +@pytest.mark.parametrize("runner", [run_pylint, run_pyreverse, run_symilar]) +def test_runner_with_arguments(runner: Callable, tmpdir: LocalPath) -> None: + """Check the runners with arguments as parameter instead of sys.argv""" + filepath = os.path.abspath(__file__) + testargs = [filepath] + with tmpdir.as_cwd(): + with pytest.raises(SystemExit) as err: + runner(arguments=testargs) + assert err.value.code == 0 -- cgit v1.2.1