Skip to content

Commit c106d91

Browse files
authored
Add flake8 to pre-commit config (#1453)
* Added flake8 to pre-commit config * Do not use mutable data structures for argument defaults
1 parent b2f116a commit c106d91

31 files changed

+200
-119
lines changed

.flake8

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[flake8]
2+
extend-ignore =
3+
# whitespace before ':' (currently conflicts with black formatting):
4+
E203,
5+
# line too long (in docstrings):
6+
E501,
7+
# ‘from module import *’ used; unable to detect undefined names:
8+
F403,
9+
# name may be undefined, or defined from star imports: module:
10+
F405,
11+
# doc line too long (105 > 80 characters):
12+
W505,
13+
# missing docstring in public module:
14+
D100,
15+
# missing docstring in public class:
16+
D101,
17+
# missing docstring in public method:
18+
D102,
19+
# missing docstring in public function:
20+
D103,
21+
# missing docstring in public package:
22+
D104,
23+
# missing docstring in magic method:
24+
D105,
25+
# missing docstring in __init__:
26+
D107,
27+
# no blank lines allowed after function docstring:
28+
D202,
29+
# first line should end with a period:
30+
D400,
31+
# first line should be in imperative mood:
32+
D401,
33+
# first line should not be the function's "signature":
34+
D402,
35+
# section has no content:
36+
D414
37+
38+
per-file-ignores =
39+
__init__.py: E402, F401
40+
dpnp/dpnp_algo/__init__.py: F401
41+
dpnp/dpnp_algo/__init__.py: F401
42+
dpnp/fft/__init__.py: F401
43+
dpnp/linalg/__init__.py: F401
44+
dpnp/random/__init__.py: F401
45+
dpnp/dpnp_iface.py: D205
46+
47+
filename = *.py, *.pyx, *.pxi, *.pxd
48+
max_line_length = 80
49+
max-doc-length = 80
50+
show-source = True
51+
52+
exclude =
53+
.git,
54+
benchmarks/*.py,
55+
build,
56+
dpnp/to_numba/*.py,
57+
conda.recipe,
58+
tests/*.py,
59+
tests_external/*.py,
60+
version.py,
61+
62+
# Print detailed statistic if any issue detected
63+
count = True
64+
statistics = True

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ repos:
3939
- id: isort
4040
name: isort (pyi)
4141
types: [pyi]
42+
- repo: https://github.com/pycqa/flake8
43+
rev: 6.0.0
44+
hooks:
45+
- id: flake8
46+
args: ["--config=.flake8"]
47+
additional_dependencies:
48+
- flake8-docstrings==1.7.0
49+
- flake8-bugbear==23.6.5
4250
- repo: https://github.com/pocc/pre-commit-hooks
4351
rev: v1.3.5
4452
hooks:

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import dpctl
3131
import dpctl.tensor as dpt
3232
import dpctl.tensor._tensor_impl as ti
33-
import numpy
3433
from dpctl.tensor._elementwise_common import BinaryElementwiseFunc
3534

3635
import dpnp
@@ -67,6 +66,7 @@
6766
def dpnp_add(x1, x2, out=None, order="K"):
6867
"""
6968
Invokes add() from dpctl.tensor implementation for add() function.
69+
7070
TODO: add a pybind11 extension of add() from OneMKL VM where possible
7171
and would be performance effective.
7272
@@ -111,21 +111,28 @@ def dpnp_add(x1, x2, out=None, order="K"):
111111
def dpnp_divide(x1, x2, out=None, order="K"):
112112
"""
113113
Invokes div() function from pybind11 extension of OneMKL VM if possible.
114+
114115
Otherwise fully relies on dpctl.tensor implementation for divide() function.
115116
116117
"""
117118

118-
def _call_divide(src1, src2, dst, sycl_queue, depends=[]):
119+
def _call_divide(src1, src2, dst, sycl_queue, depends=None):
119120
"""A callback to register in BinaryElementwiseFunc class of dpctl.tensor"""
120121

122+
if depends is None:
123+
depends = []
124+
121125
if vmi._can_call_div(sycl_queue, src1, src2, dst):
122126
# call pybind11 extension for div() function from OneMKL VM
123127
return vmi._div(sycl_queue, src1, src2, dst, depends)
124128
return ti._divide(src1, src2, dst, sycl_queue, depends)
125129

126-
def _call_divide_inplace(lhs, rhs, sycl_queue, depends=[]):
130+
def _call_divide_inplace(lhs, rhs, sycl_queue, depends=None):
127131
"""In place workaround until dpctl.tensor provides the functionality."""
128132

133+
if depends is None:
134+
depends = []
135+
129136
# allocate temporary memory for out array
130137
out = dpt.empty_like(lhs, dtype=dpnp.result_type(lhs.dtype, rhs.dtype))
131138

@@ -182,6 +189,7 @@ def _call_divide_inplace(lhs, rhs, sycl_queue, depends=[]):
182189
def dpnp_multiply(x1, x2, out=None, order="K"):
183190
"""
184191
Invokes multiply() from dpctl.tensor implementation for multiply() function.
192+
185193
TODO: add a pybind11 extension of mul() from OneMKL VM where possible
186194
and would be performance effective.
187195
@@ -230,6 +238,7 @@ def dpnp_multiply(x1, x2, out=None, order="K"):
230238
def dpnp_subtract(x1, x2, out=None, order="K"):
231239
"""
232240
Invokes subtract() from dpctl.tensor implementation for subtract() function.
241+
233242
TODO: add a pybind11 extension of sub() from OneMKL VM where possible
234243
and would be performance effective.
235244

dpnp/dpnp_array.py

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
def _get_unwrapped_index_key(key):
3434
"""
35+
Get an unwrapped index key.
36+
3537
Return a key where each nested instance of DPNP array is unwrapped into USM ndarray
3638
for futher processing in DPCTL advanced indexing functions.
3739
@@ -109,9 +111,7 @@ def T(self):
109111
return self.transpose()
110112

111113
def to_device(self, target_device):
112-
"""
113-
Transfer array to target device
114-
"""
114+
"""Transfer array to target device."""
115115

116116
return dpnp_array(
117117
shape=self.shape, buffer=self.get_array().to_device(target_device)
@@ -276,9 +276,7 @@ def __le__(self, other):
276276
return dpnp.less_equal(self, other)
277277

278278
def __len__(self):
279-
"""
280-
Performs the operation __len__.
281-
"""
279+
"""Performs the operation __len__."""
282280

283281
return self._array_obj.__len__()
284282

@@ -335,7 +333,7 @@ def __rmatmul__(self, other):
335333
return dpnp.matmul(other, self)
336334

337335
def __rmod__(self, other):
338-
return remainder(other, self)
336+
return dpnp.remainder(other, self)
339337

340338
def __rmul__(self, other):
341339
return dpnp.multiply(other, self)
@@ -499,8 +497,7 @@ def argmin(self, axis=None, out=None):
499497

500498
def argsort(self, axis=-1, kind=None, order=None):
501499
"""
502-
Return an ndarray of indices that sort the array along the
503-
specified axis.
500+
Return an ndarray of indices that sort the array along the specified axis.
504501
505502
Parameters
506503
----------
@@ -584,10 +581,7 @@ def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True):
584581
# 'byteswap',
585582

586583
def choose(input, choices, out=None, mode="raise"):
587-
"""
588-
Construct an array from an index array and a set of arrays to choose from.
589-
590-
"""
584+
"""Construct an array from an index array and a set of arrays to choose from."""
591585

592586
return dpnp.choose(input, choices, out, mode)
593587

@@ -655,7 +649,7 @@ def dot(self, other, out=None):
655649

656650
@property
657651
def dtype(self):
658-
""" """
652+
"""Returns NumPy's dtype corresponding to the type of the array elements."""
659653

660654
return self._array_obj.dtype
661655

@@ -689,19 +683,13 @@ def fill(self, value):
689683

690684
@property
691685
def flags(self):
692-
"""
693-
Return information about the memory layout of the array.
694-
695-
"""
686+
"""Return information about the memory layout of the array."""
696687

697688
return self._array_obj.flags
698689

699690
@property
700691
def flat(self):
701-
"""
702-
Return a flat iterator, or set a flattened version of self to value.
703-
704-
"""
692+
"""Return a flat iterator, or set a flattened version of self to value."""
705693

706694
return dpnp.flatiter(self)
707695

@@ -787,10 +775,7 @@ def item(self, id=None):
787775

788776
@property
789777
def itemsize(self):
790-
"""
791-
Size of one array element in bytes.
792-
793-
"""
778+
"""Size of one array element in bytes."""
794779

795780
return self._array_obj.itemsize
796781

@@ -802,16 +787,12 @@ def max(
802787
initial=numpy._NoValue,
803788
where=numpy._NoValue,
804789
):
805-
"""
806-
Return the maximum along an axis.
807-
"""
790+
"""Return the maximum along an axis."""
808791

809792
return dpnp.max(self, axis, out, keepdims, initial, where)
810793

811794
def mean(self, axis=None, **kwargs):
812-
"""
813-
Returns the average of the array elements.
814-
"""
795+
"""Returns the average of the array elements."""
815796

816797
return dpnp.mean(self, axis=axis, **kwargs)
817798

@@ -823,27 +804,19 @@ def min(
823804
initial=numpy._NoValue,
824805
where=numpy._NoValue,
825806
):
826-
"""
827-
Return the minimum along a given axis.
828-
"""
807+
"""Return the minimum along a given axis."""
829808

830809
return dpnp.min(self, axis, out, keepdims, initial, where)
831810

832811
@property
833812
def nbytes(self):
834-
"""
835-
Total bytes consumed by the elements of the array.
836-
837-
"""
813+
"""Total bytes consumed by the elements of the array."""
838814

839815
return self._array_obj.nbytes
840816

841817
@property
842818
def ndim(self):
843-
"""
844-
Number of array dimensions.
845-
846-
"""
819+
"""Number of array dimensions."""
847820

848821
return self._array_obj.ndim
849822

@@ -854,6 +827,8 @@ def nonzero(self):
854827

855828
def partition(self, kth, axis=-1, kind="introselect", order=None):
856829
"""
830+
Return a partitioned copy of an array.
831+
857832
Rearranges the elements in the array in such a way that the value of the
858833
element in kth position is in the position it would be in a sorted array.
859834
@@ -968,7 +943,10 @@ def shape(self):
968943

969944
@shape.setter
970945
def shape(self, newshape):
971-
"""Set new lengths of axes. A tuple of numbers represents size of each dimention.
946+
"""
947+
Set new lengths of axes.
948+
949+
A tuple of numbers represents size of each dimention.
972950
It involves reshaping without copy. If the array cannot be reshaped without copy,
973951
it raises an exception.
974952
@@ -980,7 +958,7 @@ def shape(self, newshape):
980958

981959
@property
982960
def size(self):
983-
""" """
961+
"""Number of elements in the array."""
984962

985963
return self._array_obj.size
986964

@@ -1009,7 +987,17 @@ def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
1009987

1010988
@property
1011989
def strides(self):
1012-
""" """
990+
"""
991+
Get strides of an array.
992+
993+
Returns memory displacement in array elements, upon unit
994+
change of respective index.
995+
996+
E.g. for strides (s1, s2, s3) and multi-index (i1, i2, i3)
997+
998+
a[i1, i2, i3] == (&a[0,0,0])[ s1*s1 + s2*i2 + s3*i3]
999+
1000+
"""
10131001

10141002
return self._array_obj.strides
10151003

dpnp/dpnp_container.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"eye",
4848
"full",
4949
"linspace",
50-
"ones" "tril",
50+
"ones",
51+
"tril",
5152
"triu",
5253
"zeros",
5354
]
@@ -297,15 +298,15 @@ def ones(
297298

298299

299300
def tril(x1, /, *, k=0):
300-
""" "Creates `dpnp_array` as lower triangular part of an input array."""
301+
"""Creates `dpnp_array` as lower triangular part of an input array."""
301302
array_obj = dpt.tril(
302303
x1.get_array() if isinstance(x1, dpnp_array) else x1, k
303304
)
304305
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
305306

306307

307308
def triu(x1, /, *, k=0):
308-
""" "Creates `dpnp_array` as upper triangular part of an input array."""
309+
"""Creates `dpnp_array` as upper triangular part of an input array."""
309310
array_obj = dpt.triu(
310311
x1.get_array() if isinstance(x1, dpnp_array) else x1, k
311312
)

dpnp/dpnp_flatiter.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
# THE POSSIBILITY OF SUCH DAMAGE.
2525
# *****************************************************************************
2626

27-
"""
28-
Implementation of flatiter
29-
30-
"""
27+
"""Implementation of flatiter."""
3128

3229
import dpnp
3330

0 commit comments

Comments
 (0)