Skip to content

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

Closed
wants to merge 18 commits into from

Conversation

piotrhm
Copy link
Contributor

@piotrhm piotrhm commented Apr 19, 2020

This pull request implements suggested approach for issue #6757. Visualization:

Screenshot from 2020-04-19 17-46-18

Copy link
Member

@bluetech bluetech left a 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.

@bluetech
Copy link
Member

Also, note that the "linting" CI check failed, due to formatting errors. You can fix it by running tox -e linting (or running black manually). You also check the logs and apply the necessary changes manually.

Copy link
Member

@nicoddemus nicoddemus left a 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 for working on this, we appreciate it.

Besides the comments left by @bluetech and me, could you please also add a test for this functionality? I like the cow example. 😁

@nicoddemus
Copy link
Member

nicoddemus commented May 4, 2020

Thanks a lot @piotrhm, great work!

@nicoddemus nicoddemus requested a review from bluetech June 2, 2020 15:18
@nicoddemus
Copy link
Member

@bluetech would you like to take a second look?

@nicoddemus
Copy link
Member

Also, @piotrhm meanwhile could you rebase and solve the conflict there? Thanks!

@piotrhm
Copy link
Contributor Author

piotrhm commented Jun 11, 2020

Done. Thanks a lot @nicoddemus!

@nicoddemus
Copy link
Member

@bluetech gentle ping for a final review. 👍

Copy link
Member

@bluetech bluetech left a 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
Copy link
Member

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]:
Copy link
Member

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 😄

Comment on lines +229 to +245

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"))
Copy link
Member

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?

Suggested change
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"),
]

@gnikonorov
Copy link
Member

Hi @piotrhm . If this PR is meant to close an issue, could you please update the description as described here so that the linked issue may autoclose?

@bluetech
Copy link
Member

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!

@bluetech bluetech closed this Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants