Skip to content

Commit

Permalink
Feature ignore-comment-errors notes (#15678)
Browse files Browse the repository at this point in the history
TODO: Figure out how to construct a unit test if the note message
includes the line text.
  • Loading branch information
NarvinSingh committed Jul 15, 2023
1 parent a61ab6b commit 494bae3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,18 @@ def strip_comment_from_line(text: str, target_lineno: int) -> str:
and TYPE_COMMENT_PATTERN.match(e.text)
):
stripped_source = strip_comment_from_line(source, e.lineno)
# TODO: log a note of what was stripped
print(f"{e.lineno}: Stripped comment from {e.text}")
errors.report(
e.lineno or -1,
e.offset or -1,
# TODO: It would be nicer if the message included the line text via
# `e.text.strip()`. However, I can't figure out how to make the unit
# test pass, because the # N: <expected_result> in the unit test must
# contain itself. If there was some partial matching mechanism for the
# unit test comment, then we could make this work.
"Ignored bad type comment",
severity="note",
code=codes.SYNTAX,
)
return None, stripped_source
raise
return ast, source
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ x = None # type: a : b # E: Syntax error in type comment "a : b"
[case testFastParseIgnoreTypeCommentSyntaxError]
# flags: --ignore-comment-errors

x: int = "1" # type: bad # E: Incompatible types in assignment (expression has type "str", variable has type "int")
y: str = 1 # type: bad # E: Incompatible types in assignment (expression has type "int", variable has type "str")
x: int = "1" # type: bad # E: Incompatible types in assignment (expression has type "str", variable has type "int") # N: Ignored bad type comment
y: str = 1 # type: bad # E: Incompatible types in assignment (expression has type "int", variable has type "str") # N: Ignored bad type comment

[case testFastParseInvalidTypeComment]

Expand Down

0 comments on commit 494bae3

Please sign in to comment.