Skip to content

Commit 7f027ac

Browse files
committed
fix ruff errors
1 parent a89f208 commit 7f027ac

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

.pre-commit-config.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,69 @@ repos:
301301
language: python
302302
files: ^doc/source/whatsnew/v
303303
exclude: ^doc/source/whatsnew/v(0|1|2\.0\.0)
304+
- id: enforce-match-arg-in-assert-produces-warning
305+
name: Enforce the usage of match arg
306+
entry: python scripts/enforce_match_arg_in_assert_produces_warning.py
307+
language: python
308+
files: ^pandas/tests
309+
exclude: |
310+
(?x)
311+
^(
312+
pandas/tests/computation/test_eval.py|
313+
pandas/tests/frame/test_query_eval.py|
314+
pandas/tests/frame/methods/test_drop.py|
315+
pandas/tests/plotting/test_datetimelike.py|
316+
pandas/tests/plotting/frame/test_frame_color.py|
317+
pandas/tests/plotting/test_hist_method.py|
318+
pandas/tests/plotting/test_boxplot_method.py|
319+
pandas/tests/util/test_deprecate_kwarg.py|
320+
pandas/tests/util/test_deprecate.py|
321+
pandas/tests/util/test_assert_produces_warning.py|
322+
pandas/tests/util/test_deprecate_nonkeyword_arguments.py|
323+
pandas/tests/api/test_types.py|
324+
pandas/tests/api/test_api.py|
325+
pandas/tests/strings/test_find_replace.py|
326+
pandas/tests/arithmetic/test_datetime64.py|
327+
pandas/tests/arithmetic/test_timedelta64.py|
328+
pandas/tests/arithmetic/test_period.py|
329+
pandas/tests/tseries/offsets/test_offsets.py|
330+
pandas/tests/indexes/datetimes/methods/test_shift.py|
331+
pandas/tests/extension/test_period.py|
332+
pandas/tests/io/pytables/test_retain_attributes.py|
333+
pandas/tests/scalar/timestamp/methods/test_round.py|
334+
pandas/tests/scalar/timestamp/methods/test_to_pydatetime.py|
335+
pandas/tests/io/test_sql.py|
336+
pandas/tests/reshape/test_pivot.py|
337+
pandas/tests/plotting/frame/test_hist_box_by.py|
338+
pandas/tests/frame/methods/test_reindex_like.py|
339+
pandas/tests/plotting/test_series.py|
340+
pandas/tests/io/parser/test_unsupported.py|
341+
pandas/tests/series/accessors/test_cat_accessor.py|
342+
pandas/tests/test_algos.py|
343+
pandas/tests/indexing/multiindex/test_multiindex.py|
344+
pandas/tests/indexes/multi/test_drop.py|
345+
pandas/tests/plotting/frame/test_frame.py|
346+
pandas/tests/window/test_numba.py|
347+
pandas/tests/reshape/test_cut.py|
348+
pandas/tests/apply/test_str.py|
349+
pandas/tests/indexes/multi/test_indexing.py|
350+
pandas/tests/io/pytables/test_store.py|
351+
pandas/tests/io/pytables/test_put.py|
352+
pandas/tests/io/pytables/test_round_trip.py|
353+
pandas/tests/io/excel/test_readers.py|
354+
pandas/tests/resample/test_datetime_index.py|
355+
pandas/tests/io/parser/test_c_parser_only.py|
356+
pandas/tests/io/test_stata.py|
357+
pandas/tests/plotting/test_misc.py|
358+
pandas/tests/series/methods/test_equals.py|
359+
pandas/tests/frame/test_block_internals.py|
360+
pandas/tests/indexes/multi/test_sorting.py|
361+
pandas/tests/series/methods/test_reindex_like.py|
362+
pandas/tests/extension/test_sparse.py|
363+
pandas/tests/indexes/test_common.py|
364+
pandas/tests/indexes/datetimes/methods/test_round.py|
365+
pandas/tests/indexing/multiindex/test_loc.py|
366+
pandas/tests/frame/indexing/test_insert.py|
367+
pandas/tests/groupby/test_groupby.py|
368+
)$
369+
types: [python]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 30th June - spent 1hr 20min
2+
# 1st July - spent close to 1hr 30min
3+
4+
"""
5+
6+
Enforce that all usages of tms.assert_produces_warning use
7+
the "match" argument. This will help ensure that users always see
8+
the correct warning message.
9+
10+
tms.assert_produces_warning(None) is excluded as no warning is
11+
produced.
12+
13+
"""
14+
15+
16+
import argparse
17+
import ast
18+
from collections.abc import Sequence
19+
import sys
20+
21+
ERROR_MESSAGE = (
22+
"{path}:{lineno}:{col_offset}: "
23+
'"match" argument missing in tm.assert_produces_warning'
24+
"\n"
25+
)
26+
27+
28+
class MatchArgForWarningsChecker(ast.NodeVisitor):
29+
def __init__(self) -> None:
30+
self.error_set = []
31+
32+
def visit_Call(self, node) -> None:
33+
if ( isinstance(node.func, ast.Attribute) and
34+
node.func.attr == "assert_produces_warning"):
35+
# only check for attribute function of class/module tm
36+
if ( isinstance(node.func.value, ast.Name) and
37+
node.func.value.id == "tm" ):
38+
# ignore tms.assert_produces_warning(None)/tms.assert_produces_warning()
39+
if ( len(node.args) == 0 or
40+
(isinstance(node.args[0], ast.Constant) and
41+
node.args[0].value is None) ):
42+
return
43+
if not any(keyword.arg == "match" for keyword in node.keywords):
44+
self.error_set.append((node.lineno, node.col_offset))
45+
46+
47+
# Returns true if a file fails the check
48+
def check_for_match_arg(content: str, filename: str) -> bool:
49+
tree = ast.parse(content)
50+
visitor = MatchArgForWarningsChecker()
51+
visitor.visit(tree)
52+
53+
if len(visitor.error_set) == 0:
54+
return False
55+
56+
for error in visitor.error_set:
57+
msg = ERROR_MESSAGE.format(
58+
lineno=error[0],
59+
col_offset=error[1],
60+
path=filename,
61+
)
62+
sys.stdout.write(msg)
63+
64+
return True
65+
66+
67+
def main(argv: Sequence[str] | None = None) -> None:
68+
parser = argparse.ArgumentParser()
69+
parser.add_argument("paths", nargs="*")
70+
71+
args = parser.parse_args(argv)
72+
isMatchMissing = False
73+
74+
for filename in args.paths:
75+
with open(filename, encoding="utf-8") as fd:
76+
content = fd.read()
77+
isMatchMissing = check_for_match_arg(content, filename) | isMatchMissing
78+
79+
if isMatchMissing:
80+
sys.exit(1)
81+
82+
83+
if __name__ == "__main__":
84+
main()

0 commit comments

Comments
 (0)