Skip to content

Commit e488c6e

Browse files
authored
Replace Black with Ruff formatter (keras-team#20445)
1 parent c052cea commit e488c6e

File tree

74 files changed

+97
-179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+97
-179
lines changed

.devcontainer/devcontainer.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
"source.organizeImports": true
1111
},
1212
"[python]": {
13-
"editor.defaultFormatter": "ms-python.black-formatter"
13+
"editor.defaultFormatter": "charliermarsh.ruff"
1414
},
1515
"editor.rulers": [
1616
80
1717
]
1818
},
1919
"extensions": [
2020
"charliermarsh.ruff",
21-
"ms-python.python",
22-
"ms-python.black-formatter"
21+
"ms-python.python"
2322
]
2423
}
2524
},

CONTRIBUTING.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ section of the README.
109109

110110
## Code style
111111

112-
Keras uses [Black](https://black.readthedocs.io/en/stable/) and
113-
[Ruff](https://docs.astral.sh/ruff/) to format the code. Please refer to
112+
Keras uses [Ruff](https://docs.astral.sh/ruff/) to format the code. Please refer to
114113
[requirements-common.txt](https://github.com/keras-team/keras/blob/master/requirements-common.txt)
115114
for the required versions. Run the following command **at the root directory of
116115
the repo** to format your code.

examples/demo_custom_layer_backend_agnostic.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ def __init__(self, rate, name=None):
4747

4848
def call(self, inputs):
4949
# Use `keras.random` for random ops.
50-
return keras.random.dropout(
51-
inputs, self.rate, seed=self.seed_generator
52-
)
50+
return keras.random.dropout(inputs, self.rate, seed=self.seed_generator)
5351

5452

5553
class MyModel(Model):

examples/demo_jax_distributed.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
BATCH_SIZE = 192
2929

30-
(x_train, train_labels), (
31-
x_eval,
32-
eval_labels,
30+
(
31+
(x_train, train_labels),
32+
(x_eval, eval_labels),
3333
) = keras.datasets.mnist.load_data()
3434
x_train = np.expand_dims(x_train, axis=-1).astype(
3535
np.float32

guides/making_new_layers_and_models_via_subclassing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def __init__(
643643
intermediate_dim=64,
644644
latent_dim=32,
645645
name="autoencoder",
646-
**kwargs
646+
**kwargs,
647647
):
648648
super().__init__(name=name, **kwargs)
649649
self.original_dim = original_dim

integration_tests/dataset_tests/boston_housing_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
class BostonHousingTest(testing.TestCase):
6-
76
def test_load_data(self):
87
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()
98
self.assertEqual(x_train.shape[1], 13)

integration_tests/dataset_tests/california_housing_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
class CaliforniaHousingTest(testing.TestCase):
6-
76
def test_load_data_large(self):
87
(x_train, y_train), (x_test, y_test) = california_housing.load_data(
98
version="large"

integration_tests/model_visualization_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def get_edge_dict(dot):
4141

4242

4343
class ModelVisualizationTest(testing.TestCase):
44-
4544
def test_plot_sequential_model(self):
4645
model = keras.Sequential(
4746
[

keras/src/backend/common/masking_test.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class MaskingTest(testing.TestCase):
9-
109
def test_mask_on_eager_tensor(self):
1110
x = ops.zeros((2, 3))
1211
self.assertIsNone(get_keras_mask(x))
@@ -25,7 +24,6 @@ def test_mask_on_eager_tensor(self):
2524
self.assertIsNone(get_keras_mask(x))
2625

2726
def test_mask_on_tracer_tensor(self):
28-
2927
def fn(x):
3028
self.assertIsNone(get_keras_mask(x))
3129

keras/src/backend/common/symbolic_scope_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
class TestSymbolicScope(testing.TestCase):
1010
def test_basic_flow(self):
11-
1211
# Define a function that behaves differently according to
1312
# `in_symbolic_scope`.
1413
def compute_loss(y, y_pred):

keras/src/backend/jax/core.py

-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ def convert_keras_tensor_to_jax(x, fill_value=None):
153153
return x
154154

155155
def wrapped_fn(*args, **kwargs):
156-
157156
# Turn inputs that are sparse to BCOO tensors
158157
def to_bcoo_if_sparse(x, maybe_symbolic_x):
159158
if (

keras/src/backend/jax/optimizer.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414

1515
class JaxOptimizer(base_optimizer.BaseOptimizer):
16-
1716
def _backend_apply_gradients(self, grads, trainable_variables):
1817
if self.gradient_accumulation_steps:
1918
is_update_step = (

keras/src/backend/jax/trainer.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,9 @@ def fit(
354354
# Create the validation data using the training data. Only supported
355355
# for TF/numpy/jax arrays.
356356
(
357-
x,
358-
y,
359-
sample_weight,
360-
), validation_data = array_slicing.train_validation_split(
357+
(x, y, sample_weight),
358+
validation_data,
359+
) = array_slicing.train_validation_split(
361360
(x, y, sample_weight), validation_split=validation_split
362361
)
363362

keras/src/backend/tensorflow/core.py

-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ def _base_case():
499499
)
500500

501501
def _recursive_case():
502-
503502
odd_elems = _scan(reduced_elems)
504503

505504
def _even_length_case():

keras/src/backend/tensorflow/optimizer.py

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919

2020
class TFOptimizer(KerasAutoTrackable, base_optimizer.BaseOptimizer):
21-
2221
def __init__(self, *args, **kwargs):
2322
super().__init__(*args, **kwargs)
2423
self._distribution_strategy = tf.distribute.get_strategy()

keras/src/backend/tensorflow/saved_model_test.py

-4
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,18 @@ def call(self, inputs):
150150
named_product(struct_type=["tuple", "array", "dict"])
151151
)
152152
def test_model_with_input_structure(self, struct_type):
153-
154153
class TupleModel(models.Model):
155-
156154
def call(self, inputs):
157155
x, y = inputs
158156
return x + ops.mean(y, axis=1)
159157

160158
class ArrayModel(models.Model):
161-
162159
def call(self, inputs):
163160
x = inputs[0]
164161
y = inputs[1]
165162
return x + ops.mean(y, axis=1)
166163

167164
class DictModel(models.Model):
168-
169165
def call(self, inputs):
170166
x = inputs["x"]
171167
y = inputs["y"]

keras/src/backend/tensorflow/trainer.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def predict_step(self, data):
9999
return y_pred
100100

101101
def _make_function(self, step_function):
102-
103102
@tf.autograph.experimental.do_not_convert
104103
def one_step_on_data(data):
105104
"""Runs a single training step on a batch of data."""
@@ -271,10 +270,9 @@ def fit(
271270
# Create the validation data using the training data. Only supported
272271
# for TF/numpy/jax arrays.
273272
(
274-
x,
275-
y,
276-
sample_weight,
277-
), validation_data = array_slicing.train_validation_split(
273+
(x, y, sample_weight),
274+
validation_data,
275+
) = array_slicing.train_validation_split(
278276
(x, y, sample_weight), validation_split=validation_split
279277
)
280278

keras/src/backend/torch/trainer.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,9 @@ def fit(
195195
# for TF/numpy/jax arrays.
196196
# TODO: Support torch tensors for validation data.
197197
(
198-
x,
199-
y,
200-
sample_weight,
201-
), validation_data = array_slicing.train_validation_split(
198+
(x, y, sample_weight),
199+
validation_data,
200+
) = array_slicing.train_validation_split(
202201
(x, y, sample_weight), validation_split=validation_split
203202
)
204203

keras/src/datasets/reuters.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ def load_data(
124124
xs = [[w for w in x if skip_top <= w < num_words] for x in xs]
125125

126126
idx = int(len(xs) * (1 - test_split))
127-
x_train, y_train = np.array(xs[:idx], dtype="object"), np.array(
128-
labels[:idx]
127+
x_train, y_train = (
128+
np.array(xs[:idx], dtype="object"),
129+
np.array(labels[:idx]),
129130
)
130131
x_test, y_test = np.array(xs[idx:], dtype="object"), np.array(labels[idx:])
131132

keras/src/export/export_lib.py

-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ def track(self, resource):
166166
# Variables in the lists below are actually part of the trackables
167167
# that get saved, because the lists are created in __init__.
168168
if backend.backend() == "jax":
169-
170169
trainable_variables = tree.flatten(resource.trainable_variables)
171170
non_trainable_variables = tree.flatten(
172171
resource.non_trainable_variables
@@ -328,7 +327,6 @@ def serving_fn(x):
328327
fn, input_signature=input_signature, autograph=False
329328
)
330329
else: # JAX backend
331-
332330
# 1. Create a stateless wrapper for `fn`
333331
# 2. jax2tf the stateless wrapper
334332
# 3. Create a stateful function that binds the variables with

keras/src/export/export_lib_test.py

-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def test_standard_model_export(self, model_type):
7474
named_product(model_type=["sequential", "functional", "subclass"])
7575
)
7676
def test_model_with_rng_export(self, model_type):
77-
7877
class RandomLayer(layers.Layer):
7978
def __init__(self):
8079
super().__init__()
@@ -104,7 +103,6 @@ def call(self, inputs):
104103
named_product(model_type=["sequential", "functional", "subclass"])
105104
)
106105
def test_model_with_non_trainable_state_export(self, model_type):
107-
108106
class StateLayer(layers.Layer):
109107
def __init__(self):
110108
super().__init__()
@@ -151,22 +149,18 @@ def test_model_with_tf_data_layer(self, model_type):
151149
named_product(struct_type=["tuple", "array", "dict"])
152150
)
153151
def test_model_with_input_structure(self, struct_type):
154-
155152
class TupleModel(models.Model):
156-
157153
def call(self, inputs):
158154
x, y = inputs
159155
return ops.add(x, y)
160156

161157
class ArrayModel(models.Model):
162-
163158
def call(self, inputs):
164159
x = inputs[0]
165160
y = inputs[1]
166161
return ops.add(x, y)
167162

168163
class DictModel(models.Model):
169-
170164
def call(self, inputs):
171165
x = inputs["x"]
172166
y = inputs["y"]
@@ -214,7 +208,6 @@ def call(self, inputs):
214208
export_lib.export_model(revived_model, self.get_temp_dir())
215209

216210
def test_model_with_multiple_inputs(self):
217-
218211
class TwoInputsModel(models.Model):
219212
def call(self, x, y):
220213
return x + y
@@ -302,7 +295,6 @@ def test_low_level_model_export_with_alias(self):
302295
named_product(model_type=["sequential", "functional", "subclass"])
303296
)
304297
def test_low_level_model_export_with_dynamic_dims(self, model_type):
305-
306298
class ReductionLayer(layers.Layer):
307299
def call(self, inputs):
308300
return ops.max(inputs, axis=1)
@@ -382,7 +374,6 @@ def test_low_level_model_export_with_jax2tf_kwargs(self):
382374
reason="This test is only for the JAX backend.",
383375
)
384376
def test_low_level_model_export_with_jax2tf_polymorphic_shapes(self):
385-
386377
class SquareLayer(layers.Layer):
387378
def call(self, inputs):
388379
return ops.matmul(inputs, inputs)

keras/src/layers/activations/prelu.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(
3737
alpha_regularizer=None,
3838
alpha_constraint=None,
3939
shared_axes=None,
40-
**kwargs
40+
**kwargs,
4141
):
4242
super().__init__(**kwargs)
4343
self.supports_masking = True

keras/src/layers/convolutional/conv1d.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(
110110
activity_regularizer=None,
111111
kernel_constraint=None,
112112
bias_constraint=None,
113-
**kwargs
113+
**kwargs,
114114
):
115115
super().__init__(
116116
rank=1,
@@ -130,7 +130,7 @@ def __init__(
130130
activity_regularizer=activity_regularizer,
131131
kernel_constraint=kernel_constraint,
132132
bias_constraint=bias_constraint,
133-
**kwargs
133+
**kwargs,
134134
)
135135

136136
def _compute_causal_padding(self):

keras/src/layers/convolutional/conv1d_transpose.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __init__(
108108
activity_regularizer=None,
109109
kernel_constraint=None,
110110
bias_constraint=None,
111-
**kwargs
111+
**kwargs,
112112
):
113113
super().__init__(
114114
rank=1,
@@ -127,5 +127,5 @@ def __init__(
127127
activity_regularizer=activity_regularizer,
128128
kernel_constraint=kernel_constraint,
129129
bias_constraint=bias_constraint,
130-
**kwargs
130+
**kwargs,
131131
)

keras/src/layers/convolutional/conv2d.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(
104104
activity_regularizer=None,
105105
kernel_constraint=None,
106106
bias_constraint=None,
107-
**kwargs
107+
**kwargs,
108108
):
109109
super().__init__(
110110
rank=2,
@@ -124,5 +124,5 @@ def __init__(
124124
activity_regularizer=activity_regularizer,
125125
kernel_constraint=kernel_constraint,
126126
bias_constraint=bias_constraint,
127-
**kwargs
127+
**kwargs,
128128
)

keras/src/layers/convolutional/conv2d_transpose.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(
110110
activity_regularizer=None,
111111
kernel_constraint=None,
112112
bias_constraint=None,
113-
**kwargs
113+
**kwargs,
114114
):
115115
super().__init__(
116116
rank=2,
@@ -129,5 +129,5 @@ def __init__(
129129
activity_regularizer=activity_regularizer,
130130
kernel_constraint=kernel_constraint,
131131
bias_constraint=bias_constraint,
132-
**kwargs
132+
**kwargs,
133133
)

keras/src/layers/convolutional/conv3d.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(
110110
activity_regularizer=None,
111111
kernel_constraint=None,
112112
bias_constraint=None,
113-
**kwargs
113+
**kwargs,
114114
):
115115
super().__init__(
116116
rank=3,
@@ -130,5 +130,5 @@ def __init__(
130130
activity_regularizer=activity_regularizer,
131131
kernel_constraint=kernel_constraint,
132132
bias_constraint=bias_constraint,
133-
**kwargs
133+
**kwargs,
134134
)

0 commit comments

Comments
 (0)