-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Added parsing of ignore annotation for import statements. #503
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Classes for representing mypy annotations""" | ||
|
||
from typing import List | ||
|
||
import mypy.nodes | ||
|
||
|
||
class Annotation(mypy.nodes.Context): | ||
"""Abstract base class for all annotations.""" | ||
|
||
def __init__(self, line: int = -1) -> None: | ||
self.line = line | ||
|
||
|
||
class IgnoreAnnotation(Annotation): | ||
"""The 'mypy: ignore' annotation""" | ||
|
||
def __init__(self, line: int = -1) -> None: | ||
super().__init__(line) | ||
|
||
def get_line(self) -> int: | ||
return self.line |
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 |
---|---|---|
|
@@ -33,6 +33,8 @@ | |
from mypy.parsetype import ( | ||
parse_type, parse_types, parse_signature, TypeParseError | ||
) | ||
from mypy.annotations import Annotation, IgnoreAnnotation | ||
from mypy.parseannotation import parse_annotation, AnnotationParseError | ||
|
||
|
||
precedence = { | ||
|
@@ -162,8 +164,10 @@ def parse_import(self) -> Import: | |
break | ||
commas.append(self.expect(',')) | ||
br = self.expect_break() | ||
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. Should we also support this for |
||
annotation = self.parse_annotation_comment(br) | ||
node = Import(ids) | ||
self.imports.append(node) | ||
if not isinstance(annotation, IgnoreAnnotation): | ||
self.imports.append(node) | ||
self.set_repr(node, noderepr.ImportRepr(import_tok, id_toks, as_names, | ||
commas, br)) | ||
return node | ||
|
@@ -208,7 +212,9 @@ def parse_import_from(self) -> Node: | |
if node is None: | ||
node = ImportFrom(name, targets) | ||
br = self.expect_break() | ||
self.imports.append(node) | ||
annotation = self.parse_annotation_comment(br) | ||
if not isinstance(annotation, IgnoreAnnotation): | ||
self.imports.append(node) | ||
# TODO: Fix representation if there is a custom typing module import. | ||
self.set_repr(node, noderepr.ImportFromRepr( | ||
from_tok, components, import_tok, lparen, name_toks, rparen, br)) | ||
|
@@ -1683,7 +1689,7 @@ def parse_type(self) -> Type: | |
raise ParseError() | ||
return typ | ||
|
||
annotation_prefix_re = re.compile(r'#\s*type:') | ||
type_annotation_prefix_re = re.compile(r'#\s*type:') | ||
|
||
def parse_type_comment(self, token: Token, signature: bool) -> Type: | ||
"""Parse a '# type: ...' annotation. | ||
|
@@ -1692,7 +1698,7 @@ def parse_type_comment(self, token: Token, signature: bool) -> Type: | |
a type signature of form (...) -> t. | ||
""" | ||
whitespace_or_comments = token.rep().strip() | ||
if self.annotation_prefix_re.match(whitespace_or_comments): | ||
if self.type_annotation_prefix_re.match(whitespace_or_comments): | ||
type_as_str = whitespace_or_comments.split(':', 1)[1].strip() | ||
tokens = lex.lex(type_as_str, token.line) | ||
if len(tokens) < 2: | ||
|
@@ -1714,6 +1720,30 @@ def parse_type_comment(self, token: Token, signature: bool) -> Type: | |
else: | ||
return None | ||
|
||
annotation_prefix_re = re.compile(r'#\s*mypy:') | ||
|
||
def parse_annotation_comment(self, token: Token) -> Annotation: | ||
"""Parse a '# mypy: ...' annotation""" | ||
whitespace_or_comments = token.rep().strip() | ||
if self.annotation_prefix_re.match(whitespace_or_comments): | ||
annotation_as_str = whitespace_or_comments.split(':', 1)[1].strip() | ||
tokens = lex.lex(annotation_as_str, token.line) | ||
if len(tokens) < 2: | ||
# Empty annotation (only Eof token) | ||
self.errors.report(token.line, 'Empty annotation') | ||
return None | ||
try: | ||
annotation, index = parse_annotation(tokens, 0) | ||
except AnnotationParseError as e: | ||
self.parse_error_at(e.token, skip = False) | ||
return None | ||
if index < len(tokens) - 2: | ||
self.parse_error_at(tokens[index], skip=False) | ||
return None | ||
return annotation | ||
else: | ||
return None | ||
|
||
# Representation management | ||
|
||
def set_repr(self, node: Node, repr: Any) -> None: | ||
|
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Annotation parse""" | ||
|
||
from typing import List, Tuple | ||
|
||
from mypy.lex import Token | ||
from mypy import nodes | ||
from mypy.annotations import Annotation, IgnoreAnnotation | ||
|
||
|
||
class AnnotationParseError(Exception): | ||
def __init__(self, token: Token, index: int) -> None: | ||
super().__init__() | ||
self.token = token | ||
self.index = index | ||
|
||
|
||
def parse_annotation(tok: List[Token], index: int) -> Tuple[Annotation, int]: | ||
"""Parse an annotation | ||
""" | ||
|
||
p = AnnotationParser(tok, index) | ||
return p.parse_annotation(), p.index() | ||
|
||
class AnnotationParser: | ||
def __init__(self, tok: List[Token], ind: int) -> None: | ||
self.tok = tok | ||
self.ind = ind | ||
|
||
def index(self) -> int: | ||
return self.ind | ||
|
||
def parse_annotation(self) -> Annotation: | ||
"""Parse an annotation.""" | ||
t = self.current_token() | ||
if t.string == 'ignore': | ||
self.skip() | ||
return IgnoreAnnotation(t.line) | ||
else: | ||
self.parse_error() | ||
|
||
# Helpers: | ||
|
||
def skip(self) -> Token: | ||
self.ind += 1 | ||
return self.tok[self.ind - 1] | ||
|
||
def current_token(self) -> Token: | ||
return self.tok[self.ind] | ||
|
||
def parse_error(self) -> None: | ||
raise AnnotationParseError(self.tok[self.ind], self.ind) |
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- Test cases for annotation parser. | ||
|
||
[case testIgnoreAnnotation] | ||
import xyz # mypy: ignore | ||
[out] | ||
MypyFile:1( | ||
Import:1(xyz : xyz)) | ||
|
||
[case testEmptyAnnotation] | ||
import xyz # mypy: | ||
[out] | ||
<input>, line 1: Empty annotation | ||
|
||
[case testInvalidAnnotation] | ||
import xyz # mypy: xxx | ||
[out] | ||
<input>, line 1: Parse error before "xxx" |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Missing empty line
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.
Good to see that I'm not the only one who cares about theses things 😄