Skip to content

Commit e6e7b3e

Browse files
authored
Adding pylint pre-commit hook (#1718)
* Resolved all issue in dpnp_iface.py * Make pylint as a local hook * Added dpnp_iface_arraycreation.py to the pylint hook * Added dpnp_iface_counting.py and dpnp_iface_indexing.py * Added dpnp_iface_libmath.py and dpnp_iface_linearalgebra.py * Adding dpnp_iface_manipulation.py dpnp_iface_nanfunctions.py dpnp_iface_searching.py dpnp_iface_sorting.py dpnp_iface_statistics.py dpnp_iface_types.py * Disable D205 flake8 warning globally
1 parent d34b8ce commit e6e7b3e

16 files changed

+1380
-996
lines changed

.flake8

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extend-ignore =
2626
D107,
2727
# no blank lines allowed after function docstring:
2828
D202,
29+
# 1 blank line required between summary line and description:
30+
D205,
2931
# first line should end with a period:
3032
D400,
3133
# first line should be in imperative mood:
@@ -42,7 +44,6 @@ per-file-ignores =
4244
dpnp/fft/__init__.py: F401
4345
dpnp/linalg/__init__.py: F401
4446
dpnp/random/__init__.py: F401
45-
dpnp/dpnp_iface.py: D205
4647

4748
filename = *.py, *.pyx, *.pxi, *.pxd
4849
max_line_length = 80

.github/workflows/pre-commit.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ jobs:
1818
sudo ln -s /usr/bin/clang-format-12 /usr/bin/clang-format
1919
clang-format --version
2020
21-
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
22-
- uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
21+
- name: Set up pylint
22+
uses: BSFishy/pip-action@8f2d471d809dc20b6ada98c91910b6ae6243f318 # v1
23+
with:
24+
packages: |
25+
pylint
26+
27+
- name: Checkout DPNP repo
28+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
29+
30+
- name: Set up python
31+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
2332
with:
2433
python-version: '3.11'
25-
- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
34+
35+
- name: Run pre-commit checks
36+
uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,19 @@ repos:
7979
rev: 3.0.0
8080
hooks:
8181
- id: shellcheck
82+
- repo: local
83+
hooks:
84+
- id: pylint
85+
name: pylint
86+
entry: pylint
87+
language: system
88+
types: [python]
89+
args:
90+
[
91+
"-rn", # Only display messages
92+
"-sn", # Don't display the score
93+
"--disable=import-error",
94+
"--disable=redefined-builtin",
95+
"--disable=unused-wildcard-import"
96+
]
97+
files: '^dpnp/dpnp_iface(|_arraycreation|_counting|_indexing|_libmath|_linearalgebra|_manipulation|_nanfunctions|_searching|_sorting|_statistics|_types).py$'

dpnp/dpnp_iface.py

Lines changed: 98 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
import dpctl
4444
import dpctl.tensor as dpt
4545
import numpy
46+
from dpctl.tensor._device import normalize_queue_device
4647

4748
import dpnp
4849
from dpnp.dpnp_algo import *
4950
from dpnp.dpnp_array import dpnp_array
50-
from dpnp.dpnp_utils import *
5151
from dpnp.fft import *
5252
from dpnp.linalg import *
5353
from dpnp.random import *
@@ -59,7 +59,6 @@
5959
"check_supported_arrays_type",
6060
"convert_single_elem_array_to_scalar",
6161
"default_float_type",
62-
"dpnp_queue_initialize",
6362
"from_dlpack",
6463
"get_dpnp_descriptor",
6564
"get_include",
@@ -101,6 +100,13 @@
101100
from dpnp.dpnp_iface_trigonometric import *
102101
from dpnp.dpnp_iface_trigonometric import __all__ as __all__trigonometric
103102

103+
# pylint: disable=no-name-in-module
104+
from .dpnp_utils import (
105+
dpnp_descriptor,
106+
map_dtype_to_device,
107+
use_origin_backend,
108+
)
109+
104110
__all__ += __all__arraycreation
105111
__all__ += __all__bitwise
106112
__all__ += __all__counting
@@ -132,27 +138,44 @@ def array_equal(a1, a2, equal_nan=False):
132138
133139
"""
134140

135-
return numpy.array_equal(a1, a2)
141+
return numpy.array_equal(a1, a2, equal_nan=equal_nan)
136142

137143

138-
def asnumpy(input, order="C"):
144+
def asnumpy(a, order="C"):
139145
"""
140146
Returns the NumPy array with input data.
141147
148+
Parameters
149+
----------
150+
a : {array_like}
151+
Arbitrary object that can be converted to :obj:`numpy.ndarray`.
152+
order : {'C', 'F', 'A', 'K'}
153+
The desired memory layout of the converted array.
154+
When `order` is ``A``, it uses ``F`` if `a` is column-major and uses
155+
``C`` otherwise. And when `order` is ``K``, it keeps strides as closely
156+
as possible.
157+
158+
Returns
159+
-------
160+
out : numpy.ndarray
161+
NumPy interpretation of input array `a`.
162+
142163
Notes
143164
-----
144165
This function works exactly the same as :obj:`numpy.asarray`.
145166
146167
"""
147-
if isinstance(input, dpnp_array):
148-
return input.asnumpy()
149168

150-
if isinstance(input, dpt.usm_ndarray):
151-
return dpt.asnumpy(input)
169+
if isinstance(a, dpnp_array):
170+
return a.asnumpy()
171+
172+
if isinstance(a, dpt.usm_ndarray):
173+
return dpt.asnumpy(a)
152174

153-
return numpy.asarray(input, order=order)
175+
return numpy.asarray(a, order=order)
154176

155177

178+
# pylint: disable=redefined-outer-name
156179
def astype(x1, dtype, order="K", casting="unsafe", copy=True):
157180
"""
158181
Copy the array with data type casting.
@@ -165,28 +188,32 @@ def astype(x1, dtype, order="K", casting="unsafe", copy=True):
165188
Target data type.
166189
order : {'C', 'F', 'A', 'K'}
167190
Row-major (C-style) or column-major (Fortran-style) order.
168-
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
169-
And when ``order`` is 'K', it keeps strides as closely as possible.
191+
When `order` is ``A``, it uses ``F`` if `a` is column-major and uses
192+
``C`` otherwise. And when `order` is ``K``, it keeps strides as closely
193+
as possible.
170194
copy : bool
171-
If it is False and no cast happens, then this method returns the array itself.
172-
Otherwise, a copy is returned.
195+
If it is ``False`` and no cast happens, then this method returns
196+
the array itself. Otherwise, a copy is returned.
173197
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
174-
Controls what kind of data casting may occur. Defaults to 'unsafe' for backwards compatibility.
175-
'no' means the data types should not be cast at all.
176-
'equiv' means only byte-order changes are allowed.
177-
'safe' means only casts which can preserve values are allowed.
178-
'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed.
179-
'unsafe' means any data conversions may be done.
198+
Controls what kind of data casting may occur. Defaults to ``unsafe``
199+
for backwards compatibility.
200+
- 'no' means the data types should not be cast at all.
201+
- 'equiv' means only byte-order changes are allowed.
202+
- 'safe' means only casts which can preserve values are allowed.
203+
- 'same_kind' means only safe casts or casts within a kind, like
204+
float64 to float32, are allowed.
205+
- 'unsafe' means any data conversions may be done.
180206
copy : bool, optional
181-
By default, astype always returns a newly allocated array. If this is set to false, and the dtype,
182-
order, and subok requirements are satisfied, the input array is returned instead of a copy.
207+
By default, astype always returns a newly allocated array. If this
208+
is set to ``False``, and the dtype, order, and subok requirements
209+
are satisfied, the input array is returned instead of a copy.
183210
184211
Returns
185212
-------
186213
arr_t : dpnp.ndarray
187-
Unless `copy` is ``False`` and the other conditions for returning the input array
188-
are satisfied, `arr_t` is a new array of the same shape as the input array,
189-
with dtype, order given by dtype, order.
214+
Unless `copy` is ``False`` and the other conditions for returning
215+
the input array are satisfied, `arr_t` is a new array of the same shape
216+
as the input array, with dtype, order given by dtype, order.
190217
191218
"""
192219

@@ -238,16 +265,18 @@ def check_supported_arrays_type(*arrays, scalar_type=False, all_scalars=False):
238265
if is_supported_array_type(a):
239266
any_is_array = True
240267
continue
241-
elif scalar_type and dpnp.isscalar(a):
268+
269+
if scalar_type and dpnp.isscalar(a):
242270
continue
243271

244272
raise TypeError(
245-
"An array must be any of supported type, but got {}".format(type(a))
273+
f"An array must be any of supported type, but got {type(a)}"
246274
)
247275

248276
if len(arrays) > 1 and not (all_scalars or any_is_array):
249277
raise TypeError(
250-
"At least one input must be of supported array type, but got all scalars."
278+
"At least one input must be of supported array type, "
279+
"but got all scalars."
251280
)
252281
return True
253282

@@ -263,21 +292,24 @@ def convert_single_elem_array_to_scalar(obj, keepdims=False):
263292

264293
def default_float_type(device=None, sycl_queue=None):
265294
"""
266-
Return a floating type used by default in DPNP depending on device capabilities.
295+
Return a floating type used by default in DPNP depending on device
296+
capabilities.
267297
268298
Parameters
269299
----------
270300
device : {None, string, SyclDevice, SyclQueue}, optional
271-
An array API concept of device where an array of default floating type might be created.
272-
The `device` can be ``None`` (the default), an OneAPI filter selector string,
273-
an instance of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL device,
274-
an instance of :class:`dpctl.SyclQueue`, or a `Device` object returned by
301+
An array API concept of device where an array of default floating type
302+
might be created. The `device` can be ``None`` (the default), an OneAPI
303+
filter selector string, an instance of :class:`dpctl.SyclDevice`
304+
corresponding to a non-partitioned SYCL device, an instance of
305+
:class:`dpctl.SyclQueue`, or a `Device` object returned by
275306
:obj:`dpnp.dpnp_array.dpnp_array.device` property.
276307
The value ``None`` is interpreted as to use a default device.
277308
sycl_queue : {None, SyclQueue}, optional
278-
A SYCL queue which might be used to create an array of default floating type.
279-
The `sycl_queue` can be ``None`` (the default), which is interpreted as
280-
to get the SYCL queue from `device` keyword if present or to use a default queue.
309+
A SYCL queue which might be used to create an array of default floating
310+
type. The `sycl_queue` can be ``None`` (the default), which is
311+
interpreted as to get the SYCL queue from `device` keyword if present
312+
or to use a default queue.
281313
282314
Returns
283315
-------
@@ -336,19 +368,16 @@ def get_dpnp_descriptor(
336368
2. We can not handle with input data object
337369
"""
338370

339-
# TODO need to allow "import dpnp" with no build procedure
340-
# if no_modules_load_doc_build();
341-
# return False
342-
343371
if use_origin_backend():
344372
return False
345373

346-
# It's required to keep track of input object if a non-strided copy is going to be created.
347-
# Thus there will be an extra descriptor allocated to refer on original input.
374+
# It's required to keep track of input object if a non-strided copy is
375+
# going to be created. Thus there will be an extra descriptor allocated
376+
# to refer on original input.
348377
orig_desc = None
349378

350379
# If input object is a scalar, it means it was allocated on host memory.
351-
# We need to copy it to USM memory according to compute follows data paradigm.
380+
# We need to copy it to USM memory according to compute follows data.
352381
if isscalar(ext_obj):
353382
ext_obj = array(
354383
ext_obj,
@@ -362,7 +391,8 @@ def get_dpnp_descriptor(
362391
# if function get implementation for strides case
363392
# then this behavior can be disabled with setting "copy_when_strides"
364393
if copy_when_strides and getattr(ext_obj, "strides", None) is not None:
365-
# TODO: replace this workaround when usm_ndarray will provide such functionality
394+
# TODO: replace this workaround when usm_ndarray will provide
395+
# such functionality
366396
shape_offsets = tuple(
367397
numpy.prod(ext_obj.shape[i + 1 :], dtype=numpy.int64)
368398
for i in range(ext_obj.ndim)
@@ -382,7 +412,8 @@ def get_dpnp_descriptor(
382412
# while dpnp functions are based on DPNP_QUEUE
383413
# we need to create a copy on device associated with DPNP_QUEUE
384414
# if function get implementation for different queue
385-
# then this behavior can be disabled with setting "copy_when_nondefault_queue"
415+
# then this behavior can be disabled with setting
416+
# "copy_when_nondefault_queue"
386417
queue = getattr(ext_obj, "sycl_queue", None)
387418
if queue is not None and copy_when_nondefault_queue:
388419
default_queue = dpctl.SyclQueue()
@@ -393,7 +424,7 @@ def get_dpnp_descriptor(
393424
ext_obj = array(ext_obj, sycl_queue=default_queue)
394425

395426
dpnp_desc = dpnp_descriptor(ext_obj, orig_desc)
396-
if dpnp_desc.is_valid:
427+
if dpnp_desc.is_valid: # pylint: disable=using-constant-test
397428
return dpnp_desc
398429

399430
return False
@@ -418,8 +449,8 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
418449
419450
If both arguments 'device' and 'sycl_queue' have default value ``None``
420451
and 'obj' has `sycl_queue` attribute, it assumes that Compute Follows Data
421-
approach has to be applied and so the resulting SYCL queue will be normalized
422-
based on the queue value from 'obj'.
452+
approach has to be applied and so the resulting SYCL queue will be
453+
normalized based on the queue value from 'obj'.
423454
424455
Parameters
425456
----------
@@ -443,15 +474,11 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
443474
Returns
444475
-------
445476
sycl_queue: dpctl.SyclQueue
446-
A :class:`dpctl.SyclQueue` object normalized by `normalize_queue_device` call
447-
of `dpctl.tensor` module invoked with 'device' and 'sycl_queue' values.
448-
If both incoming 'device' and 'sycl_queue' are None and 'obj' has `sycl_queue` attribute,
449-
the normalization will be performed for 'obj.sycl_queue' value.
450-
451-
Raises
452-
------
453-
TypeError
454-
If argument is not of the expected type, or keywords imply incompatible queues.
477+
A :class:`dpctl.SyclQueue` object normalized by
478+
`normalize_queue_device` call of `dpctl.tensor` module invoked with
479+
`device` and `sycl_queue` values. If both incoming `device` and
480+
`sycl_queue` are ``None`` and `obj` has `sycl_queue` attribute,
481+
the normalization will be performed for `obj.sycl_queue` value.
455482
456483
"""
457484

@@ -463,9 +490,7 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
463490
):
464491
sycl_queue = obj.sycl_queue
465492

466-
return dpt._device.normalize_queue_device(
467-
sycl_queue=sycl_queue, device=device
468-
)
493+
return normalize_queue_device(sycl_queue=sycl_queue, device=device)
469494

470495

471496
def get_result_array(a, out=None, casting="safe"):
@@ -494,21 +519,21 @@ def get_result_array(a, out=None, casting="safe"):
494519

495520
if out is None:
496521
return a
497-
else:
498-
if a is out:
499-
return out
500-
else:
501-
dpnp.check_supported_arrays_type(out)
502-
if out.shape != a.shape:
503-
raise ValueError(
504-
f"Output array of shape {a.shape} is needed, got {out.shape}."
505-
)
506-
elif isinstance(out, dpt.usm_ndarray):
507-
out = dpnp_array._create_from_usm_ndarray(out)
508522

509-
dpnp.copyto(out, a, casting=casting)
523+
if a is out:
524+
return out
525+
526+
dpnp.check_supported_arrays_type(out)
527+
if out.shape != a.shape:
528+
raise ValueError(
529+
f"Output array of shape {a.shape} is needed, got {out.shape}."
530+
)
531+
532+
if isinstance(out, dpt.usm_ndarray):
533+
out = dpnp_array._create_from_usm_ndarray(out)
510534

511-
return out
535+
dpnp.copyto(out, a, casting=casting)
536+
return out
512537

513538

514539
def get_usm_ndarray(a):
@@ -538,7 +563,7 @@ def get_usm_ndarray(a):
538563
if isinstance(a, dpt.usm_ndarray):
539564
return a
540565
raise TypeError(
541-
"An array must be any of supported type, but got {}".format(type(a))
566+
f"An array must be any of supported type, but got {type(a)}"
542567
)
543568

544569

0 commit comments

Comments
 (0)