Skip to content

Proposal for supporting call-overload errors #45

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ the file:
* `# F: <msg>` - we expect a mypy fatal error message
* `# R: <msg>` - we expect a mypy note message `Revealed type is
'<msg>'`. This is useful to easily check `reveal_type` output:

```python
@pytest.mark.mypy_testing
def mypy_use_reveal_type():
reveal_type(123) # N: Revealed type is 'Literal[123]?'
reveal_type(456) # R: Literal[456]?
```

* `# O: <msg>` - we expect a mypy error message and additionally suppress any
notes on the same line. This is useful to test for errors such as
`call-overload` where mypy provides extra details in notes along with the error.

## mypy Error Code Matching

The algorithm matching messages parses mypy error code both in the
Expand Down
19 changes: 14 additions & 5 deletions src/pytest_mypy_testing/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __repr__(self) -> str:
"W": Severity.WARNING,
"E": Severity.ERROR,
"F": Severity.FATAL,
"O": Severity.ERROR,
}

_COMMENT_MESSAGES = frozenset(
Expand All @@ -64,6 +65,7 @@ class Message:
message: str = ""
revealed_type: Optional[str] = None
error_code: Optional[str] = None
suppress_notes: bool = False

TupleType = Tuple[
str, int, Optional[int], Severity, str, Optional[str], Optional[str]
Expand All @@ -73,7 +75,7 @@ class Message:

COMMENT_RE = re.compile(
r"^(?:# *type: *ignore *)?(?:# *)?"
r"(?P<severity>[RENW]):"
r"(?P<severity>[RENWO]):"
r"((?P<colno>\d+):)?"
r" *"
r"(?P<message_and_error_code>[^#]*)"
Expand Down Expand Up @@ -239,9 +241,11 @@ def from_comment(
"""Create message object from Python *comment*.

>>> Message.from_comment("foo.py", 1, "R: foo")
Message(filename='foo.py', lineno=1, colno=None, severity=Severity.NOTE, message="Revealed type is 'foo'", revealed_type='foo', error_code=None)
Message(filename='foo.py', lineno=1, colno=None, severity=Severity.NOTE, message="Revealed type is 'foo'", revealed_type='foo', error_code=None, suppress_notes=False)
>>> Message.from_comment("foo.py", 1, "E: [assignment]")
Message(filename='foo.py', lineno=1, colno=None, severity=Severity.ERROR, message='', revealed_type=None, error_code='assignment')
Message(filename='foo.py', lineno=1, colno=None, severity=Severity.ERROR, message='', revealed_type=None, error_code='assignment', suppress_notes=False)
>>> Message.from_comment("foo.py", 1, "O: [call-overload]")
Message(filename='foo.py', lineno=1, colno=None, severity=Severity.ERROR, message='', revealed_type=None, error_code='call-overload', suppress_notes=True)
"""
m = cls.COMMENT_RE.match(comment.strip())
if not m:
Expand All @@ -250,11 +254,15 @@ def from_comment(
message, error_code = cls.__split_message_and_error_code(
m.group("message_and_error_code")
)

suppress_notes = False
revealed_type = None
if m.group("severity") == "R":
revealed_type = message
message = "Revealed type is {!r}".format(message)
else:
revealed_type = None
elif m.group("severity") == "O":
suppress_notes = True

return Message(
str(filename),
lineno=lineno,
Expand All @@ -263,6 +271,7 @@ def from_comment(
message=message,
revealed_type=revealed_type,
error_code=error_code,
suppress_notes=suppress_notes,
)

@classmethod
Expand Down
11 changes: 11 additions & 0 deletions src/pytest_mypy_testing/output_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ def _chunk_to_dict(chunk: Sequence[Message]) -> Dict[int, List[Message]]:

errors: List[OutputMismatch] = []

# If an expected message has specified to suppress notes on a line, then
# drop them before performing the comparison.
suppress_notes_lines = {
msg.lineno for msg in expected_messages if msg.suppress_notes
}
actual_messages = [
msg
for msg in actual_messages
if msg.lineno not in suppress_notes_lines or msg.severity != Severity.NOTE
]

for a_chunk, b_chunk in iter_msg_seq_diff_chunks(
actual_messages, expected_messages
):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_basics.mypy-testing
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: CC0-1.0

import pytest
import dataclasses


@pytest.mark.mypy_testing
Expand Down Expand Up @@ -97,3 +98,11 @@ def mypy_test_xfail_missing_note():
@pytest.mark.xfail
def mypy_test_xfail_unexpected_note():
reveal_type([]) # unexpected message


@pytest.mark.mypy_testing
def mypy_test_suppress_notes():
# Check the use of the "O" severity
dataclasses.field(default=123, default_factory=lambda: 456) # O: [call-overload]
# Check that notes on other lines are not suppressed
reveal_type(123) # N: Revealed type is 'Literal[123]?'