-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Ignoring a file with a single # type: ignore comment. #6830
Changes from 3 commits
7a51cf2
aa11089
ce54ab4
4567661
c95b98d
07cc315
9937b1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import sys | ||
|
||
from typing import ( | ||
Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, Dict, cast, List, overload | ||
Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, Dict, cast, List, overload, Set | ||
) | ||
MYPY = False | ||
if MYPY: | ||
|
@@ -258,7 +258,7 @@ def __init__(self, | |
self.is_stub = is_stub | ||
self.errors = errors | ||
|
||
self.extra_type_ignores = [] # type: List[int] | ||
self.type_ignores = set() # type: Set[int] | ||
|
||
# Cache of visit_X methods keyed by type of visited object | ||
self.visitor_cache = {} # type: Dict[type, Callable[[Optional[AST]], Any]] | ||
|
@@ -294,11 +294,50 @@ def translate_expr_list(self, l: Sequence[AST]) -> List[Expression]: | |
res.append(exp) | ||
return res | ||
|
||
def translate_stmt_list(self, l: Sequence[AST]) -> List[Statement]: | ||
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 translate_stmt_list(self, | ||
l: Sequence[ast3.stmt], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please choose a longer name for this argument, maybe |
||
module: bool = False) -> List[Statement]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps rename to |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd leave this blank line out, as well a the one below this block comment. (Ditto for other block comments below; in general you sprinkle more whitespace throughout the code than is mypy's tradition.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed a lot of my extra whitespace throughout. |
||
# 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]): | ||
self.errors.used_ignored_lines[self.errors.file].add(min(self.type_ignores)) | ||
b = Block(self.fix_function_overloads(self.translate_stmt_list(l))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename to |
||
b.is_unreachable = True | ||
return [b] | ||
|
||
res = [] # type: List[Statement] | ||
for e in l: | ||
line = 0 | ||
|
||
for i, e in enumerate(l): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I'd propose a longer name for |
||
|
||
if module: # and... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somehow it looks inefficient to check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was originally If it would be clearer, I can just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, then the two |
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most importantly, explain why: this check needs access to |
||
|
||
ignores = set(range(line + 1, self.get_line(e))) & 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) | ||
return res | ||
|
||
line = e.end_lineno if e.end_lineno is not None else e.lineno | ||
|
||
stmt = self.visit(e) | ||
res.append(stmt) | ||
|
||
return res | ||
|
||
op_map = { | ||
|
@@ -403,13 +442,12 @@ def translate_module_id(self, id: str) -> str: | |
return id | ||
|
||
def visit_Module(self, mod: ast3.Module) -> MypyFile: | ||
body = self.fix_function_overloads(self.translate_stmt_list(mod.body)) | ||
ignores = [ti.lineno for ti in mod.type_ignores] | ||
ignores.extend(self.extra_type_ignores) | ||
self.type_ignores = {ti.lineno for ti in mod.type_ignores} | ||
body = self.fix_function_overloads(self.translate_stmt_list(mod.body, module=True)) | ||
return MypyFile(body, | ||
self.imports, | ||
False, | ||
set(ignores), | ||
set(self.type_ignores), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add a comment explaining why it's copied here. (If there is a good reason!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this isn't needed. Removed. |
||
) | ||
|
||
# --- stmt --- | ||
|
@@ -615,7 +653,7 @@ def make_argument(self, arg: ast3.arg, default: Optional[ast3.expr], kind: int, | |
elif type_comment is not None: | ||
extra_ignore, arg_type = parse_type_comment(type_comment, arg.lineno, self.errors) | ||
if extra_ignore: | ||
self.extra_type_ignores.append(arg.lineno) | ||
self.type_ignores.add(arg.lineno) | ||
|
||
return Argument(Var(arg.arg), arg_type, self.visit(default), kind) | ||
|
||
|
@@ -673,7 +711,7 @@ def visit_Assign(self, n: ast3.Assign) -> AssignmentStmt: | |
if n.type_comment is not None: | ||
extra_ignore, typ = parse_type_comment(n.type_comment, n.lineno, self.errors) | ||
if extra_ignore: | ||
self.extra_type_ignores.append(n.lineno) | ||
self.type_ignores.add(n.lineno) | ||
else: | ||
typ = None | ||
s = AssignmentStmt(lvalues, rvalue, type=typ, new_syntax=False) | ||
|
@@ -707,7 +745,7 @@ def visit_For(self, n: ast3.For) -> ForStmt: | |
if n.type_comment is not None: | ||
extra_ignore, target_type = parse_type_comment(n.type_comment, n.lineno, self.errors) | ||
if extra_ignore: | ||
self.extra_type_ignores.append(n.lineno) | ||
self.type_ignores.add(n.lineno) | ||
else: | ||
target_type = None | ||
node = ForStmt(self.visit(n.target), | ||
|
@@ -722,7 +760,7 @@ def visit_AsyncFor(self, n: ast3.AsyncFor) -> ForStmt: | |
if n.type_comment is not None: | ||
extra_ignore, target_type = parse_type_comment(n.type_comment, n.lineno, self.errors) | ||
if extra_ignore: | ||
self.extra_type_ignores.append(n.lineno) | ||
self.type_ignores.add(n.lineno) | ||
else: | ||
target_type = None | ||
node = ForStmt(self.visit(n.target), | ||
|
@@ -753,7 +791,7 @@ def visit_With(self, n: ast3.With) -> WithStmt: | |
if n.type_comment is not None: | ||
extra_ignore, target_type = parse_type_comment(n.type_comment, n.lineno, self.errors) | ||
if extra_ignore: | ||
self.extra_type_ignores.append(n.lineno) | ||
self.type_ignores.add(n.lineno) | ||
else: | ||
target_type = None | ||
node = WithStmt([self.visit(i.context_expr) for i in n.items], | ||
|
@@ -767,7 +805,7 @@ def visit_AsyncWith(self, n: ast3.AsyncWith) -> WithStmt: | |
if n.type_comment is not None: | ||
extra_ignore, target_type = parse_type_comment(n.type_comment, n.lineno, self.errors) | ||
if extra_ignore: | ||
self.extra_type_ignores.append(n.lineno) | ||
self.type_ignores.add(n.lineno) | ||
else: | ||
target_type = None | ||
s = WithStmt([self.visit(i.context_expr) for i in n.items], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to
get_lineno
? Without context, the nameget_line
vaguely makes me think that it would get the source code line somehow.