From fbee5a5aad2467f44481d0a87692e9b4c1f5c35d Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Thu, 18 Aug 2022 20:30:53 +0500 Subject: [PATCH 1/2] support async def tests --- src/pytest_mypy_testing/parser.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pytest_mypy_testing/parser.py b/src/pytest_mypy_testing/parser.py index a38ea01..78c2b6a 100644 --- a/src/pytest_mypy_testing/parser.py +++ b/src/pytest_mypy_testing/parser.py @@ -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) @@ -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: From 8725ae8d7baa0f92d988385afc9c5e8714700d31 Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Thu, 18 Aug 2022 20:47:02 +0500 Subject: [PATCH 2/2] test for async def parsing --- tests/test_parser.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_parser.py b/tests/test_parser.py index 21ea37f..764c1a5 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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"