Skip to content
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
10 changes: 5 additions & 5 deletions src/pytest_mypy_testing/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ class MypyTestItem:
lineno: int
end_lineno: int
expected_messages: List[Message]
func_node: Optional[ast.FunctionDef] = None
func_node: Optional[Union[ast.FunctionDef, ast.AsyncFunctionDef]] = None
marks: Set[str] = dataclasses.field(default_factory=lambda: set())
actual_messages: List[Message] = dataclasses.field(default_factory=lambda: [])

@classmethod
def from_ast_node(
cls,
func_node: ast.FunctionDef,
func_node: Union[ast.FunctionDef, ast.AsyncFunctionDef],
marks: Optional[Set[str]] = None,
unfiltered_messages: Optional[Iterable[Message]] = None,
) -> "MypyTestItem":
if not isinstance(func_node, ast.FunctionDef):
if not isinstance(func_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
raise ValueError(
f"Invalid func_node type: Got {type(func_node)}, "
f"expected {ast.FunctionDef}"
f"expected {ast.FunctionDef} or {ast.AsyncFunctionDef}"
)
lineno = func_node.lineno
end_lineno = getattr(func_node, "end_lineno", 0)
Expand Down Expand Up @@ -121,7 +121,7 @@ def parse_file(filename: Union[os.PathLike, str, pathlib.Path], config) -> MypyT
items: List[MypyTestItem] = []

for node in ast.iter_child_nodes(tree):
if not isinstance(node, ast.FunctionDef):
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
marks = _find_marks(node)
if "mypy_testing" in marks:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,22 @@ def test_mypy_bar():
monkeypatch.setattr(sys, "version_info", (3, 7, 5))
config = Mock(spec=Config)
parse_file(str(path), config)


def test_parse_async(tmp_path):
path = tmp_path / "test_async.mypy-testing"
path.write_text(dedent(
r"""
import pytest

@pytest.mark.mypy_testing
async def mypy_test_invalid_assginment():
foo = "abc"
foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
"""
))
config = Mock(spec=Config)
result = parse_file(str(path), config)
assert len(result.items) == 1
item = result.items[0]
assert item.name == "mypy_test_invalid_assginment"