-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[refactor] Start using match case in 'assertrepr_compare' #13762
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
Open
Pierre-Sassoulas
wants to merge
3
commits into
pytest-dev:main
Choose a base branch
from
Pierre-Sassoulas:match-case-goodies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−100
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from collections.abc import Set as AbstractSet | ||
from typing import Any | ||
|
||
from _pytest._io.saferepr import saferepr | ||
from _pytest.assertion._typing import _HighlightFunc | ||
|
||
|
||
def _set_one_sided_diff( | ||
posn: str, | ||
set1: AbstractSet[Any], | ||
set2: AbstractSet[Any], | ||
highlighter: _HighlightFunc, | ||
) -> list[str]: | ||
explanation = [] | ||
diff = set1 - set2 | ||
if diff: | ||
explanation.append(f"Extra items in the {posn} set:") | ||
for item in diff: | ||
explanation.append(highlighter(saferepr(item))) | ||
return explanation | ||
|
||
|
||
def _compare_eq_set( | ||
left: AbstractSet[Any], | ||
right: AbstractSet[Any], | ||
highlighter: _HighlightFunc, | ||
verbose: int = 0, | ||
) -> list[str]: | ||
explanation = [] | ||
explanation.extend(_set_one_sided_diff("left", left, right, highlighter)) | ||
explanation.extend(_set_one_sided_diff("right", right, left, highlighter)) | ||
return explanation | ||
|
||
|
||
def _compare_gt_set( | ||
left: AbstractSet[Any], | ||
right: AbstractSet[Any], | ||
highlighter: _HighlightFunc, | ||
verbose: int = 0, | ||
) -> list[str]: | ||
explanation = _compare_gte_set(left, right, highlighter) | ||
if not explanation: | ||
return ["Both sets are equal"] | ||
return explanation | ||
|
||
|
||
def _compare_lt_set( | ||
left: AbstractSet[Any], | ||
right: AbstractSet[Any], | ||
highlighter: _HighlightFunc, | ||
verbose: int = 0, | ||
) -> list[str]: | ||
explanation = _compare_lte_set(left, right, highlighter) | ||
if not explanation: | ||
return ["Both sets are equal"] | ||
return explanation | ||
|
||
|
||
def _compare_gte_set( | ||
left: AbstractSet[Any], | ||
right: AbstractSet[Any], | ||
highlighter: _HighlightFunc, | ||
verbose: int = 0, | ||
) -> list[str]: | ||
return _set_one_sided_diff("right", right, left, highlighter) | ||
|
||
|
||
def _compare_lte_set( | ||
left: AbstractSet[Any], | ||
right: AbstractSet[Any], | ||
highlighter: _HighlightFunc, | ||
verbose: int = 0, | ||
) -> list[str]: | ||
return _set_one_sided_diff("left", left, right, highlighter) | ||
|
||
|
||
SetComparisonFunction = dict[ | ||
str, | ||
Callable[ | ||
[AbstractSet[Any], AbstractSet[Any], _HighlightFunc, int], | ||
list[str], | ||
], | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
from typing import Protocol | ||
|
||
|
||
class _HighlightFunc(Protocol): # noqa: PYI046 | ||
def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") -> str: | ||
"""Apply highlighting to the given source.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This duplicates the
isset
logic, while that seems unlikely to change it seems conceptually wrong to duplicate since the idea is to have a single place which defines what a set is. WDYT?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 agree, I have a follow up:
But this require a prior refactor because there's additional information that are added only for iterable in _compare_eq_any. Actually finding the right order of refactors for optimal reviewing ease was a little tricky and I decided to open this one instead of entering rebase hell.
The goal would be to remove all the isx function once the match case structure make them redundant because the complexity can be contained in the match case itself while still being readable.
Uh oh!
There was an error while loading. Please reload this page.
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.
Also wondering if merging the entire
_compare_eq_any
function in the match case ofassertrepr_compare
is going to make it too big. But it's probably worth it because the whole logic for the assert_repr decision tree is easy to locate.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.
Your follow up makes sense to me (moving the type-specific checking before the generic
_ == _
case).I understand your comment about rebase hell, though it would be helpful to see the final state if you have it, even if it's one huge commit blob :)
BTW, regarding
isset
, I wonder if we should usecollections.abc.Set
(immutable set operations, includes set and frozenset, and possibly user types which implement the interface) instead ofset | frozenset
. I haven't checked if it makes sense.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 pushed the concept a little and
_compare_eq_any
is recursive through_compare_eq_cls
(launched on each element inside iterables) so what I envisioned is not possible. I'm going to make a second switch to match/case in_compare_eq_any
and keep them separated. Here's a messy unfinished glob with what I currently have: https://github.com/Pierre-Sassoulas/pytest/pull/2/files#diff-0e1605330bae69222e30a50a4c573ae5eadb529e8f64662c0c9134e431af9d4aR27-R120