Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replaced flake8 with ruff and removed setup.cfg by merging the configurations into pyproject.toml #27779

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ repos:
- id: trailing-whitespace
- id: check-toml
- id: end-of-file-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.8
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.12.0
hooks:
Expand All @@ -23,21 +29,10 @@ repos:
rev: v2.2.1
hooks:
- id: autoflake
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
exclude: ^.*__init__.py$
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
hooks:
- id: docformatter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.8
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
- repo: https://github.com/unifyai/lint-hook
rev: a72ffb17562d919311653d7f593cb537d1245c19
hooks:
Expand Down
2 changes: 1 addition & 1 deletion ivy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ class Node(str):
pass # Added for the finally statement
try:
from .compiler.replace_with import replace_with, transform_function
except:
except: # noqa: E722
pass
finally:
# Skip framework imports done by Ivy compiler for now
Expand Down
8 changes: 4 additions & 4 deletions ivy/data_classes/array/experimental/linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ def multi_mode_dot(
If True, the matrices or vectors in in the list are transposed.
For complex tensors, the conjugate transpose is used.
out
optional output array, for writing the result to. It must have a shape that the
result can broadcast to.
optional output array, for writing the result to.
It must have a shape that the result can broadcast to.

Returns
-------
Expand All @@ -334,8 +334,8 @@ def multi_mode_dot(
Notes
-----
If no modes are specified, just assumes there is one matrix or vector per mode and returns:
:math:`\\text{x }\\times_0 \\text{ matrix or vec list[0] }\\times_1 \\cdots \\times_n \\text{ matrix or vec list[n] }` # noqa
"""
:math:`\\text{x }\\times_0 \\text{ matrix or vec list[0] }\\times_1 \\cdots \\times_n \\text{ matrix or vec list[n] }`
""" # noqa: E501
return ivy.multi_mode_dot(
self._data, mat_or_vec_list, modes, skip, transpose, out=out
)
Expand Down
8 changes: 4 additions & 4 deletions ivy/data_classes/container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ def cont_from_disk_as_hdf5(
),
)
container_dict = {}
if type(h5_obj_or_filepath) is str:
if isinstance(h5_obj_or_filepath, str):
h5_obj = h5py.File(h5_obj_or_filepath, "r")
else:
h5_obj = h5_obj_or_filepath
Expand Down Expand Up @@ -1238,7 +1238,7 @@ def h5_file_size(h5_obj_or_filepath):
"the size of hdf5 files."
),
)
if type(h5_obj_or_filepath) is str:
if isinstance(h5_obj_or_filepath, str):
h5_obj = h5py.File(h5_obj_or_filepath, "r")
else:
h5_obj = h5_obj_or_filepath
Expand Down Expand Up @@ -1280,7 +1280,7 @@ def shuffle_h5_file(h5_obj_or_filepath, seed_value=0):
)
if seed_value is None:
seed_value = random.randint(0, 1000)
if type(h5_obj_or_filepath) is str:
if isinstance(h5_obj_or_filepath, str):
h5_obj = h5py.File(h5_obj_or_filepath, "a")
else:
h5_obj = h5_obj_or_filepath
Expand Down Expand Up @@ -1997,7 +1997,7 @@ def cont_to_disk_as_hdf5(
"containers to disk as hdf5 files."
),
)
if type(h5_obj_or_filepath) is str:
if isinstance(h5_obj_or_filepath, str):
h5_obj = h5py.File(h5_obj_or_filepath, mode)
else:
h5_obj = h5_obj_or_filepath
Expand Down
6 changes: 3 additions & 3 deletions ivy/data_classes/factorized_tensor/cp_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ def cp_lstsq_grad(cp_tensor, tensor, return_loss=False, mask=None):

.. math::

\nabla 0.5 ||\\mathcal{X} - [\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]||^2 # noqa
\nabla 0.5 ||\\mathcal{X} - [\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]||^2

where :math:`[\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]`
is the CP decomposition with weights
:math:`\\mathbf{w}` and factor matrices :math:`\\mathbf{A}`, :math:`\\mathbf{B}` and :math:`\\mathbf{C}`. # noqa
:math:`\\mathbf{w}` and factor matrices :math:`\\mathbf{A}`, :math:`\\mathbf{B}` and :math:`\\mathbf{C}`.
Note that this does not return the gradient
with respect to the weights even if CP is normalized.

Expand Down Expand Up @@ -444,7 +444,7 @@ def cp_lstsq_grad(cp_tensor, tensor, return_loss=False, mask=None):
loss : float
Scalar quantity of the loss function corresponding to cp_gradient. Only returned
if return_loss = True.
"""
""" # noqa: E501
ivy.CPTensor.validate_cp_tensor(cp_tensor)
_, factors = cp_tensor

Expand Down
2 changes: 1 addition & 1 deletion ivy/data_classes/factorized_tensor/parafac2_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def parafac2_to_slices(parafac2_tensor, validate=True):
weights = None

