Skip to content

Commit 4d332c5

Browse files
maksbotanmsullivan
authored andcommitted
Add option to print absolute file paths (#7754)
It is a common use case to integrate mypy into an IDE or other environment where it's run automatically and its output is processed by other programs instead of being read by a human. IDEs usually are able to parse paths, lines and messages printed by mypy and show those messages directly in their code editor right under affected lines. However, some IDEs, like PyCharm, support reading only absolute file paths from tools' output. For example, there is an issue in PyCharm's issue tracker asking for a way to read relative paths from tools: https://youtrack.jetbrains.com/issue/IDEA-48163 Sadly, that issue did not receive any action for 11 years already, apparently. And there is a comment from someone saying that mypy is affected by this problem, not having any way to force absolute paths in the output. This commit adds a new option, --show-absolute-path, to fix exactly this, with a straightforward implementation. I believe that this simple fix will bring considerable benefits to PyCharm users and potentially other people as well.
1 parent 6db1663 commit 4d332c5

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

mypy/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def _build(sources: List[BuildSource],
203203
options.show_column_numbers,
204204
options.show_error_codes,
205205
options.pretty,
206-
lambda path: read_py_file(path, cached_read, options.python_version))
206+
lambda path: read_py_file(path, cached_read, options.python_version),
207+
options.show_absolute_path)
207208
plugin, snapshot = load_plugins(options, errors, stdout)
208209

209210
# Construct a build manager object to hold state during the build.

mypy/errors.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ class Errors:
148148
# Set to True to show column numbers in error messages.
149149
show_column_numbers = False # type: bool
150150

151+
# Set to True to show absolute file paths in error messages.
152+
show_absolute_path = False # type: bool
153+
151154
# State for keeping track of the current fine-grained incremental mode target.
152155
# (See mypy.server.update for more about targets.)
153156
# Current module id.
@@ -159,10 +162,12 @@ def __init__(self,
159162
show_column_numbers: bool = False,
160163
show_error_codes: bool = False,
161164
pretty: bool = False,
162-
read_source: Optional[Callable[[str], Optional[List[str]]]] = None) -> None:
165+
read_source: Optional[Callable[[str], Optional[List[str]]]] = None,
166+
show_absolute_path: bool = False) -> None:
163167
self.show_error_context = show_error_context
164168
self.show_column_numbers = show_column_numbers
165169
self.show_error_codes = show_error_codes
170+
self.show_absolute_path = show_absolute_path
166171
self.pretty = pretty
167172
# We use fscache to read source code when showing snippets.
168173
self.read_source = read_source
@@ -188,7 +193,8 @@ def copy(self) -> 'Errors':
188193
self.show_column_numbers,
189194
self.show_error_codes,
190195
self.pretty,
191-
self.read_source)
196+
self.read_source,
197+
self.show_absolute_path)
192198
new.file = self.file
193199
new.import_ctx = self.import_ctx[:]
194200
new.function_or_member = self.function_or_member[:]
@@ -208,8 +214,11 @@ def set_ignore_prefix(self, prefix: str) -> None:
208214
self.ignore_prefix = prefix
209215

210216
def simplify_path(self, file: str) -> str:
211-
file = os.path.normpath(file)
212-
return remove_path_prefix(file, self.ignore_prefix)
217+
if self.show_absolute_path:
218+
return os.path.abspath(file)
219+
else:
220+
file = os.path.normpath(file)
221+
return remove_path_prefix(file, self.ignore_prefix)
213222

214223
def set_file(self, file: str,
215224
module: Optional[str],

mypy/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ def add_invertible_flag(flag: str,
595595
add_invertible_flag('--no-error-summary', dest='error_summary', default=True,
596596
help="Do not show error stats summary",
597597
group=error_group)
598+
add_invertible_flag('--show-absolute-path', default=False,
599+
help="Show absolute paths to files",
600+
group=error_group)
598601

599602
strict_help = "Strict mode; enables the following flags: {}".format(
600603
", ".join(strict_flag_names))

mypy/options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ def __init__(self) -> None:
261261
self.cache_map = {} # type: Dict[str, Tuple[str, str]]
262262
# Don't properly free objects on exit, just kill the current process.
263263
self.fast_exit = False
264+
# Print full path to each file in the report.
265+
self.show_absolute_path = False # type: bool
264266

265267
# To avoid breaking plugin compatability, keep providing new_semantic_analyzer
266268
@property

0 commit comments

Comments
 (0)