Skip to content

Check syntax errors in "# type: ignore[code]" comments #7460

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 11 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
JukkaL committed Sep 4, 2019
commit 83b8626b4f6680e946441e6723c7e66059efe3ea
16 changes: 11 additions & 5 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def ast3_parse(source: Union[str, bytes], filename: str, mode: str,

TYPE_COMMENT_SYNTAX_ERROR = 'syntax error in type comment' # type: Final

TYPE_IGNORE_PATTERN = re.compile(r'[^#]*#\s*type:\s*ignore\s*(\[[^[#]*\]\s*)?($|#)')
TYPE_IGNORE_PATTERN = re.compile(r'[^#]*#\s*type:\s*ignore\s*\b(.*)')


def parse(source: Union[str, bytes],
Expand Down Expand Up @@ -208,10 +208,16 @@ def parse_type_comment(type_comment: str,
if extra_ignore:
# Typeshed has a non-optional return type for group!
tag = cast(Any, extra_ignore).group(1) # type: Optional[str]
ignored = parse_type_ignore_tag(tag) # type: Optional[List[str]]
if not tag.strip().startswith('#'):
ignored = parse_type_ignore_tag(tag) # type: Optional[List[str]]
else:
ignored = [] # Ignore everything
if ignored is None:
errors.report(line, e.offset, 'Invalid "# type: ignore" comment',
code=codes.SYNTAX)
if errors is not None:
errors.report(line, column, 'Invalid "type: ignore" comment',
code=codes.SYNTAX)
else:
raise SyntaxError
else:
ignored = None
assert isinstance(typ, ast3_Expression)
Expand Down Expand Up @@ -459,7 +465,7 @@ def translate_module_id(self, id: str) -> str:
def visit_Module(self, mod: ast3.Module) -> MypyFile:
self.type_ignores = {}
for ti in mod.type_ignores:
parsed = parse_type_ignore_tag(ti.tag)
parsed = parse_type_ignore_tag(ti.tag) # type: ignore[attr-defined]
if parsed is not None:
self.type_ignores[ti.lineno] = parsed
else:
Expand Down
12 changes: 9 additions & 3 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def translate_module_id(self, id: str) -> str:
def visit_Module(self, mod: ast27.Module) -> MypyFile:
self.type_ignores = {}
for ti in mod.type_ignores:
parsed = parse_type_ignore_tag(ti.tag)
parsed = parse_type_ignore_tag(ti.tag) # type: ignore[attr-defined]
if parsed is not None:
self.type_ignores[ti.lineno] = parsed
else:
Expand Down Expand Up @@ -557,8 +557,14 @@ def get_type(self,
extra_ignore = TYPE_IGNORE_PATTERN.match(comment)
if extra_ignore:
tag = cast(Any, extra_ignore).group(1) # type: Optional[str]
ignored = parse_type_ignore_tag(tag)
self.type_ignores[converter.line] = ignored
if not tag.strip().startswith('#'):
ignored = parse_type_ignore_tag(tag)
else:
ignored = []
if ignored is None:
self.fail('Invalid "type: ignore" comment', converter.line, -1)
else:
self.type_ignores[converter.line] = ignored
return typ
return None

Expand Down
27 changes: 24 additions & 3 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,43 @@ import nostub # type: ignore[foo] [bar] # E: Invalid "type: ignore" comment [s

x = 0 # type: ignore[ # E: Invalid "type: ignore" comment [syntax]

def f(x, # type: int # type: ignore[ # E: Invalid "type: ignore" comment [syntax]
):
# type: (...) -> None
pass

[case testErrorCodeBadIgnoreNoExtraComment]
# Omit the E: ... comments, as they affect parsing
import nostub # type: ignore xyz
import nostub # type: ignore[xyz
import nostub # type: ignore[xyz][xyz]
x = 0 # type: ignore[
def f(x, # type: int # type: ignore[
):
# type: (...) -> None
pass
[out]
main:2: error: Invalid "type: ignore" comment [syntax]
main:3: error: Invalid "type: ignore" comment [syntax]
main:4: error: Invalid "type: ignore" comment [syntax]
main:5: error: Invalid "type: ignore" comment [syntax]
main:6: error: Invalid "type: ignore" comment [syntax]

[case testErrorCodeBadIgnore_python2]
import nostub # type: ignore xyz # E: Invalid "type: ignore" comment [syntax]
import nostub # type: ignore[xyz # E: Invalid "type: ignore" comment [syntax]
import nostub # type: ignore[xyz][xyz] # E: Invalid "type: ignore" comment [syntax]
import nostub # type: ignore xyz
import nostub # type: ignore[xyz # Comment [x]
import nostub # type: ignore[xyz][xyz]
x = 0 # type: ignore[
def f(x, # type: int # type: ignore[
):
# type: (...) -> None
pass
[out]
main:1: error: Invalid "type: ignore" comment [syntax]
main:2: error: Invalid "type: ignore" comment [syntax]
main:3: error: Invalid "type: ignore" comment [syntax]
main:4: error: Invalid "type: ignore" comment [syntax]
main:5: error: Invalid "type: ignore" comment [syntax]

[case testErrorCodeArgKindAndCount]
def f(x: int) -> None: pass # N: "f" defined here
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy