From 2dfef3b3fc99a30faab1f97260871c5efcc56e9f Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Fri, 20 Dec 2024 10:51:47 +0000 Subject: [PATCH 1/3] Tighten regex for output assertion parsing The `severity` group was permissive enough to also capture parts of the message text if that contained any colons. Update it to only capture (ASCII) words, and update preceding groups to match at least 1 character. Fixes #155 --- .../tests/test-regex-assertions.yml | 15 +++++++++++++++ pytest_mypy_plugins/utils.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pytest_mypy_plugins/tests/test-regex-assertions.yml b/pytest_mypy_plugins/tests/test-regex-assertions.yml index c16e5d7..2e8806b 100644 --- a/pytest_mypy_plugins/tests/test-regex-assertions.yml +++ b/pytest_mypy_plugins/tests/test-regex-assertions.yml @@ -54,3 +54,18 @@ main: | a = 'hello' reveal_type(a) # NR: .*banana.* + +- case: regex_against_callable_comment + main: | + def foo(bar: str, ham: int = 42) -> set[str | int]: + return {bar, ham} + reveal_type(foo) # NR: Revealed type is "def \(bar: builtins\.str, ham: builtins\.int =\) -> .*" + +- case: regex_against_callable_out + regex: yes + main: | + def foo(bar: str, ham: int = 42) -> set[str | int]: + return {bar, ham} + reveal_type(foo) + out: | + main:3: note: Revealed type is "def \(bar: builtins\.str, ham: builtins\.int =\) -> .*" diff --git a/pytest_mypy_plugins/utils.py b/pytest_mypy_plugins/utils.py index 6eb3d1f..d755508 100644 --- a/pytest_mypy_plugins/utils.py +++ b/pytest_mypy_plugins/utils.py @@ -326,7 +326,7 @@ def extract_output_matchers_from_out(out: str, params: Mapping[str, Any], regex: lines = render_template(out, params).split("\n") for line in lines: match = re.search( - r"^(?P.*):(?P\d*): (?P.*):((?P\d+):)? (?P.*)$", line.strip() + r"^(?P.+):(?P\d+): (?P[A-Za-z]+):((?P\d+):)? (?P.*)$", line.strip() ) if match: if match.group("severity") == "E": From a4e661de82102759b1a91ff818034f28177537c5 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 21 Dec 2024 22:32:11 +0300 Subject: [PATCH 2/3] Update test-regex-assertions.yml --- pytest_mypy_plugins/tests/test-regex-assertions.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pytest_mypy_plugins/tests/test-regex-assertions.yml b/pytest_mypy_plugins/tests/test-regex-assertions.yml index 2e8806b..cc33520 100644 --- a/pytest_mypy_plugins/tests/test-regex-assertions.yml +++ b/pytest_mypy_plugins/tests/test-regex-assertions.yml @@ -57,14 +57,18 @@ - case: regex_against_callable_comment main: | - def foo(bar: str, ham: int = 42) -> set[str | int]: + from typing import Set, Union + + def foo(bar: str, ham: int = 42) -> Set[Union[str, int]]: return {bar, ham} reveal_type(foo) # NR: Revealed type is "def \(bar: builtins\.str, ham: builtins\.int =\) -> .*" - case: regex_against_callable_out regex: yes main: | - def foo(bar: str, ham: int = 42) -> set[str | int]: + from typing import Set, Union + + def foo(bar: str, ham: int = 42) -> Set[Union[str, int]]: return {bar, ham} reveal_type(foo) out: | From a957f91b16beadc31cfb7c57efdec1d86ef873cf Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 21 Dec 2024 22:33:53 +0300 Subject: [PATCH 3/3] Update test-regex-assertions.yml --- pytest_mypy_plugins/tests/test-regex-assertions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_mypy_plugins/tests/test-regex-assertions.yml b/pytest_mypy_plugins/tests/test-regex-assertions.yml index cc33520..eef8a70 100644 --- a/pytest_mypy_plugins/tests/test-regex-assertions.yml +++ b/pytest_mypy_plugins/tests/test-regex-assertions.yml @@ -72,4 +72,4 @@ return {bar, ham} reveal_type(foo) out: | - main:3: note: Revealed type is "def \(bar: builtins\.str, ham: builtins\.int =\) -> .*" + main:5: note: Revealed type is "def \(bar: builtins\.str, ham: builtins\.int =\) -> .*"