diff --git a/mypy/build.py b/mypy/build.py index 60ffb8935054..0da427dd1bef 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -1262,7 +1262,7 @@ def wrap_context(self) -> Iterator[None]: except CompileError: raise except Exception as err: - report_internal_error(err, self.path, 0, self.manager.errors) + report_internal_error(err, self.path, 0, self.manager.errors, self.options) self.manager.errors.set_import_context(save_import_context) self.check_blockers() @@ -1405,7 +1405,7 @@ def semantic_analysis(self) -> None: def semantic_analysis_pass_three(self) -> None: with self.wrap_context(): - self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath) + self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath, self.options) if self.options.dump_type_stats: dump_type_stats(self.tree, self.xpath) diff --git a/mypy/checker.py b/mypy/checker.py index c30c00dd00ab..fc2684dcc685 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -219,7 +219,7 @@ def accept(self, node: Node, type_context: Type = None) -> Type: try: typ = node.accept(self) except Exception as err: - report_internal_error(err, self.errors.file, node.line, self.errors) + report_internal_error(err, self.errors.file, node.line, self.errors, self.options) self.type_context.pop() self.store_type(node, typ) if self.typing_mode_none(): diff --git a/mypy/errors.py b/mypy/errors.py index a448bdd01af4..541e4ca61cd2 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -6,6 +6,8 @@ from typing import Tuple, List, TypeVar, Set, Dict, Optional +from mypy.options import Options + T = TypeVar('T') @@ -420,24 +422,8 @@ def remove_path_prefix(path: str, prefix: str) -> str: return path -# Corresponds to command-line flag --pdb. -drop_into_pdb = False - -# Corresponds to command-line flag --show-traceback. -show_tb = False - - -def set_drop_into_pdb(flag: bool) -> None: - global drop_into_pdb - drop_into_pdb = flag - - -def set_show_tb(flag: bool) -> None: - global show_tb - show_tb = flag - - -def report_internal_error(err: Exception, file: str, line: int, errors: Errors) -> None: +def report_internal_error(err: Exception, file: str, line: int, + errors: Errors, options: Options) -> None: """Report internal error and exit. This optionally starts pdb or shows a traceback. @@ -462,14 +448,14 @@ def report_internal_error(err: Exception, file: str, line: int, errors: Errors) file=sys.stderr) # If requested, drop into pdb. This overrides show_tb. - if drop_into_pdb: + if options.pdb: print('Dropping into pdb', file=sys.stderr) import pdb pdb.post_mortem(sys.exc_info()[2]) # If requested, print traceback, else print note explaining how to get one. - if not show_tb: - if not drop_into_pdb: + if not options.show_traceback: + if not options.pdb: print('{}: note: please use --show-traceback to print a traceback ' 'when reporting a bug'.format(prefix), file=sys.stderr) diff --git a/mypy/main.py b/mypy/main.py index fef94b595efa..62d068c586c8 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -13,7 +13,7 @@ from mypy import git from mypy import experiments from mypy.build import BuildSource, BuildResult, PYTHON_EXTENSIONS -from mypy.errors import CompileError, set_drop_into_pdb, set_show_tb +from mypy.errors import CompileError from mypy.options import Options, BuildType from mypy.report import reporter_classes @@ -33,10 +33,6 @@ def main(script_path: str) -> None: else: bin_dir = None sources, options = process_options(sys.argv[1:]) - if options.pdb: - set_drop_into_pdb(True) - if options.show_traceback: - set_show_tb(True) f = sys.stdout try: res = type_check_only(sources, bin_dir, options) diff --git a/mypy/semanal.py b/mypy/semanal.py index c886065996f7..22148ba6fb85 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -2559,7 +2559,7 @@ def accept(self, node: Node) -> None: try: node.accept(self) except Exception as err: - report_internal_error(err, self.errors.file, node.line, self.errors) + report_internal_error(err, self.errors.file, node.line, self.errors, self.options) class FirstPass(NodeVisitor): @@ -2768,15 +2768,16 @@ def __init__(self, modules: Dict[str, MypyFile], errors: Errors) -> None: self.modules = modules self.errors = errors - def visit_file(self, file_node: MypyFile, fnam: str) -> None: + def visit_file(self, file_node: MypyFile, fnam: str, options: Options) -> None: self.errors.set_file(fnam) + self.options = options self.accept(file_node) def accept(self, node: Node) -> None: try: node.accept(self) except Exception as err: - report_internal_error(err, self.errors.file, node.line, self.errors) + report_internal_error(err, self.errors.file, node.line, self.errors, self.options) def visit_block(self, b: Block) -> None: if b.is_unreachable: diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index aa5668f25098..d90dd371b965 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -20,7 +20,7 @@ assert_string_arrays_equal, normalize_error_messages, testcase_pyversion, update_testcase_output, ) -from mypy.errors import CompileError, set_show_tb +from mypy.errors import CompileError from mypy.options import Options from mypy import experiments @@ -118,7 +118,7 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental=0) -> None: options = self.parse_options(original_program_text, testcase) options.use_builtins_fixtures = True - set_show_tb(True) # Show traceback on crash. + options.show_traceback = True if 'optional' in testcase.file: options.strict_optional = True diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py index d1ad9e76688e..78cc9ce3ab5c 100644 --- a/mypy/test/testcmdline.py +++ b/mypy/test/testcmdline.py @@ -44,7 +44,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None: for s in testcase.input: file.write('{}\n'.format(s)) args = parse_args(testcase.input[0]) - args.append('--tb') # Show traceback on crash. + args.append('--show-traceback') # Type check the program. fixed = [python3_path, os.path.join(testcase.old_cwd, 'scripts', 'mypy')] diff --git a/mypy/test/testpythoneval.py b/mypy/test/testpythoneval.py index f5d94a8c8a94..e4dfb0231764 100644 --- a/mypy/test/testpythoneval.py +++ b/mypy/test/testpythoneval.py @@ -62,7 +62,7 @@ def test_python_evaluation(testcase): interpreter = python3_path args = [] py2 = False - args.append('--tb') # Show traceback on crash. + args.append('--show-traceback') # Write the program to a file. program = '_program.py' program_path = os.path.join(test_temp_dir, program) diff --git a/mypy/test/testsemanal.py b/mypy/test/testsemanal.py index c2f7280db8f5..3486d0fdc528 100644 --- a/mypy/test/testsemanal.py +++ b/mypy/test/testsemanal.py @@ -36,6 +36,7 @@ def get_semanal_options(): options = Options() options.use_builtins_fixtures = True options.semantic_analysis_only = True + options.show_traceback = True return options diff --git a/mypy/test/testtransform.py b/mypy/test/testtransform.py index d96af94ee729..782b2655126f 100644 --- a/mypy/test/testtransform.py +++ b/mypy/test/testtransform.py @@ -45,6 +45,7 @@ def test_transform(testcase): options = Options() options.use_builtins_fixtures = True options.semantic_analysis_only = True + options.show_traceback = True options.python_version = testfile_pyversion(testcase.file) result = build.build(sources=[BuildSource('main', None, src)], options=options, diff --git a/mypy/test/testtypegen.py b/mypy/test/testtypegen.py index e8278e07ef24..b176090fa983 100644 --- a/mypy/test/testtypegen.py +++ b/mypy/test/testtypegen.py @@ -39,6 +39,7 @@ def run_test(self, testcase): src = '\n'.join(testcase.input) options = Options() options.use_builtins_fixtures = True + options.show_traceback = True result = build.build(sources=[BuildSource('main', None, src)], options=options, alt_lib_path=config.test_temp_dir) diff --git a/runtests.py b/runtests.py index 9ca8c0d2413e..08c74d1c4822 100755 --- a/runtests.py +++ b/runtests.py @@ -77,7 +77,7 @@ def add_mypy_cmd(self, name: str, mypy_args: List[str], cwd: Optional[str] = Non if not self.allow(full_name): return args = [sys.executable, self.mypy] + mypy_args - args.append('--tb') # Show traceback on crash. + args.append('--show-traceback') self.waiter.add(LazySubprocess(full_name, args, cwd=cwd, env=self.env)) def add_mypy(self, name: str, *args: str, cwd: Optional[str] = None) -> None: