FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Ignoring a file with a single # type: ignore comment. by brandtbucher · Pull Request #6830 · python/mypy · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Code cleanup from GvR's first review.
  • Loading branch information
brandtbucher committed May 14, 2019
commit c95b98ddf69d1f8b6ef2de0e2a67f73bbd7a7cca
55 changes: 27 additions & 28 deletions mypy/fastparse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -294,49 +294,48 @@ def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]:
res.append(exp)
return res

def get_line(self, n: ast3.stmt) -> int:
if (isinstance(n, (ast3.AsyncFunctionDef, ast3.ClassDef, ast3.FunctionDef))
and n.decorator_list):
return n.decorator_list[0].lineno
return n.lineno
def get_lineno(self, node: Union[ast3.expr, ast3.stmt]) -> int:
if (isinstance(node, (ast3.AsyncFunctionDef, ast3.ClassDef, ast3.FunctionDef))
and node.decorator_list):
return node.decorator_list[0].lineno
return node.lineno

def translate_stmt_list(self,
l: Sequence[ast3.stmt],
module: bool = False) -> List[Statement]:

stmts: Sequence[ast3.stmt],
ismodule: bool = False) -> List[Statement]:
# A "# type: ignore" comment before the first statement of a module
# ignores the whole module:

if module and l and self.type_ignores and min(self.type_ignores) < self.get_line(l[0]):
if (ismodule and stmts and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])):
self.errors.used_ignored_lines[self.errors.file].add(min(self.type_ignores))
b = Block(self.fix_function_overloads(self.translate_stmt_list(l)))
b.is_unreachable = True
return [b]
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
block.is_unreachable = True
return [block]

res = [] # type: List[Statement]
line = 0

for i, e in enumerate(l):
for i, stmt in enumerate(stmts):

if module: # and...
if ismodule: # This line needs to be split for mypy to branch on version:
if sys.version_info >= (3, 8):

# In Python 3.8, a "# type: ignore" comment between statements at
# the top level of a module skips checking for everything else:

ignores = set(range(line + 1, self.get_line(e))) & self.type_ignores
# In Python 3.8+ (we need end_lineno), a "# type: ignore" comment
# between statements at the top level of a module skips checking
# for everything else:
ignores = set(range(line + 1, self.get_lineno(stmt))) & self.type_ignores

if ignores:
self.errors.used_ignored_lines[self.errors.file].add(min(ignores))
b = Block(self.fix_function_overloads(self.translate_stmt_list(l[i:])))
b.is_unreachable = True
res.append(b)
rest = self.fix_function_overloads(self.translate_stmt_list(stmts[i:]))
block = Block(rest)
block.is_unreachable = True
res.append(block)
return res

line = e.end_lineno if e.end_lineno is not None else e.lineno
line = stmt.end_lineno if stmt.end_lineno is not None else stmt.lineno

stmt = self.visit(e)
res.append(stmt)
node = self.visit(stmt)
res.append(node)

return res

Expand Down Expand Up @@ -443,11 +442,11 @@ def translate_module_id(self, id: str) -> str:

def visit_Module(self, mod: ast3.Module) -> MypyFile:
self.type_ignores = {ti.lineno for ti in mod.type_ignores}
body = self.fix_function_overloads(self.translate_stmt_list(mod.body, module=True))
body = self.fix_function_overloads(self.translate_stmt_list(mod.body, ismodule=True))
return MypyFile(body,
self.imports,
False,
set(self.type_ignores),
self.type_ignores,
)

# --- stmt ---
Expand Down
31 changes: 15 additions & 16 deletions mypy/fastparse2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -193,29 +193,28 @@ def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]:
res.append(exp)
return res

def get_line(self, n: ast27.stmt) -> int:
if isinstance(n, (ast27.ClassDef, ast27.FunctionDef)) and n.decorator_list:
return n.decorator_list[0].lineno
return n.lineno
def get_lineno(self, node: Union[ast27.expr, ast27.stmt]) -> int:
if isinstance(node, (ast27.ClassDef, ast27.FunctionDef)) and node.decorator_list:
return node.decorator_list[0].lineno
return node.lineno

def translate_stmt_list(self,
l: Sequence[ast27.stmt],
stmts: Sequence[ast27.stmt],
module: bool = False) -> List[Statement]:

# A "# type: ignore" comment before the first statement of a module
# ignores the whole module:

if module and l and self.type_ignores and min(self.type_ignores) < self.get_line(l[0]):
if (module and stmts and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])):
self.errors.used_ignored_lines[self.errors.file].add(min(self.type_ignores))
b = Block(self.fix_function_overloads(self.translate_stmt_list(l)))
b.is_unreachable = True
return [b]
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
block.is_unreachable = True
return [block]

res = [] # type: List[Statement]
for e in l:
stmt = self.visit(e)
assert isinstance(stmt, Statement)
res.append(stmt)
for stmt in stmts:
node = self.visit(stmt)
assert isinstance(node, Statement)
res.append(node)
return res

op_map = {
Expand Down Expand Up @@ -326,7 +325,7 @@ def visit_Module(self, mod: ast27.Module) -> MypyFile:
return MypyFile(body,
self.imports,
False,
set(self.type_ignores),
self.type_ignores,
)

# --- stmt ---
Expand Down

Back | FazBrowse Home | New Git URL