Skip to content

Commit 32a80a5

Browse files
author
Guido van Rossum
committed
Suppress errors from semantic analysis in dynamic (unannotated) functions. Fix #1334.
1 parent e0151a2 commit 32a80a5

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

mypy/build.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,18 @@ def __init__(self, data_dir: str,
368368
self.custom_typing_module = custom_typing_module
369369
self.source_set = source_set
370370
self.reports = reports
371+
check_untyped_defs = CHECK_UNTYPED_DEFS in self.flags
371372
self.semantic_analyzer = SemanticAnalyzer(lib_path, self.errors,
372-
pyversion=pyversion)
373+
pyversion=pyversion,
374+
check_untyped_defs=check_untyped_defs)
373375
self.modules = self.semantic_analyzer.modules
374376
self.semantic_analyzer_pass3 = ThirdPass(self.modules, self.errors)
375377
self.type_checker = TypeChecker(self.errors,
376378
self.modules,
377379
self.pyversion,
378380
DISALLOW_UNTYPED_CALLS in self.flags,
379381
DISALLOW_UNTYPED_DEFS in self.flags,
380-
CHECK_UNTYPED_DEFS in self.flags)
382+
check_untyped_defs)
381383
self.missing_modules = set() # type: Set[str]
382384

383385
def all_imported_modules_in_file(self,

mypy/semanal.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ class SemanticAnalyzer(NodeVisitor):
191191
errors = None # type: Errors # Keeps track of generated errors
192192

193193
def __init__(self, lib_path: List[str], errors: Errors,
194-
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION) -> None:
194+
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
195+
check_untyped_defs: bool = False) -> None:
195196
"""Construct semantic analyzer.
196197
197198
Use lib_path to search for modules, and report analysis errors
@@ -211,6 +212,7 @@ def __init__(self, lib_path: List[str], errors: Errors,
211212
self.errors = errors
212213
self.modules = {}
213214
self.pyversion = pyversion
215+
self.check_untyped_defs = check_untyped_defs
214216
self.postpone_nested_functions_stack = [FUNCTION_BOTH_PHASES]
215217
self.postponed_functions_stack = []
216218

@@ -244,10 +246,12 @@ def visit_file(self, file_node: MypyFile, fnam: str) -> None:
244246
def visit_func_def(self, defn: FuncDef) -> None:
245247
phase_info = self.postpone_nested_functions_stack[-1]
246248
if phase_info != FUNCTION_SECOND_PHASE:
249+
self.function_stack.append(defn)
247250
# First phase of analysis for function.
248251
self.errors.push_function(defn.name())
249252
self.update_function_type_variables(defn)
250253
self.errors.pop_function()
254+
self.function_stack.pop()
251255

252256
defn.is_conditional = self.block_depth[-1] > 0
253257

@@ -2204,9 +2208,17 @@ def name_already_defined(self, name: str, ctx: Context) -> None:
22042208
self.fail("Name '{}' already defined".format(name), ctx)
22052209

22062210
def fail(self, msg: str, ctx: Context) -> None:
2211+
if (self.function_stack and
2212+
self.function_stack[-1].is_dynamic() and
2213+
not self.check_untyped_defs):
2214+
return
22072215
self.errors.report(ctx.get_line(), msg)
22082216

22092217
def note(self, msg: str, ctx: Context) -> None:
2218+
if (self.function_stack and
2219+
self.function_stack[-1].is_dynamic() and
2220+
not self.check_untyped_defs):
2221+
return
22102222
self.errors.report(ctx.get_line(), msg, severity='note')
22112223

22122224
def undefined_name_extra_info(self, fullname: str) -> Optional[str]:

0 commit comments

Comments
 (0)