Skip to content

Commit b9fe597

Browse files
authored
Allow passing arguments when entry points are used as functions (#5613)
1 parent 319ba11 commit b9fe597

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ Release date: TBA
9090

9191
Closes #5504
9292

93+
* When invoking ``pylint``, ``symilar`` or ``pyreverse`` by importing them in a python file
94+
you can now pass an ``arguments`` keyword besides patching ``sys.argv``.
95+
96+
Closes #5320
97+
9398
* The ``PyLinter`` class will now be initialized with a ``TextReporter``
9499
as its reporter if none is provided.
95100

doc/user_guide/run.rst

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ thanks to the ``Run()`` function in the ``pylint.lint`` module
4545
pylint_opts = ['--disable=line-too-long', 'myfile.py']
4646
pylint.lint.Run(pylint_opts)
4747

48+
Another option would be to use the ``run_pylint`` function, which is the same function
49+
called by the command line. You can either patch ``sys.argv`` or supply arguments yourself:
50+
51+
.. sourcecode:: python
52+
53+
import pylint
54+
55+
sys.argv = ["pylint", "your_file"]
56+
pylint.run_pylint()
57+
58+
# Or:
59+
60+
pylint.run_pylint(arguments=["your_file"])
61+
4862
To silently run Pylint on a ``module_name.py`` module,
4963
and get its standard output and error:
5064

doc/whatsnew/2.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ Other Changes
121121

122122
Closes #5557
123123

124+
* When invoking ``pylint``, ``symilar`` or ``pyreverse`` by importing them in a python file
125+
you can now pass an ``arguments`` keyword besides patching ``sys.argv``.
126+
127+
Closes #5320
128+
124129
* The ``PyLinter`` class will now be initialized with a ``TextReporter``
125130
as its reporter if none is provided.
126131

pylint/__init__.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@
1111

1212
import os
1313
import sys
14+
from typing import List, Optional
1415

1516
from pylint.__pkginfo__ import __version__
1617

1718
# pylint: disable=import-outside-toplevel
1819

1920

20-
def run_pylint():
21+
def run_pylint(*, arguments: Optional[List[str]] = None):
22+
"""Run pylint
23+
24+
Arguments can be a list of strings normally supplied as arguments on the command line
25+
"""
2126
from pylint.lint import Run as PylintRun
2227

2328
try:
24-
PylintRun(sys.argv[1:])
29+
PylintRun(arguments or sys.argv[1:])
2530
except KeyboardInterrupt:
2631
sys.exit(1)
2732

@@ -32,18 +37,24 @@ def run_epylint():
3237
EpylintRun()
3338

3439

35-
def run_pyreverse():
36-
"""run pyreverse"""
40+
def run_pyreverse(*, arguments: Optional[List[str]] = None):
41+
"""Run pyreverse
42+
43+
Arguments can be a list of strings normally supplied as arguments on the command line
44+
"""
3745
from pylint.pyreverse.main import Run as PyreverseRun
3846

39-
PyreverseRun(sys.argv[1:])
47+
PyreverseRun(arguments or sys.argv[1:])
48+
4049

50+
def run_symilar(*, arguments: Optional[List[str]] = None):
51+
"""Run symilar
4152
42-
def run_symilar():
43-
"""run symilar"""
53+
Arguments can be a list of strings normally supplied as arguments on the command line
54+
"""
4455
from pylint.checkers.similar import Run as SimilarRun
4556

46-
SimilarRun(sys.argv[1:])
57+
SimilarRun(arguments or sys.argv[1:])
4758

4859

4960
def modify_sys_path() -> None:

tests/test_pylint_runners.py

+11
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ def test_runner(runner: Callable, tmpdir: LocalPath) -> None:
2121
with pytest.raises(SystemExit) as err:
2222
runner()
2323
assert err.value.code == 0
24+
25+
26+
@pytest.mark.parametrize("runner", [run_pylint, run_pyreverse, run_symilar])
27+
def test_runner_with_arguments(runner: Callable, tmpdir: LocalPath) -> None:
28+
"""Check the runners with arguments as parameter instead of sys.argv"""
29+
filepath = os.path.abspath(__file__)
30+
testargs = [filepath]
31+
with tmpdir.as_cwd():
32+
with pytest.raises(SystemExit) as err:
33+
runner(arguments=testargs)
34+
assert err.value.code == 0

0 commit comments

Comments
 (0)