decomposition = weights, (A, B, C), projections
I, _ = A.shape
I, _ = A.shape # noqa: E741
return [
ivy.Parafac2Tensor.parafac2_to_slice(decomposition, i, validate=False)
for i in range(I)
Expand Down
10 changes: 5 additions & 5 deletions ivy/data_classes/factorized_tensor/tt_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def validate_tt_tensor(tt_tensor):
def tt_to_tensor(factors):
"""Return the full tensor whose TT decomposition is given by 'factors'.

Re-assembles 'factors', which represent a tensor in TT/Matrix-Product-State format # noqa: E501
Re-assembles 'factors', which represent a tensor in TT/Matrix-Product-State format
into the corresponding full tensor

Parameters
Expand All @@ -120,7 +120,7 @@ def tt_to_tensor(factors):
-------
output_tensor
tensor whose TT/MPS decomposition was given by 'factors'
"""
""" # noqa: E501
if isinstance(factors, (float, int)):
return factors

Expand Down Expand Up @@ -213,8 +213,8 @@ def validate_tt_rank(
shape of the tensor to decompose
rank
way to determine the rank, by default 'same'
if 'same': rank is computed to keep the number of parameters (at most) the same # noqa: E501
if float, computes a rank so as to keep rank percent of the original number of parameters # noqa: E501
if 'same': rank is computed to keep the number of parameters (at most) the same
if float, computes a rank so as to keep rank percent of the original number of parameters
if int or tuple, just returns rank
constant_rank
if True, the *same* rank will be chosen for each modes
Expand All @@ -233,7 +233,7 @@ def validate_tt_rank(
-------
rank
rank of the decomposition
"""
""" # noqa: E501
if rounding == "ceil":
rounding_fn = ivy.ceil
elif rounding == "floor":
Expand Down
2 changes: 1 addition & 1 deletion ivy/functional/backends/numpy/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def flip(
return x
if axis is None:
axis = list(range(num_dims))
if type(axis) is int:
if isinstance(axis, int):
axis = [axis]
axis = [item + num_dims if item < 0 else item for item in axis]
return np.flip(x, axis)
Expand Down
2 changes: 1 addition & 1 deletion ivy/functional/backends/tensorflow/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def flip(
new_axis = list(range(num_dims))
else:
new_axis = axis
if type(new_axis) is int:
if isinstance(new_axis, int):
new_axis = [new_axis]
else:
new_axis = new_axis
Expand Down
2 changes: 1 addition & 1 deletion ivy/functional/frontends/jax/numpy/manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def transpose(a, axes=None):
return a
if not axes:
axes = list(range(len(a.shape)))[::-1]
if type(axes) is int:
if isinstance(axes, int):
axes = [axes]
if (len(a.shape) == 0 and not axes) or (len(a.shape) == 1 and axes[0] == 0):
return a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def swapaxes(a, axis1, axis2):
def transpose(array, /, *, axes=None):
if not axes:
axes = list(range(len(array.shape)))[::-1]
if type(axes) is int:
if isinstance(axes, int):
axes = [axes]
if (len(array.shape) == 0 and not axes) or (len(array.shape) == 1 and axes[0] == 0):
return array
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/ivy/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def abs(
a: ivy.array([0., 2.6, 3.5]),
b: ivy.array([4.5, 5.3, 0., 2.3])
}
"""
""" # noqa: E501
return ivy.current_backend(x).abs(x, out=out)


Expand Down Expand Up @@ -6109,7 +6109,7 @@ def sqrt(
b: ivy.array([[7., 1.],
[0., 4.47]])
}
"""
""" # noqa: E501
return ivy.current_backend(x).sqrt(x, out=out)


Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/ivy/experimental/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ def max_pool1d(

[[16., 17., 18., 19.]]])
>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.max_pool1d(x, 2, 2, [(1,0)], data_format="NCW", dilation=2, ceil_mode=True)) # noqa
>>> print(ivy.max_pool1d(x, 2, 2, [(1,0)], data_format="NCW", dilation=2, ceil_mode=True))
ivy.array([[[ 1., 3.],
[ 5., 7.],
[ 9., 11.]],

[[13., 15.],
[17., 19.],
[21., 23.]]])
"""
""" # noqa: E501
return ivy.current_backend(x).max_pool1d(
x,
kernel,
Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/ivy/experimental/linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,8 @@ def multi_mode_dot(
Notes
-----
If no modes are specified, just assumes there is one matrix or vector per mode and returns:
:math:`\\text{x }\\times_0 \\text{ matrix or vec list[0] }\\times_1 \\cdots \\times_n \\text{ matrix or vec list[n] }` # noqa
"""
:math:`\\text{x }\\times_0 \\text{ matrix or vec list[0] }\\times_1 \\cdots \\times_n \\text{ matrix or vec list[n] }`
""" # noqa: E501
if modes is None:
modes = range(len(mat_or_vec_list))

Expand Down
4 changes: 2 additions & 2 deletions ivy/functional/ivy/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def binary_cross_entropy(
>>> x = ivy.array([[0, 1, 1, 0]])
>>> y = ivy.array([[2.6, 6.2, 3.7, 5.3]])
>>> pos_weight = ivy.array([1, 2, 3, 4])
>>> z = ivy.binary_cross_entropy(x, y, pos_weight=pos_weight, from_logits=True, reduction='sum', axis=1) # noqa: E501
>>> z = ivy.binary_cross_entropy(x, y, pos_weight=pos_weight, from_logits=True, reduction='sum', axis=1)
ivy.array([8.05393649])

>>> x = ivy.array([[0, 1, 1, 0]])
Expand Down Expand Up @@ -216,7 +216,7 @@ def binary_cross_entropy(
>>> z = ivy.binary_cross_entropy(x, y)
>>> print(z)
ivy.array([0.223, 0.223, 0.223, 0.223])
"""
""" # noqa: E501
ivy.utils.assertions.check_elem_in_list(reduction, ["none", "sum", "mean"])

if not (0.0 <= epsilon <= 1.0):
Expand Down
4 changes: 2 additions & 2 deletions ivy/utils/backend/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _determine_backend_from_args(args):
>>> x = jnp.array([1])
>>> print(_determine_backend_from_args(x))
<module 'ivy.functional.backends.jax' from '/ivy/ivy/functional/backends/jax/__init__.py'> # noqa
"""
""" # noqa: E501
arg_type = type(args)
if isinstance(args, ivy.Array):
args = args.data
Expand Down Expand Up @@ -175,7 +175,7 @@ def current_backend(*args, **kwargs):
>>> x = np.array([2.0])
>>> print(ivy.current_backend(x))
<module 'ivy.functional.backends.jax' from '/ivy/ivy/functional/backends/jax/__init__.py'> # noqa
"""
""" # noqa: E501
global implicit_backend
# if a global backend has been set with
# set_backend then this will be returned
Expand Down
4 changes: 2 additions & 2 deletions ivy_tests/test_ivy/test_frontends/test_paddle/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def test_paddle_eig(
ret = [ivy.to_numpy(x).astype("float64") for x in ret]
frontend_ret = [np.asarray(x, dtype=np.float64) for x in frontend_ret]

l, v = ret
l, v = ret # noqa: E741
front_l, front_v = frontend_ret

assert_all_close(
Expand Down Expand Up @@ -619,7 +619,7 @@ def test_paddle_eigh(
ret = [ivy.to_numpy(x).astype("float64") for x in ret]
frontend_ret = [np.asarray(x, dtype=np.float64) for x in frontend_ret]

l, v = ret
l, v = ret # noqa: E741
front_l, front_v = frontend_ret

assert_all_close(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def test_torch_eig(
ret = [ivy.to_numpy(x).astype("float64") for x in ret]
frontend_ret = [np.asarray(x, dtype=np.float64) for x in frontend_ret]

l, v = ret
l, v = ret # noqa: E741
front_l, front_v = frontend_ret

assert_all_close(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def test_num_cpu_cores(backend_fw):
# using multiprocessing module too because ivy uses psutil as basis.
p_cpu_cores = psutil.cpu_count()
m_cpu_cores = multiprocessing.cpu_count()
assert type(ivy_backend.num_cpu_cores()) == int
assert isinstance(ivy_backend.num_cpu_cores(), int)
assert ivy_backend.num_cpu_cores() == p_cpu_cores
assert ivy_backend.num_cpu_cores() == m_cpu_cores

Expand Down
38 changes: 34 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,41 @@ requires = [
]
build-backend = "setuptools.build_meta"


[tool.docformatter]
wrap-summaries = 88
pre-summary-newline = true


[tool.autoflake]
in-place = true
remove-all-unused-imports = true
ignore-init-module-imports = true
remove-duplicate-keys = true
remove-unused-variables = true
quiet = true
ignore-pass-after-docstring = true
exclude = ["__init__.py"]


[tool.ruff]
line-length = 88
target-version = "py38"

select = ["D"] # Enabling Only pydocstyle checks as of now (https://docs.astral.sh/ruff/rules/#pydocstyle-d)
select = [
# pyflakes
"F",
# pycodestyle
"E", "W",
# pydocstyle
"D"
]


ignore = [
"E203", # Whitespace-before-punctuation.
"E402", # Module-import-not-at-top-of-file.
"E731", # Do not assign a lambda expression, use a def.
"D100", # Missing docstring in public module.
"D101", # Missing docstring in public class.
"D102", # Missing docstring in public method.
Expand All @@ -32,6 +61,7 @@ ignore = [
"D417", # Missing argument description in the docstring for argument "X".
]

exclude = [
'ivy/functional/(frontends|backends)/(?!.*/func_wrapper\.py$).*(?!__init__\.py$)',
]
[tool.ruff.per-file-ignores]
'ivy/functional/(frontends|backends)/(?!.*/func_wrapper\.py$).*(?!__init__\.py$)' = ["D"]
"**/__init__.py" = ["F401","F403","F405","F811","F821", "E501"]
"ivy/functional/frontends/paddle/**" = ["F401", "F403", "F405"]
Loading
Loading