-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Issue 6757 - Improved diff output for similar strings. #7099
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
Conversation
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 don't have an opinion on this feature (yet?), but left some minor comments/suggestions.
Also, note that the "linting" CI check failed, due to formatting errors. You can fix it by running |
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 @piotrhm, great work! |
@bluetech would you like to take a second look? |
Also, @piotrhm meanwhile could you rebase and solve the conflict there? Thanks! |
Done. Thanks a lot @nicoddemus! |
@bluetech gentle ping for a final review. 👍 |
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 left some code comments. Some high level comments as well:
-
I'm confused about the desired behavior. The PR title says "Improve diff output for similar strings"; the changelog says "if the texts differ significantly (more than 30% of lines and more than 4 lines)", the actual code triggers the new behavior on
s.ratio() >= 0.30 and max(nlines_left, nlines_right) >= 5
(that is 30% similarity). Logically the changelog sounds right (I think), but the code does the opposite. -
I believe that using
screen_width
as done here is problematic because the explanation can be indented, e.g. in the new "drill down" mode and possibility other cases, in which case the=
s will overflow and look funny. Somehow the indentation needs to be account for (though I don't think the code is built for that), or a solution that doesn't rely on screen width should be used.
@@ -131,6 +132,8 @@ def isiterable(obj: Any) -> bool: | |||
def assertrepr_compare(config, op: str, left: Any, right: Any) -> Optional[List[str]]: | |||
"""Return specialised explanations for some operators/operands""" | |||
verbose = config.getoption("verbose") | |||
screen_width = config.get_terminal_reporter().screen_width |
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.
Instead of adding get_terminal_reporter()
to Config
and a screen_width
property, it will be better to use config.get_terminal_writer().fullwidth
.
@@ -168,29 +171,7 @@ def assertrepr_compare(config, op: str, left: Any, right: Any) -> Optional[List[ | |||
return [summary] + explanation | |||
|
|||
|
|||
def _compare_eq_any(left: Any, right: Any, verbose: int = 0) -> List[str]: |
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.
Just a note, moving code around makes it harder to review, because it breaks the diff view, so best to do it separately.
I guess this is a relevant comment for this PR 😄
|
||
def _text_header(header: str, screen_width: int, margin: int = 10) -> List[str]: | ||
hlength = len(header) | ||
lines = [ | ||
"=" * int((screen_width - hlength - margin) / 2) | ||
+ header | ||
+ "=" * int((screen_width - hlength - margin) / 2) | ||
] | ||
if screen_width % 2 != 0: | ||
lines[-1] += "=" | ||
|
||
return lines | ||
|
||
explanation += _text_header(" ACTUAL ", screen_width) | ||
explanation += list(left.split("\n")) | ||
explanation += _text_header(" EXPECTED ", screen_width) | ||
explanation += list(right.split("\n")) |
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.
IIUC str.center
can take care of most of this. Also can you explain why the margin (10) is needed?
def _text_header(header: str, screen_width: int, margin: int = 10) -> List[str]: | |
hlength = len(header) | |
lines = [ | |
"=" * int((screen_width - hlength - margin) / 2) | |
+ header | |
+ "=" * int((screen_width - hlength - margin) / 2) | |
] | |
if screen_width % 2 != 0: | |
lines[-1] += "=" | |
return lines | |
explanation += _text_header(" ACTUAL ", screen_width) | |
explanation += list(left.split("\n")) | |
explanation += _text_header(" EXPECTED ", screen_width) | |
explanation += list(right.split("\n")) | |
explanation += [ | |
" ACTUAL ".center(screen_width - 10, "="), | |
*left.split("\n"), | |
" EXPECTED ".center(screen_width - 10, "="), | |
*right.split("\n"), | |
] |
Hi @piotrhm, First of all we would like to thank you for your time and effort on working on this, the pytest team deeply appreciates it. We noticed it has been awhile since you have updated this PR, however. pytest is a high activity project, with many issues/PRs being opened daily, so it is hard for us maintainers to track which PRs are ready for merging, for review, or need more attention. So for those reasons we think it is best to close the PR for now, but with the only intention to cleanup our queue, it is by no means a rejection of your changes. We still encourage you to re-open this PR (it is just a click of a button away) when you are ready to get back to it. Again we appreciate your time for working on this, and hope you might get back to this at a later time! |
This pull request implements suggested approach for issue #6757. Visualization: