Skip to content

bpo-36878: Allow extra text after # type: ignore comments #13238

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 3 commits into from
May 11, 2019
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
9 changes: 8 additions & 1 deletion Lib/test/test_type_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def foo():

def bar():
x = 1 # type: ignore

def baz():
pass # type: ignore[excuse]
pass # type: ignore=excuse
pass # type: ignore [excuse]
x = 1 # type: ignore whatever
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that this is super-important, but I would add two other typical scenarios:

# type: ignore=excuse  <- Pylint style
# type: ignore [excuse]  <- just in case

"""

# Test for long-form type-comments in arguments. A test function
Expand Down Expand Up @@ -266,7 +272,7 @@ def test_vardecl(self):

def test_ignores(self):
for tree in self.parse_all(ignores):
self.assertEqual([ti.lineno for ti in tree.type_ignores], [2, 5])
self.assertEqual([ti.lineno for ti in tree.type_ignores], [2, 5, 8, 9, 10, 11])
tree = self.classic_parse(ignores)
self.assertEqual(tree.type_ignores, [])

Expand Down Expand Up @@ -318,6 +324,7 @@ def check_both_ways(source):
check_both_ways("while True:\n continue # type: int\n")
check_both_ways("try: # type: int\n pass\nfinally:\n pass\n")
check_both_ways("try:\n pass\nfinally: # type: int\n pass\n")
check_both_ways("pass # type: ignorewhatever\n")

def test_func_type_input(self):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
When using `type_comments=True` in `ast.parse`, treat `# type: ignore` followed by
a non-alphanumeric character and then arbitrary text as a type ignore, instead of
requiring nothing but whitespace or another comment. This is to permit formations
such as `# type: ignore[E1000]`.
13 changes: 5 additions & 8 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,14 +1272,11 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)

type_start = p;

is_type_ignore = tok->cur >= p + 6 && memcmp(p, "ignore", 6) == 0;
p += 6;
while (is_type_ignore && p < tok->cur) {
if (*p == '#')
break;
is_type_ignore = is_type_ignore && (*p == ' ' || *p == '\t');
p++;
}
/* A TYPE_IGNORE is "type: ignore" followed by the end of the token
* or anything non-alphanumeric. */
is_type_ignore = (
tok->cur >= p + 6 && memcmp(p, "ignore", 6) == 0
&& !(tok->cur > p + 6 && isalnum(p[6])));

if (is_type_ignore) {
/* If this type ignore is the only thing on the line, consume the newline also. */
Expand Down