Skip to content

Commit eb16d1d

Browse files
authored
Remove weird implementation of --show_traceback and --pdb. (#2216)
Also set options.show_traceback in all test modules I could find. These flags used to be implemented as globals in errors.py that had to be set by calling set_show_tb() or set_drop_into_pdb() (and setting the corresponding Options attributes had no effect). Now they're just regular options. I had to adjust some plumbing to give report_internal_error() access to the options but it was easier than I thought.
1 parent d6a7069 commit eb16d1d

12 files changed

+23
-37
lines changed

mypy/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ def wrap_context(self) -> Iterator[None]:
12621262
except CompileError:
12631263
raise
12641264
except Exception as err:
1265-
report_internal_error(err, self.path, 0, self.manager.errors)
1265+
report_internal_error(err, self.path, 0, self.manager.errors, self.options)
12661266
self.manager.errors.set_import_context(save_import_context)
12671267
self.check_blockers()
12681268

@@ -1405,7 +1405,7 @@ def semantic_analysis(self) -> None:
14051405

14061406
def semantic_analysis_pass_three(self) -> None:
14071407
with self.wrap_context():
1408-
self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath)
1408+
self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath, self.options)
14091409
if self.options.dump_type_stats:
14101410
dump_type_stats(self.tree, self.xpath)
14111411

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def accept(self, node: Node, type_context: Type = None) -> Type:
219219
try:
220220
typ = node.accept(self)
221221
except Exception as err:
222-
report_internal_error(err, self.errors.file, node.line, self.errors)
222+
report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
223223
self.type_context.pop()
224224
self.store_type(node, typ)
225225
if self.typing_mode_none():

mypy/errors.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from typing import Tuple, List, TypeVar, Set, Dict, Optional
88

9+
from mypy.options import Options
10+
911

1012
T = TypeVar('T')
1113

@@ -420,24 +422,8 @@ def remove_path_prefix(path: str, prefix: str) -> str:
420422
return path
421423

422424

