-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: Added script to enforce usage of match argument for tm.assert_produces_warning #59173
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
chaarvii
wants to merge
15
commits into
pandas-dev:main
Choose a base branch
from
chaarvii:enforce-match
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.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7f027ac
fix ruff errors
chaarvii bc1bf8c
Merge remote-tracking branch 'upstream/main' into enforce-match
chaarvii d74e92e
update comments
chaarvii 2562c79
fix typo
chaarvii c6cdc2d
exclude tm.assert_produces_warning(False)
chaarvii fde9825
update exclude list
chaarvii a5ea822
Merge remote-tracking branch 'upstream/main' into enforce-match
chaarvii 7c4caf2
update comments
chaarvii cdd6303
Update scripts/enforce_match_arg_in_assert_produces_warning.py
chaarvii 1bfe27e
as per reviews
chaarvii 9a2313d
Merge branch 'enforce-match' of https://github.com/chaarvii/pandas in…
chaarvii b745226
trigger pre-commit
chaarvii 87dd8c3
uncommit suggestion
chaarvii b700b71
fix sequence import
chaarvii 75a181b
Merge branch 'main' into enforce-match
chaarvii 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
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,88 @@ | ||
""" | ||
Enforce that all usages of tm.assert_produces_warning use | ||
the "match" argument. This will help ensure that users always see | ||
the correct warning message. | ||
|
||
tm.assert_produces_warning(None), tm.assert_produces_warning() | ||
and tm.assert_produces_warning(False) are excluded as no warning is | ||
produced. | ||
|
||
This is meant to be run as a pre-commit hook - to run it manually, you can do: | ||
|
||
pre-commit run enforce-match-arg-in-assert-produces-warning --all-files | ||
""" | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import ast | ||
import sys | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Sequence | ||
|
||
ERROR_MESSAGE = ( | ||
"{path}:{lineno}:{col_offset}: " | ||
'"match" argument missing in tm.assert_produces_warning' | ||
"\n" | ||
) | ||
|
||
|
||
class MatchArgForWarningsChecker(ast.NodeVisitor): | ||
def __init__(self) -> None: | ||
self.error_set = [] | ||
|
||
def visit_Call(self, node) -> None: | ||
if ( isinstance(node.func, ast.Attribute) and | ||
node.func.attr == "assert_produces_warning"): | ||
# only check for attribute function of class/module tm | ||
if ( isinstance(node.func.value, ast.Name) and | ||
node.func.value.id == "tm" ): | ||
# ignore tm.assert_produces_warning(None),tm.assert_produces_warning() | ||
# and tm.assert_produces_warning(False) | ||
if ( len(node.args) == 0 or | ||
(isinstance(node.args[0], ast.Constant) and | ||
( node.args[0].value is None or node.args[0].value is False))): | ||
return | ||
if not any(keyword.arg == "match" for keyword in node.keywords): | ||
self.error_set.append((node.lineno, node.col_offset)) | ||
|
||
|
||
# Returns true if a file fails the check | ||
def check_for_match_arg(content: str, filename: str) -> bool: | ||
tree = ast.parse(content) | ||
visitor = MatchArgForWarningsChecker() | ||
visitor.visit(tree) | ||
|
||
if len(visitor.error_set) == 0: | ||
return False | ||
|
||
for error in visitor.error_set: | ||
msg = ERROR_MESSAGE.format( | ||
lineno=error[0], | ||
col_offset=error[1], | ||
path=filename, | ||
) | ||
sys.stdout.write(msg) | ||
|
||
return True | ||
|
||
|
||
def main(argv: Sequence[str] | None = None) -> None: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("paths", nargs="*") | ||
|
||
args = parser.parse_args(argv) | ||
is_match_missing = False | ||
|
||
for filename in args.paths: | ||
with open(filename, encoding="utf-8") as fd: | ||
content = fd.read() | ||
is_match_missing = check_for_match_arg(content, filename) | is_match_missing | ||
|
||
if is_match_missing: | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.