Skip to content

Suppress errors from semantic analysis in dynamic (unannotated) functions. #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,18 @@ def __init__(self, data_dir: str,
self.custom_typing_module = custom_typing_module
self.source_set = source_set
self.reports = reports
check_untyped_defs = CHECK_UNTYPED_DEFS in self.flags
self.semantic_analyzer = SemanticAnalyzer(lib_path, self.errors,
pyversion=pyversion)
pyversion=pyversion,
check_untyped_defs=check_untyped_defs)
self.modules = self.semantic_analyzer.modules
self.semantic_analyzer_pass3 = ThirdPass(self.modules, self.errors)
self.type_checker = TypeChecker(self.errors,
self.modules,
self.pyversion,
DISALLOW_UNTYPED_CALLS in self.flags,
DISALLOW_UNTYPED_DEFS in self.flags,
CHECK_UNTYPED_DEFS in self.flags)
check_untyped_defs)
self.missing_modules = set() # type: Set[str]

def all_imported_modules_in_file(self,
Expand Down
25 changes: 20 additions & 5 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ class SemanticAnalyzer(NodeVisitor):
imports = None # type: Set[str] # Imported modules (during phase 2 analysis)
errors = None # type: Errors # Keeps track of generated errors

def __init__(self, lib_path: List[str], errors: Errors,
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION) -> None:
def __init__(self,
lib_path: List[str],
errors: Errors,
pyversion: Tuple[int, int],
check_untyped_defs: bool) -> None:
"""Construct semantic analyzer.

Use lib_path to search for modules, and report analysis errors
Expand All @@ -211,6 +214,7 @@ def __init__(self, lib_path: List[str], errors: Errors,
self.errors = errors
self.modules = {}
self.pyversion = pyversion
self.check_untyped_defs = check_untyped_defs
self.postpone_nested_functions_stack = [FUNCTION_BOTH_PHASES]
self.postponed_functions_stack = []

Expand Down Expand Up @@ -244,10 +248,12 @@ def visit_file(self, file_node: MypyFile, fnam: str) -> None:
def visit_func_def(self, defn: FuncDef) -> None:
phase_info = self.postpone_nested_functions_stack[-1]
if phase_info != FUNCTION_SECOND_PHASE:
self.function_stack.append(defn)
# First phase of analysis for function.
self.errors.push_function(defn.name())
self.update_function_type_variables(defn)
self.errors.pop_function()
self.function_stack.pop()

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

Expand Down Expand Up @@ -1630,11 +1636,11 @@ def visit_for_stmt(self, s: ForStmt) -> None:

def visit_break_stmt(self, s: BreakStmt) -> None:
if self.loop_depth == 0:
self.fail("'break' outside loop", s)
self.fail("'break' outside loop", s, True)

def visit_continue_stmt(self, s: ContinueStmt) -> None:
if self.loop_depth == 0:
self.fail("'continue' outside loop", s)
self.fail("'continue' outside loop", s, True)

def visit_if_stmt(self, s: IfStmt) -> None:
infer_reachability_of_if_statement(s, pyversion=self.pyversion)
Expand Down Expand Up @@ -2203,10 +2209,19 @@ def name_not_defined(self, name: str, ctx: Context) -> None:
def name_already_defined(self, name: str, ctx: Context) -> None:
self.fail("Name '{}' already defined".format(name), ctx)

def fail(self, msg: str, ctx: Context) -> None:
def fail(self, msg: str, ctx: Context, serious: bool = False) -> None:
if (not serious and
not self.check_untyped_defs and
self.function_stack and
self.function_stack[-1].is_dynamic()):
return
self.errors.report(ctx.get_line(), msg)

def note(self, msg: str, ctx: Context) -> None:
if (not self.check_untyped_defs and
self.function_stack and
self.function_stack[-1].is_dynamic()):
return
self.errors.report(ctx.get_line(), msg, severity='note')

def undefined_name_extra_info(self, fullname: str) -> Optional[str]:
Expand Down
8 changes: 0 additions & 8 deletions mypy/test/data/check-weak-typing.test
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ def f():
1 + 'a' # E: Unsupported left operand type for + ("int")
[out]
main: note: In function "f":
[case testNonWeakFunction]
def f():
if y: # E: Name 'y' is not defined
x = 1
else:
x = 'a'
[out]
main: note: In function "f":
[case testWeakFunctionCall]
# mypy: weak=global
def f(a: str) -> None: pass
Expand Down
38 changes: 19 additions & 19 deletions mypy/test/data/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ main:3: error: Name 'y' is not defined

[case testUndefinedVariableWithinFunctionContext]
import typing
def f():
def f() -> None:
x
y
[out]
Expand All @@ -39,7 +39,7 @@ import typing
class A:
def f(self): pass
class B:
def g(self):
def g(self) -> None:
f # error
g # error
[out]
Expand Down Expand Up @@ -178,7 +178,7 @@ main:4: error: Name 'x' already defined
[case testLocalVarRedefinition]
import typing
class A: pass
def f():
def f() -> None:
x = 0 # type: A
x = 0 # type: A
[out]
Expand Down Expand Up @@ -523,7 +523,7 @@ main:4: error: Invalid type "__main__.t"
from typing import TypeVar, Generic
t = TypeVar('t')
class c(Generic[t]):
def f(self): x = t
def f(self) -> None: x = t
def f(y: t): x = t
[out]
main: note: In function "f":
Expand All @@ -546,7 +546,7 @@ main:2: error: Name 'B' is not defined
[case testSuperOutsideClass]
class A: pass
super().x
def f(): super().y
def f() -> None: super().y
[out]
main:2: error: "super" used outside class
main: note: In function "f":
Expand All @@ -572,7 +572,7 @@ main:5: error: Name 'f' already defined

[case testInvalidGlobalDecl]
import typing
def f():
def f() -> None:
global x
x = None
[out]
Expand All @@ -582,7 +582,7 @@ main:4: error: Name 'x' is not defined
[case testInvalidNonlocalDecl]
import typing
def f():
def g():
def g() -> None:
nonlocal x
x = None
[out]
Expand All @@ -593,7 +593,7 @@ main:5: error: Name 'x' is not defined
[case testNonlocalDeclNotMatchingGlobal]
import typing
x = None
def f():
def f() -> None:
nonlocal x
x = None
[out]
Expand All @@ -605,7 +605,7 @@ main:5: error: Name 'x' is not defined
import typing
def g():
x = None
def f(x):
def f(x) -> None:
nonlocal x
x = None
[out]
Expand All @@ -623,7 +623,7 @@ import typing
x = 1
def f():
x = 1
def g():
def g() -> None:
global x
nonlocal x
x = None
Expand All @@ -636,7 +636,7 @@ import typing
x = 1
def f():
x = 1
def g():
def g() -> None:
nonlocal x
global x
x = None
Expand All @@ -646,7 +646,7 @@ main:7: error: Name 'x' is nonlocal and global

[case testNestedFunctionAndScoping]
import typing
def f(x):
def f(x) -> None:
def g(y):
z = x
z
Expand All @@ -659,7 +659,7 @@ main:6: error: Name 'y' is not defined

[case testMultipleNestedFunctionDef]
import typing
def f(x):
def f(x) -> None:
def g(): pass
x = 1
def g(): pass
Expand All @@ -669,7 +669,7 @@ main:5: error: Name 'g' already defined

[case testRedefinedOverloadedFunction]
from typing import overload, Any
def f():
def f() -> None:
@overload
def p(o: object) -> None: pass # no error
@overload
Expand All @@ -683,8 +683,8 @@ main:8: error: Name 'p' already defined
[case testNestedFunctionInMethod]
import typing
class A:
def f(self):
def g():
def f(self) -> None:
def g() -> None:
x
y
[out]
Expand Down Expand Up @@ -861,7 +861,7 @@ main:3: error: 'abstractmethod' used with a non-method
[case testAbstractNestedFunction]
import typing
from abc import abstractmethod
def g():
def g() -> None:
@abstractmethod
def foo(): pass
[out]
Expand Down Expand Up @@ -1133,7 +1133,7 @@ import typing
@staticmethod
def f(): pass
class A:
def g(self):
def g(self) -> None:
@staticmethod
def h(): pass
[builtins fixtures/staticmethod.py]
Expand All @@ -1147,7 +1147,7 @@ import typing
@classmethod
def f(): pass
class A:
def g(self):
def g(self) -> None:
@classmethod
def h(): pass
[builtins fixtures/classmethod.py]
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/data/semanal-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ MypyFile:1(
IntExpr(0)))))))

[case testDelMultipleThingsInvalid]
def f(x, y):
def f(x, y) -> None:
del x, y + 1
[out]
main: note: In function "f":
Expand Down