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
Refactor and fix assert_expected_matched_actual #65
Refactor and fix assert_expected_matched_actual #65
Conversation
This PR: - Refactors assert_expected_matched_actual function to avoid repeated matching between expected and actual output - Fixes typeddjango#63, typeddjango#64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the quick fix! Can you please add a testcase that was failing for you to our suite?
Sure, but I'll need some guidance here. All the cases covered here (refactoring aside) are suppose to fail, so we cannot simply add these to |
I guess we need something like |
@sobolevn I am going to convert it to draft. While bringing back leading / trailing indicator I detected some discrepancies that might require further work. Specifically I was looking at It seems like any preceding mismatch will actually brake all the following matches. Let's say I have following case: - case: break_following
main: |
reveal_type(1 + 1)
reveal_type(1 + 1) # N: Revealed type is "builtins.int"
reveal_type(1 + 1) # N: Revealed type is "builtins.int"
reveal_type(1 + 1) # N: Revealed type is "builtins.int"
reveal_type(1 + 1) # N: Revealed type is "builtins.int" While testing,
which is rather counter-intuitive, given that all expectations, but the first one, are satisfied. WDYT? |
After this patch it would be
but it feels like we would actually want something around these lines:
This also escalates, when we have matching blocks interleaved with failures (lets say we copy |
This looks correct to me: #65 (comment)
I need more examples 🙂 |
Sorry, let me try to explain it better. Let's assume that I added simple patch to current master diff --git a/pytest_mypy_plugins/utils.py b/pytest_mypy_plugins/utils.py
index dd8db72..b5f85a0 100644
--- a/pytest_mypy_plugins/utils.py
+++ b/pytest_mypy_plugins/utils.py
@@ -251,7 +251,7 @@ def assert_expected_matched_actual(expected: List[OutputMatcher], actual: List[s
if i >= len(actual) or not expected[i].matches(actual[i]):
if first_diff < 0:
first_diff = i
- error_message += " {:<45} (diff)".format(expected[i])
+ error_message += " {:<45} (diff)".format(str(expected[i]))
else:
e = expected[i]
error_message += " " + str(e)[:width] and I have a test case like this - case: break_following_2
main: |
reveal_type(1 + 1)
reveal_type(1.0 + 2.0) # N: Revealed type is "builtins.float"
reveal_type("foo" + "bar") # N: Revealed type is "builtins.str" When I run tests I see:
If you analyze the test case, you'll see that actual state is like this:
however alignment message
clearly shows that we start with comparing line 2 of expected and line 1 of actual. This escalates to all the following lines and probably gets worse with multi-line messages (I wanted to investigate that, hence #66). I am aware that this is consistent with behavior of the internal mypy test suite, which returns
for equivalent input, but it seems a bit counter-intuitive. |
@zero323 thanks a lot for this great explanation! 👍 Let's fix the initial bug first. |
Fair enough :) I've updated tests and PR description with up-to-date output. |
@zero323 just a quick thought: maybe we can write a couple of regular python unit-tests to make sure this works? |
Agreed. I was just thinking how to add tests for #66 and realized that run on YAML samples is not going to cut it. And here, we're interested as much about failure itself, as the actual output. Two questions:
|
Plain functions are the best! ⭐
I think that we should test the output, since it is the root issue. |
"""Invalid output: """, | ||
"""Actual:""", | ||
""" main:2: note: Revealed type is "Literal['foo']?" (diff)""", | ||
"""Expected:""", | ||
""" (empty)""", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if that's the best way of showing the result in such case. Maybe something around these lines
Invalid output:
Actual:
...
main:2: note: Revealed type is "Literal['foo']?" (diff)
Expected:
...
(empty)
would be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like this? https://github.com/wemake-services/wemake-python-styleguide/tree/master/tests/test_formatter/snapshots
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually thinking about the output for this specific test, where match is followed by failure, that we didn't anticipate.
If I understand you correctly, you think about redesigning the test itself. Is that right? I glanced over linked code and snapshottest
examples, but I am not sure if I see the advantage here. Let me sleep on that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand you correctly, you think about redesigning the test itself. Is that right? I glanced over linked code and
snapshottest
examples, but I am not sure if I see the advantage here. Let me sleep on that :)
So I took another look at this, but still don't see it (to be honest, parsing non-trivial snapshot keys hurts my brain, so I am probably biased) ‒ the arguments used here are probably to complex to be used directly, and to simple to move to files and justify the indirection (for example, not being able to just eyeball the test to understand the expectations).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks @sobolevn! |
This PR:
matching between expected and actual output
The following test file:
has been used to check for expected failures and gives output as shown below