Skip to content

Commit f725c1e

Browse files
authored
STY: Consolidate & add pre-commit checks (#54047)
* STY: Consolidate & add pre-commit checks * Remove & consolidate pre-commit checks
1 parent 51c6e79 commit f725c1e

File tree

3 files changed

+47
-87
lines changed

3 files changed

+47
-87
lines changed

.pre-commit-config.yaml

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@ repos:
4646
- repo: https://github.com/pre-commit/pre-commit-hooks
4747
rev: v4.4.0
4848
hooks:
49+
- id: check-ast
50+
- id: check-case-conflict
51+
- id: check-toml
52+
- id: check-xml
53+
- id: check-yaml
54+
exclude: ^ci/meta.yaml$
4955
- id: debug-statements
5056
- id: end-of-file-fixer
5157
exclude: \.txt$
52-
stages: [commit, merge-commit, push, prepare-commit-msg, commit-msg,
53-
post-checkout, post-commit, post-merge, post-rewrite]
58+
- id: mixed-line-ending
59+
args: [--fix=auto]
60+
exclude: ^pandas/tests/io/parser/data/utf16_ex.txt$
61+
- id: fix-byte-order-marker
62+
- id: fix-encoding-pragma
63+
args: [--remove]
5464
- id: trailing-whitespace
55-
stages: [commit, merge-commit, push, prepare-commit-msg, commit-msg,
56-
post-checkout, post-commit, post-merge, post-rewrite]
5765
- repo: https://github.com/cpplint/cpplint
5866
rev: 1.6.1
5967
hooks:
@@ -98,6 +106,8 @@ repos:
98106
- repo: https://github.com/pre-commit/pygrep-hooks
99107
rev: v1.10.0
100108
hooks:
109+
- id: python-check-blanket-noqa
110+
- id: python-check-blanket-type-ignore
101111
- id: rst-backticks
102112
- id: rst-directive-colons
103113
types: [text] # overwrite types: [rst]
@@ -154,31 +164,17 @@ repos:
154164
exclude: ^pandas/core/interchange/
155165
language: python
156166
types: [python]
157-
- id: no-os-remove
158-
name: Check code for instances of os.remove
159-
entry: os\.remove
160-
language: pygrep
161-
types: [python]
162-
files: ^pandas/tests/
163-
exclude: |
164-
(?x)^
165-
pandas/tests/io/pytables/test_store\.py$
166167
- id: unwanted-patterns
167168
name: Unwanted patterns
168169
language: pygrep
169170
entry: |
170171
(?x)
171-
# outdated annotation syntax, missing error codes
172+
# outdated annotation syntax
172173
\#\ type:\ (?!ignore)
173-
|\#\ type:\s?ignore(?!\[)
174174
175175
# foo._class__ instead of type(foo)
176176
|\.__class__
177177
178-
# np.bool/np.object instead of np.bool_/np.object_
179-
|np\.bool[^_8`]
180-
|np\.object[^_8`]
181-
182178
# Numpy
183179
|from\ numpy\ import\ random
184180
|from\ numpy\.random\ import
@@ -197,16 +193,8 @@ repos:
197193
198194
# builtin filter function
199195
|(?<!def)[\(\s]filter\(
200-
201-
# exec
202-
|[^a-zA-Z0-9_]exec\(
203196
types_or: [python, cython, rst]
204197
exclude: ^doc/source/development/code_style\.rst # contains examples of patterns to avoid
205-
- id: cython-casting
206-
name: Check Cython casting is `<type>obj`, not `<type> obj`
207-
language: pygrep
208-
entry: '[a-zA-Z0-9*]> '
209-
files: (\.pyx|\.pxi.in)$
210198
- id: incorrect-backticks
211199
name: Check for backticks incorrectly rendering because of missing spaces
212200
language: pygrep
@@ -219,19 +207,6 @@ repos:
219207
entry: 'np\.random\.seed'
220208
files: ^asv_bench/benchmarks
221209
exclude: ^asv_bench/benchmarks/pandas_vb_common\.py
222-
- id: np-testing-array-equal
223-
name: Check for usage of numpy testing or array_equal
224-
language: pygrep
225-
entry: '(numpy|np)(\.testing|\.array_equal)'
226-
files: ^pandas/tests/
227-
types: [python]
228-
- id: invalid-ea-testing
229-
name: Check for invalid EA testing
230-
language: pygrep
231-
entry: 'tm\.assert_(series|frame)_equal'
232-
files: ^pandas/tests/extension/base
233-
types: [python]
234-
exclude: ^pandas/tests/extension/base/base\.py
235210
- id: unwanted-patterns-in-tests
236211
name: Unwanted patterns in tests
237212
language: pygrep
@@ -265,6 +240,9 @@ repos:
265240
266241
# pytest.warns (use tm.assert_produces_warning instead)
267242
|pytest\.warns
243+
244+
# os.remove
245+
|os\.remove
268246
files: ^pandas/tests/
269247
types_or: [python, cython, rst]
270248
- id: unwanted-patterns-in-ea-tests

pandas/tests/io/pytables/test_store.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import contextlib
12
import datetime as dt
23
import hashlib
3-
import os
44
import tempfile
55
import time
66
from warnings import (
@@ -27,7 +27,6 @@
2727
from pandas.tests.io.pytables.common import (
2828
_maybe_remove,
2929
ensure_clean_store,
30-
safe_close,
3130
)
3231

3332
from pandas.io.pytables import (
@@ -832,53 +831,34 @@ def reader(path):
832831
tm.assert_frame_equal(df, result)
833832

834833

835-
def test_copy():
836-
with catch_warnings(record=True):
837-
838-
def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs):
839-
if new_f is None:
840-
fd, new_f = tempfile.mkstemp()
841-
842-
try:
843-
store = HDFStore(f, "r")
844-
tstore = store.copy(new_f, keys=keys, propindexes=propindexes, **kwargs)
834+
@pytest.mark.parametrize("propindexes", [True, False])
835+
def test_copy(propindexes):
836+
df = tm.makeDataFrame()
845837

846-
# check keys
847-
if keys is None:
838+
with tm.ensure_clean() as path:
839+
with HDFStore(path) as st:
840+
st.append("df", df, data_columns=["A"])
841+
with tempfile.NamedTemporaryFile() as new_f:
842+
with HDFStore(path) as store:
843+
with contextlib.closing(
844+
store.copy(new_f.name, keys=None, propindexes=propindexes)
845+
) as tstore:
846+
# check keys
848847
keys = store.keys()
849-
assert set(keys) == set(tstore.keys())
850-
851-
# check indices & nrows
852-
for k in tstore.keys():
853-
if tstore.get_storer(k).is_table:
854-
new_t = tstore.get_storer(k)
855-
orig_t = store.get_storer(k)
856-
857-
assert orig_t.nrows == new_t.nrows
858-
859-
# check propindixes
860-
if propindexes:
861-
for a in orig_t.axes:
862-
if a.is_indexed:
863-
assert new_t[a.name].is_indexed
864-
865-
finally:
866-
safe_close(store)
867-
safe_close(tstore)
868-
try:
869-
os.close(fd)
870-
except (OSError, ValueError):
871-
pass
872-
os.remove(new_f)
873-
874-
# new table
875-
df = tm.makeDataFrame()
876-
877-
with tm.ensure_clean() as path:
878-
with HDFStore(path) as st:
879-
st.append("df", df, data_columns=["A"])
880-
do_copy(f=path)
881-
do_copy(f=path, propindexes=False)
848+
assert set(keys) == set(tstore.keys())
849+
# check indices & nrows
850+
for k in tstore.keys():
851+
if tstore.get_storer(k).is_table:
852+
new_t = tstore.get_storer(k)
853+
orig_t = store.get_storer(k)
854+
855+
assert orig_t.nrows == new_t.nrows
856+
857+
# check propindixes
858+
if propindexes:
859+
for a in orig_t.axes:
860+
if a.is_indexed:
861+
assert new_t[a.name].is_indexed
882862

883863

884864
def test_duplicate_column_name(tmp_path, setup_path):

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ select = [
239239
"PGH",
240240
# Ruff-specific rules
241241
"RUF",
242+
# flake8-bandit: exec-builtin
243+
"S102"
242244
]
243245

244246
ignore = [

0 commit comments

Comments
 (0)