423-
# Corresponds to command-line flag --pdb.
424-
drop_into_pdb = False
425-
426-
# Corresponds to command-line flag --show-traceback.
427-
show_tb = False
428-
429-
430-
def set_drop_into_pdb(flag: bool) -> None:
431-
global drop_into_pdb
432-
drop_into_pdb = flag
433-
434-
435-
def set_show_tb(flag: bool) -> None:
436-
global show_tb
437-
show_tb = flag
438-
439-
440-
def report_internal_error(err: Exception, file: str, line: int, errors: Errors) -> None:
425+
def report_internal_error(err: Exception, file: str, line: int,
426+
errors: Errors, options: Options) -> None:
441427
"""Report internal error and exit.
442428
443429
This optionally starts pdb or shows a traceback.
@@ -462,14 +448,14 @@ def report_internal_error(err: Exception, file: str, line: int, errors: Errors)
462448
file=sys.stderr)
463449

464450
# If requested, drop into pdb. This overrides show_tb.
465-
if drop_into_pdb:
451+
if options.pdb:
466452
print('Dropping into pdb', file=sys.stderr)
467453
import pdb
468454
pdb.post_mortem(sys.exc_info()[2])
469455

470456
# If requested, print traceback, else print note explaining how to get one.
471-
if not show_tb:
472-
if not drop_into_pdb:
457+
if not options.show_traceback:
458+
if not options.pdb:
473459
print('{}: note: please use --show-traceback to print a traceback '
474460
'when reporting a bug'.format(prefix),
475461
file=sys.stderr)

mypy/main.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mypy import git
1414
from mypy import experiments
1515
from mypy.build import BuildSource, BuildResult, PYTHON_EXTENSIONS
16-
from mypy.errors import CompileError, set_drop_into_pdb, set_show_tb
16+
from mypy.errors import CompileError
1717
from mypy.options import Options, BuildType
1818
from mypy.report import reporter_classes
1919

@@ -33,10 +33,6 @@ def main(script_path: str) -> None:
3333
else:
3434
bin_dir = None
3535
sources, options = process_options(sys.argv[1:])
36-
if options.pdb:
37-
set_drop_into_pdb(True)
38-
if options.show_traceback:
39-
set_show_tb(True)
4036
f = sys.stdout
4137
try:
4238
res = type_check_only(sources, bin_dir, options)

mypy/semanal.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ def accept(self, node: Node) -> None:
25592559
try:
25602560
node.accept(self)
25612561
except Exception as err:
2562-
report_internal_error(err, self.errors.file, node.line, self.errors)
2562+
report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
25632563

25642564

25652565
class FirstPass(NodeVisitor):
@@ -2768,15 +2768,16 @@ def __init__(self, modules: Dict[str, MypyFile], errors: Errors) -> None:
27682768
self.modules = modules
27692769
self.errors = errors
27702770

2771-
def visit_file(self, file_node: MypyFile, fnam: str) -> None:
2771+
def visit_file(self, file_node: MypyFile, fnam: str, options: Options) -> None:
27722772
self.errors.set_file(fnam)
2773+
self.options = options
27732774
self.accept(file_node)
27742775

27752776
def accept(self, node: Node) -> None:
27762777
try:
27772778
node.accept(self)
27782779
except Exception as err:
2779-
report_internal_error(err, self.errors.file, node.line, self.errors)
2780+
report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
27802781

27812782
def visit_block(self, b: Block) -> None:
27822783
if b.is_unreachable:

mypy/test/testcheck.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
assert_string_arrays_equal, normalize_error_messages,
2121
testcase_pyversion, update_testcase_output,
2222
)
23-
from mypy.errors import CompileError, set_show_tb
23+
from mypy.errors import CompileError
2424
from mypy.options import Options
2525

2626
from mypy import experiments
@@ -118,7 +118,7 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental=0) -> None:
118118

119119
options = self.parse_options(original_program_text, testcase)
120120
options.use_builtins_fixtures = True
121-
set_show_tb(True) # Show traceback on crash.
121+
options.show_traceback = True
122122
if 'optional' in testcase.file:
123123
options.strict_optional = True
124124

mypy/test/testcmdline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
4444
for s in testcase.input:
4545
file.write('{}\n'.format(s))
4646
args = parse_args(testcase.input[0])
47-
args.append('--tb') # Show traceback on crash.
47+
args.append('--show-traceback')
4848
# Type check the program.
4949
fixed = [python3_path,
5050
os.path.join(testcase.old_cwd, 'scripts', 'mypy')]

mypy/test/testpythoneval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_python_evaluation(testcase):
6262
interpreter = python3_path
6363
args = []
6464
py2 = False
65-
args.append('--tb') # Show traceback on crash.
65+
args.append('--show-traceback')
6666
# Write the program to a file.
6767
program = '_program.py'
6868
program_path = os.path.join(test_temp_dir, program)

mypy/test/testsemanal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def get_semanal_options():
3636
options = Options()
3737
options.use_builtins_fixtures = True
3838
options.semantic_analysis_only = True
39+
options.show_traceback = True
3940
return options
4041

4142

mypy/test/testtransform.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_transform(testcase):
4545
options = Options()
4646
options.use_builtins_fixtures = True
4747
options.semantic_analysis_only = True
48+
options.show_traceback = True
4849
options.python_version = testfile_pyversion(testcase.file)
4950
result = build.build(sources=[BuildSource('main', None, src)],
5051
options=options,

mypy/test/testtypegen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def run_test(self, testcase):
3939
src = '\n'.join(testcase.input)
4040
options = Options()
4141
options.use_builtins_fixtures = True
42+
options.show_traceback = True
4243
result = build.build(sources=[BuildSource('main', None, src)],
4344
options=options,
4445
alt_lib_path=config.test_temp_dir)

runtests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def add_mypy_cmd(self, name: str, mypy_args: List[str], cwd: Optional[str] = Non
7777
if not self.allow(full_name):
7878
return
7979
args = [sys.executable, self.mypy] + mypy_args
80-
args.append('--tb') # Show traceback on crash.
80+
args.append('--show-traceback')
8181
self.waiter.add(LazySubprocess(full_name, args, cwd=cwd, env=self.env))
8282

8383
def add_mypy(self, name: str, *args: str, cwd: Optional[str] = None) -> None:

0 commit comments

Comments
 (0)