Method: pylint.epylint.lint
Calls: 2, Exceptions: 0, Paths: 1Back
Path 1: 2 calls (1.0)
'/Users/andrehora/Documents/git/projects-pathspotter/pylint/tests/test_pylint_runners.py' (2)
[] (2)
1def lint(filename: str, options: Sequence[str] = ()) -> int:
2 """Pylint the given file.
3
4 When run from Emacs we will be in the directory of a file, and passed its
5 filename. If this file is part of a package and is trying to import other
6 modules from within its own package or another package rooted in a directory
7 below it, pylint will classify it as a failed import.
8
9 To get around this, we traverse down the directory tree to find the root of
10 the package this module is in. We then invoke pylint from this directory.
11
12 Finally, we must correct the filenames in the output generated by pylint so
13 Emacs doesn't become confused (it will expect just the original filename,
14 while pylint may extend it with extra directories if we've traversed down
15 the tree)
16 """
17 # traverse downwards until we are out of a python package
18 full_path = os.path.abspath(filename)
19 parent_path = os.path.dirname(full_path)
20 child_path = os.path.basename(full_path)
21
22 while parent_path != "/" and os.path.exists(
23 os.path.join(parent_path, "__init__.py")
24 ):
25 child_path = os.path.join(os.path.basename(parent_path), child_path)
26 parent_path = os.path.dirname(parent_path)
27
28 # Start pylint
29 # Ensure we use the python and pylint associated with the running epylint
30 run_cmd = "import sys; from pylint.lint import Run; Run(sys.argv[1:])"
31 cmd = (
32 [sys.executable, "-c", run_cmd]
33 + [
34 "--msg-template",
35 "{path}:{line}: {category} ({msg_id}, {symbol}, {obj}) {msg}",
36 "-r",
37 "n",
38 child_path,
39 ]
40 + list(options)
41 )
42
43 with Popen(
44 cmd, stdout=PIPE, cwd=parent_path, env=_get_env(), universal_newlines=True
45 ) as process:
46 for line in process.stdout: # type: ignore[union-attr]
47 # remove pylintrc warning
48 if line.startswith("No config file found"):
49 continue
50
51 # modify the file name that's put out to reverse the path traversal we made
52 parts = line.split(":")
53 if parts and parts[0] == child_path:
54 line = ":".join([filename] + parts[1:])
55 print(line, end=" ")
56
57 process.wait()
58 return process.returncode