Skip to content

Commit

Permalink
Replace isort and flake8 with Ruff checker (#20442)
Browse files Browse the repository at this point in the history
* Replace isort and flake8 with Ruff checker

* Resolve issue with shell/api_gen.sh and correction to fix/check logic

* Resolve E721 to use `is` and `is not` for type comparisons
  • Loading branch information
mwtoews authored Nov 4, 2024
1 parent 0adc924 commit 192b7b2
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 63 deletions.
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
]
},
"extensions": [
"charliermarsh.ruff",
"ms-python.python",
"ms-python.isort",
"ms-python.flake8",
"ms-python.black-formatter"
]
}
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ section of the README.
## Code style

Keras uses [Black](https://black.readthedocs.io/en/stable/) and
[isort](https://pycqa.github.io/isort/) to format the code. Please refer to
[Ruff](https://docs.astral.sh/ruff/) to format the code. Please refer to
[requirements-common.txt](https://github.com/keras-team/keras/blob/master/requirements-common.txt)
for the required versions. Run the following command **at the root directory of
the repo** to format your code.
Expand Down
4 changes: 2 additions & 2 deletions keras/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

# DO NOT EDIT. Generated by api_gen.sh
from keras.api import DTypePolicy
from keras.api import FloatDTypePolicy
Expand Down Expand Up @@ -54,6 +52,8 @@

# END DO NOT EDIT.

import os # isort: skip

# Add everything in /api/ to the module search path.
__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405

Expand Down
4 changes: 2 additions & 2 deletions keras/src/callbacks/tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TensorBoard(Callback):
Batch-level summary writing is also available via `train_step`
override. Please see
[TensorBoard Scalars tutorial](
https://www.tensorflow.org/tensorboard/scalars_and_keras#batch-level_logging) # noqa: E501
https://www.tensorflow.org/tensorboard/scalars_and_keras#batch-level_logging)
for more details.
profile_batch: (Not supported at this time)
Profile the batch(es) to sample compute characteristics.
Expand Down Expand Up @@ -152,7 +152,7 @@ def my_summary(x):
log_dir='./logs', profile_batch=(10,20))
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])
```
"""
""" # noqa: E501

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion keras/src/models/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def test_restored_multi_output_type(self, out_type):
x = layers.Dense(5)(inputs)
output_a = layers.Dense(4)(x)
output_b = layers.Dense(5)(x)
if dict == out_type:
if out_type is dict:
outputs = {"a": output_a, "b": output_b}
else:
outputs = out_type([output_a, output_b])
Expand Down
4 changes: 2 additions & 2 deletions keras/src/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,11 @@ def inject_functional_model_class(cls):
"""Inject `Functional` into the hierarchy of this class if needed."""
from keras.src.models import functional

if cls == Model:
if cls is Model:
return functional.Functional
# In case there is any multiple inheritance, we stop injecting the
# class if keras model is not in its class hierarchy.
if cls == object:
if cls is object:
return object

cls.__bases__ = tuple(
Expand Down
2 changes: 1 addition & 1 deletion keras/src/trainers/data_adapters/array_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def convert_single_array(x):
# Step 2. Normalize floats to floatx.
def is_non_floatx_float(dtype):
return (
not dtype == object
dtype is not object
and backend.is_float_dtype(dtype)
and not backend.standardize_dtype(dtype) == backend.floatx()
)
Expand Down
2 changes: 1 addition & 1 deletion keras/src/utils/sequence_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def pad_sequences(
is_dtype_str = np.issubdtype(dtype, np.str_) or np.issubdtype(
dtype, np.str_
)
if isinstance(value, str) and dtype != object and not is_dtype_str:
if isinstance(value, str) and dtype is not object and not is_dtype_str:
raise ValueError(
f"`dtype` {dtype} is not compatible with `value`'s type: "
f"{type(value)}\nYou should set `dtype=object` for variable length "
Expand Down
32 changes: 23 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,31 @@ extend-exclude = """
)
"""

[tool.isort]
profile = "black"
force_single_line = "True"
known_first_party = ["keras", "tests"]
default_section = "THIRDPARTY"
line_length = 80
extend_skip_glob=[
"examples/*",
"guides/*",
[tool.ruff]
line-length = 80

[tool.ruff.lint]
select = [
"E", # pycodestyle error
"F", # Pyflakes
"I", # isort
]
ignore = [
"E722", # do not use bare 'except'
"E741", # ambiguous variable name
"E731", # do not assign a `lambda` expression, use a `def`
]

[tool.ruff.lint.per-file-ignores]
"**/__init__.py" = ["E501", "F401"] # lines too long; imported but unused
"**/random.py" = ["F401"] # imported but unused
"examples/*" = ["I", "E"]
"guides/*" = ["I", "E", "F"]

[tool.ruff.lint.isort]
force-single-line = true
known-first-party = ["keras"]

[tool.pytest.ini_options]
filterwarnings = [
"error",
Expand Down
3 changes: 1 addition & 2 deletions requirements-common.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namex>=0.0.8
black>=22
flake8
isort
ruff
pytest
numpy
scipy
Expand Down
34 changes: 0 additions & 34 deletions setup.cfg

This file was deleted.

4 changes: 1 addition & 3 deletions shell/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ set -Eeuo pipefail

base_dir=$(dirname $(dirname $0))

isort --sp "${base_dir}/pyproject.toml" .
ruff check --exit-zero --config "${base_dir}/pyproject.toml" --fix .

black --config "${base_dir}/pyproject.toml" .

flake8 --config "${base_dir}/setup.cfg" .

4 changes: 1 addition & 3 deletions shell/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ set -Eeuo pipefail

base_dir=$(dirname $(dirname $0))

isort --sp "${base_dir}/pyproject.toml" --check .
ruff check --exit-zero --config "${base_dir}/pyproject.toml" .

black --config "${base_dir}/pyproject.toml" --check .

flake8 --config "${base_dir}/setup.cfg" .

0 comments on commit 192b7b2

Please sign in to comment.