Skip to content

Commit a3febc9

Browse files
committed
Print mypy version on error
1 parent 0a9f88d commit a3febc9

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

mypy/errors.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Tuple, List, TypeVar, Set, Dict, Iterator, Optional
88

99
from mypy.options import Options
10+
from mypy.version import __version__ as mypy_version
1011

1112

1213
T = TypeVar('T')
@@ -514,40 +515,45 @@ def remove_path_prefix(path: str, prefix: str) -> str:
514515

515516

516517
def report_internal_error(err: Exception, file: str, line: int,
517-
errors: Errors, options: Options) -> None:
518+
errors: Errors = None, options: Options = None) -> None:
518519
"""Report internal error and exit.
519520
520521
This optionally starts pdb or shows a traceback.
521522
"""
522523
# Dump out errors so far, they often provide a clue.
523524
# But catch unexpected errors rendering them.
524-
try:
525-
for msg in errors.messages():
526-
print(msg)
527-
except Exception as e:
528-
print("Failed to dump errors:", repr(e), file=sys.stderr)
525+
if errors:
526+
try:
527+
for msg in errors.messages():
528+
print(msg)
529+
except Exception as e:
530+
print("Failed to dump errors:", repr(e), file=sys.stderr)
529531

530532
# Compute file:line prefix for official-looking error messages.
531533
if line:
532-
prefix = '{}:{}'.format(file, line)
534+
prefix = '{}:{}: '.format(file, line)
535+
elif file:
536+
prefix = '{}: '.format(file)
533537
else:
534-
prefix = file
538+
prefix = ''
539+
535540

536541
# Print "INTERNAL ERROR" message.
537-
print('{}: error: INTERNAL ERROR --'.format(prefix),
542+
print('{}error: INTERNAL ERROR --'.format(prefix),
543+
'mypy version: {}'.format(mypy_version),
538544
'please report a bug at https://github.com/python/mypy/issues',
539545
file=sys.stderr)
540546

541547
# If requested, drop into pdb. This overrides show_tb.
542-
if options.pdb:
548+
if options and options.pdb:
543549
print('Dropping into pdb', file=sys.stderr)
544550
import pdb
545551
pdb.post_mortem(sys.exc_info()[2])
546552

547553
# If requested, print traceback, else print note explaining how to get one.
548-
if not options.show_traceback:
554+
if options and not options.show_traceback:
549555
if not options.pdb:
550-
print('{}: note: please use --show-traceback to print a traceback '
556+
print('{}note: please use --show-traceback to print a traceback '
551557
'when reporting a bug'.format(prefix),
552558
file=sys.stderr)
553559
else:
@@ -557,7 +563,7 @@ def report_internal_error(err: Exception, file: str, line: int,
557563
for s in traceback.format_list(tb + tb2):
558564
print(s.rstrip('\n'))
559565
print('{}: {}'.format(type(err).__name__, err))
560-
print('{}: note: use --pdb to drop into pdb'.format(prefix), file=sys.stderr)
566+
print('{}note: use --pdb to drop into pdb'.format(prefix), file=sys.stderr)
561567

562568
# Exit. The caller has nothing more to say.
563569
raise SystemExit(1)

scripts/mypy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env python3
22
"""Mypy type checker command line tool."""
33

4-
from mypy.main import main
4+
from mypy.errors import report_internal_error
55

6-
main(__file__)
6+
try:
7+
from mypy.main import main
8+
main(__file__)
9+
except Exception as err:
10+
report_internal_error(err, None, 0)

0 commit comments

Comments
 (0)