Skip to content

Commit 1f27ed0

Browse files
authored
STYLE use pandas-dev-flaker (#40906)
1 parent 499aa67 commit 1f27ed0

23 files changed

+365
-1455
lines changed

.pre-commit-config.yaml

+9-95
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ repos:
3838
rev: 3.9.0
3939
hooks:
4040
- id: flake8
41-
additional_dependencies: [flake8-comprehensions>=3.1.0, flake8-bugbear>=21.3.2]
41+
additional_dependencies:
42+
- flake8-comprehensions==3.1.0
43+
- flake8-bugbear==21.3.2
44+
- pandas-dev-flaker==0.2.0
4245
- id: flake8
4346
name: flake8 (cython)
4447
types: [cython]
@@ -71,7 +74,11 @@ repos:
7174
rev: v1.2.2
7275
hooks:
7376
- id: yesqa
74-
additional_dependencies: [flake8==3.9.0]
77+
additional_dependencies:
78+
- flake8==3.9.0
79+
- flake8-comprehensions==3.1.0
80+
- flake8-bugbear==21.3.2
81+
- pandas-dev-flaker==0.2.0
7582
- repo: local
7683
hooks:
7784
- id: flake8-rst
@@ -82,28 +89,6 @@ repos:
8289
types: [rst]
8390
args: [--filename=*.rst]
8491
additional_dependencies: [flake8-rst==0.7.0, flake8==3.7.9]
85-
- id: frame-or-series-union
86-
name: Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias
87-
entry: Union\[.*(Series,.*DataFrame|DataFrame,.*Series).*\]
88-
language: pygrep
89-
types: [python]
90-
exclude: ^pandas/_typing\.py$
91-
- id: inconsistent-namespace-usage
92-
name: 'Check for inconsistent use of pandas namespace'
93-
entry: python scripts/check_for_inconsistent_pandas_namespace.py
94-
language: python
95-
types: [python]
96-
- id: no-os-remove
97-
name: Check code for instances of os.remove
98-
entry: os\.remove
99-
language: pygrep
100-
types: [python]
101-
files: ^pandas/tests/
102-
exclude: |
103-
(?x)^
104-
pandas/tests/io/excel/test_writers\.py
105-
|pandas/tests/io/pytables/common\.py
106-
|pandas/tests/io/pytables/test_store\.py$
10792
- id: unwanted-patterns
10893
name: Unwanted patterns
10994
language: pygrep
@@ -113,52 +98,10 @@ repos:
11398
\#\ type:\ (?!ignore)
11499
|\#\ type:\s?ignore(?!\[)
115100
116-
# foo._class__ instead of type(foo)
117-
|\.__class__
118-
119-
# np.bool/np.object instead of np.bool_/np.object_
120-
|np\.bool[^_8]
121-
|np\.object[^_8]
122-
123-
# imports from pandas.core.common instead of `import pandas.core.common as com`
124-
|from\ pandas\.core\.common\ import
125-
|from\ pandas\.core\ import\ common
126-
127-
# imports from collections.abc instead of `from collections import abc`
128-
|from\ collections\.abc\ import
129-
130-
# Numpy
131-
|from\ numpy\ import\ random
132-
|from\ numpy\.random\ import
133-
134101
# Incorrect code-block / IPython directives
135102
|\.\.\ code-block\ ::
136103
|\.\.\ ipython\ ::
137104
types_or: [python, cython, rst]
138-
exclude: ^doc/source/development/code_style\.rst # contains examples of patterns to avoid
139-
- id: unwanted-patterns-in-tests
140-
name: Unwanted patterns in tests
141-
language: pygrep
142-
entry: |
143-
(?x)
144-
# pytest.xfail instead of pytest.mark.xfail
145-
pytest\.xfail
146-
147-
# imports from pandas._testing instead of `import pandas._testing as tm`
148-
|from\ pandas\._testing\ import
149-
|from\ pandas\ import\ _testing\ as\ tm
150-
151-
# No direct imports from conftest
152-
|conftest\ import
153-
|import\ conftest
154-
155-
# pandas.testing instead of tm
156-
|pd\.testing\.
157-
158-
# pd.api.types instead of from pandas.api.types import ...
159-
|(pd|pandas)\.api\.types\.
160-
files: ^pandas/tests/
161-
types_or: [python, cython, rst]
162105
- id: pip-to-conda
163106
name: Generate pip dependency from conda
164107
description: This hook checks if the conda environment.yml and requirements-dev.txt are equal
@@ -180,35 +123,6 @@ repos:
180123
language: python
181124
types: [rst]
182125
files: ^doc/source/(development|reference)/
183-
- id: unwanted-patterns-bare-pytest-raises
184-
name: Check for use of bare pytest raises
185-
language: python
186-
entry: python scripts/validate_unwanted_patterns.py --validation-type="bare_pytest_raises"
187-
types: [python]
188-
files: ^pandas/tests/
189-
exclude: ^pandas/tests/extension/
190-
- id: unwanted-patterns-private-function-across-module
191-
name: Check for use of private functions across modules
192-
language: python
193-
entry: python scripts/validate_unwanted_patterns.py --validation-type="private_function_across_module"
194-
types: [python]
195-
exclude: ^(asv_bench|pandas/tests|doc)/
196-
- id: unwanted-patterns-private-import-across-module
197-
name: Check for import of private attributes across modules
198-
language: python
199-
entry: python scripts/validate_unwanted_patterns.py --validation-type="private_import_across_module"
200-
types: [python]
201-
exclude: ^(asv_bench|pandas/tests|doc)/
202-
- id: unwanted-patterns-strings-to-concatenate
203-
name: Check for use of not concatenated strings
204-
language: python
205-
entry: python scripts/validate_unwanted_patterns.py --validation-type="strings_to_concatenate"
206-
types_or: [python, cython]
207-
- id: unwanted-patterns-strings-with-wrong-placed-whitespace
208-
name: Check for strings with wrong placed spaces
209-
language: python
210-
entry: python scripts/validate_unwanted_patterns.py --validation-type="strings_with_wrong_placed_whitespace"
211-
types_or: [python, cython]
212126
- id: use-pd_array-in-core
213127
name: Import pandas.array as pd_array in core
214128
language: python

asv_bench/benchmarks/gil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
except ImportError:
3232
from pandas import algos
3333
try:
34-
from pandas._testing import test_parallel
34+
from pandas._testing import test_parallel # noqa: PDF014
3535

3636
have_real_test_parallel = True
3737
except ImportError:

asv_bench/benchmarks/pandas_vb_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BaseIO:
7070
def remove(self, f):
7171
"""Remove created files"""
7272
try:
73-
os.remove(f)
73+
os.remove(f) # noqa: PDF008
7474
except OSError:
7575
# On Windows, attempting to remove a file that is in use
7676
# causes an exception to be raised

ci/code_checks.sh

-30
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,6 @@ fi
6464
### PATTERNS ###
6565
if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
6666

67-
MSG='Check for use of exec' ; echo $MSG
68-
invgrep -R --include="*.py*" -E "[^a-zA-Z0-9_]exec\(" pandas
69-
RET=$(($RET + $?)) ; echo $MSG "DONE"
70-
71-
MSG='Check for pytest warns' ; echo $MSG
72-
invgrep -r -E --include '*.py' 'pytest\.warns' pandas/tests/
73-
RET=$(($RET + $?)) ; echo $MSG "DONE"
74-
75-
MSG='Check for pytest raises without context' ; echo $MSG
76-
invgrep -r -E --include '*.py' "[[:space:]] pytest.raises" pandas/tests/
77-
RET=$(($RET + $?)) ; echo $MSG "DONE"
78-
79-
MSG='Check for use of builtin filter function' ; echo $MSG
80-
invgrep -R --include="*.py" -P '(?<!def)[\(\s]filter\(' pandas
81-
RET=$(($RET + $?)) ; echo $MSG "DONE"
82-
83-
# Check for the following code in testing: `np.testing` and `np.array_equal`
84-
MSG='Check for invalid testing' ; echo $MSG
85-
invgrep -r -E --include '*.py' --exclude testing.py '(numpy|np)(\.testing|\.array_equal)' pandas/tests/
86-
RET=$(($RET + $?)) ; echo $MSG "DONE"
87-
8867
# Check for the following code in the extension array base tests: `tm.assert_frame_equal` and `tm.assert_series_equal`
8968
MSG='Check for invalid EA testing' ; echo $MSG
9069
invgrep -r -E --include '*.py' --exclude base.py 'tm.assert_(series|frame)_equal' pandas/tests/extension/base
@@ -98,15 +77,6 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
9877
invgrep -R --include="*.rst" -E "[a-zA-Z0-9]\`\`?[a-zA-Z0-9]" doc/source/
9978
RET=$(($RET + $?)) ; echo $MSG "DONE"
10079

101-
# Check for the following code in testing: `unittest.mock`, `mock.Mock()` or `mock.patch`
102-
MSG='Check that unittest.mock is not used (pytest builtin monkeypatch fixture should be used instead)' ; echo $MSG
103-
invgrep -r -E --include '*.py' '(unittest(\.| import )mock|mock\.Mock\(\)|mock\.patch)' pandas/tests/
104-
RET=$(($RET + $?)) ; echo $MSG "DONE"
105-
106-
MSG='Check for use of {foo!r} instead of {repr(foo)}' ; echo $MSG
107-
invgrep -R --include=*.{py,pyx} '!r}' pandas
108-
RET=$(($RET + $?)) ; echo $MSG "DONE"
109-
echo $MSG "DONE"
11080
fi
11181

11282
### CODE ###

doc/source/development/code_style.rst

+2-141
Original file line numberDiff line numberDiff line change
@@ -19,147 +19,8 @@ consistent code format throughout the project. We encourage you to use
1919
Patterns
2020
========
2121

22-
Using foo.__class__
23-
-------------------
24-
25-
26-
pandas uses 'type(foo)' instead 'foo.__class__' as it is making the code more
27-
readable.
28-
For example:
29-
30-
**Good:**
31-
32-
.. code-block:: python
33-
34-
foo = "bar"
35-
type(foo)
36-
37-
**Bad:**
38-
39-
.. code-block:: python
40-
41-
foo = "bar"
42-
foo.__class__
43-
44-
45-
String formatting
46-
=================
47-
48-
Concatenated strings
49-
--------------------
50-
51-
Using f-strings
52-
~~~~~~~~~~~~~~~
53-
54-
pandas uses f-strings formatting instead of '%' and '.format()' string formatters.
55-
56-
The convention of using f-strings on a string that is concatenated over several lines,
57-
is to prefix only the lines containing values which need to be interpreted.
58-
59-
For example:
60-
61-
**Good:**
62-
63-
.. code-block:: python
64-
65-
foo = "old_function"
66-
bar = "new_function"
67-
68-
my_warning_message = (
69-
f"Warning, {foo} is deprecated, "
70-
"please use the new and way better "
71-
f"{bar}"
72-
)
73-
74-
**Bad:**
75-
76-
.. code-block:: python
77-
78-
foo = "old_function"
79-
bar = "new_function"
80-
81-
my_warning_message = (
82-
f"Warning, {foo} is deprecated, "
83-
f"please use the new and way better "
84-
f"{bar}"
85-
)
86-
87-
White spaces
88-
~~~~~~~~~~~~
89-
90-
Only put white space at the end of the previous line, so
91-
there is no whitespace at the beginning of the concatenated string.
92-
93-
For example:
94-
95-
**Good:**
96-
97-
.. code-block:: python
98-
99-
example_string = (
100-
"Some long concatenated string, "
101-
"with good placement of the "
102-
"whitespaces"
103-
)
104-
105-
**Bad:**
106-
107-
.. code-block:: python
108-
109-
example_string = (
110-
"Some long concatenated string,"
111-
" with bad placement of the"
112-
" whitespaces"
113-
)
114-
115-
Representation function (aka 'repr()')
116-
--------------------------------------
117-
118-
pandas uses 'repr()' instead of '%r' and '!r'.
119-
120-
The use of 'repr()' will only happen when the value is not an obvious string.
121-
122-
For example:
123-
124-
**Good:**
125-
126-
.. code-block:: python
127-
128-
value = str
129-
f"Unknown received value, got: {repr(value)}"
130-
131-
**Good:**
132-
133-
.. code-block:: python
134-
135-
value = str
136-
f"Unknown received type, got: '{type(value).__name__}'"
137-
138-
139-
Imports (aim for absolute)
140-
==========================
141-
142-
In Python 3, absolute imports are recommended. Using absolute imports, doing something
143-
like ``import string`` will import the string module rather than ``string.py``
144-
in the same directory. As much as possible, you should try to write out
145-
absolute imports that show the whole import chain from top-level pandas.
146-
147-
Explicit relative imports are also supported in Python 3 but it is not
148-
recommended to use them. Implicit relative imports should never be used
149-
and are removed in Python 3.
150-
151-
For example:
152-
153-
::
154-
155-
# preferred
156-
import pandas.core.common as com
157-
158-
# not preferred
159-
from .common import test_base
160-
161-
# wrong
162-
from common import test_base
22+
We use a ``flake8`` plugin, `pandas-dev-flaker <https://github.com/pandas-dev/pandas-dev-flaker>`_, to
23+
check our codebase for unwanted patterns. See its ``README`` for the up-to-date list of rules we enforce.
16324

16425
Testing
16526
=======

environment.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ dependencies:
2121
- black=20.8b1
2222
- cpplint
2323
- flake8=3.9.0
24-
- flake8-bugbear>=21.3.2 # used by flake8, find likely bugs
25-
- flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions
24+
- flake8-bugbear=21.3.2 # used by flake8, find likely bugs
25+
- flake8-comprehensions=3.1.0 # used by flake8, linting of unnecessary comprehensions
2626
- isort>=5.2.1 # check that imports are in the right order
2727
- mypy=0.812
2828
- pre-commit>=2.9.2
@@ -117,3 +117,4 @@ dependencies:
117117
- pip:
118118
- git+https://github.com/pydata/pydata-sphinx-theme.git@master
119119
- numpydoc < 1.2 # 2021-02-09 1.2dev breaking CI
120+
- pandas-dev-flaker==0.2.0

pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ def external_error_raised(expected_exception: type[Exception]) -> ContextManager
915915
"""
916916
import pytest
917917

918-
return pytest.raises(expected_exception, match=None)
918+
return pytest.raises(expected_exception, match=None) # noqa: PDF010
919919

920920

921921
cython_table = pd.core.common._cython_table.items()

pandas/core/window/ewm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas.core.dtypes.common import is_datetime64_ns_dtype
2222
from pandas.core.dtypes.missing import isna
2323

24-
import pandas.core.common as common
24+
import pandas.core.common as common # noqa: PDF018
2525
from pandas.core.util.numba_ import maybe_use_numba
2626
from pandas.core.window.common import zsqrt
2727
from pandas.core.window.doc import (

0 commit comments

Comments
 (0)