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

Replace Black with Ruff formatter #20445

Merged
merged 1 commit into from
Nov 5, 2024
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
5 changes: 2 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
"source.organizeImports": true
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "charliermarsh.ruff"
},
"editor.rulers": [
80
]
},
"extensions": [
"charliermarsh.ruff",
"ms-python.python",
"ms-python.black-formatter"
"ms-python.python"
]
}
},
Expand Down
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ section of the README.

## Code style

Keras uses [Black](https://black.readthedocs.io/en/stable/) and
[Ruff](https://docs.astral.sh/ruff/) to format the code. Please refer to
Keras uses [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: 1 addition & 3 deletions examples/demo_custom_layer_backend_agnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def __init__(self, rate, name=None):

def call(self, inputs):
# Use `keras.random` for random ops.
return keras.random.dropout(
inputs, self.rate, seed=self.seed_generator
)
return keras.random.dropout(inputs, self.rate, seed=self.seed_generator)


class MyModel(Model):
Expand Down
6 changes: 3 additions & 3 deletions examples/demo_jax_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

BATCH_SIZE = 192

(x_train, train_labels), (
x_eval,
eval_labels,
(
(x_train, train_labels),
(x_eval, eval_labels),
) = keras.datasets.mnist.load_data()
x_train = np.expand_dims(x_train, axis=-1).astype(
np.float32
Expand Down
2 changes: 1 addition & 1 deletion guides/making_new_layers_and_models_via_subclassing.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def __init__(
intermediate_dim=64,
latent_dim=32,
name="autoencoder",
**kwargs
**kwargs,
):
super().__init__(name=name, **kwargs)
self.original_dim = original_dim
Expand Down
1 change: 0 additions & 1 deletion integration_tests/dataset_tests/boston_housing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class BostonHousingTest(testing.TestCase):

def test_load_data(self):
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()
self.assertEqual(x_train.shape[1], 13)
Expand Down
1 change: 0 additions & 1 deletion integration_tests/dataset_tests/california_housing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class CaliforniaHousingTest(testing.TestCase):

def test_load_data_large(self):
(x_train, y_train), (x_test, y_test) = california_housing.load_data(
version="large"
Expand Down
1 change: 0 additions & 1 deletion integration_tests/model_visualization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def get_edge_dict(dot):


class ModelVisualizationTest(testing.TestCase):

def test_plot_sequential_model(self):
model = keras.Sequential(
[
Expand Down
2 changes: 0 additions & 2 deletions keras/src/backend/common/masking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class MaskingTest(testing.TestCase):

def test_mask_on_eager_tensor(self):
x = ops.zeros((2, 3))
self.assertIsNone(get_keras_mask(x))
Expand All @@ -25,7 +24,6 @@ def test_mask_on_eager_tensor(self):
self.assertIsNone(get_keras_mask(x))

def test_mask_on_tracer_tensor(self):

def fn(x):
self.assertIsNone(get_keras_mask(x))

Expand Down
1 change: 0 additions & 1 deletion keras/src/backend/common/symbolic_scope_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class TestSymbolicScope(testing.TestCase):
def test_basic_flow(self):

# Define a function that behaves differently according to
# `in_symbolic_scope`.
def compute_loss(y, y_pred):
Expand Down
1 change: 0 additions & 1 deletion keras/src/backend/jax/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def convert_keras_tensor_to_jax(x, fill_value=None):
return x

def wrapped_fn(*args, **kwargs):

# Turn inputs that are sparse to BCOO tensors
def to_bcoo_if_sparse(x, maybe_symbolic_x):
if (
Expand Down
1 change: 0 additions & 1 deletion keras/src/backend/jax/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class JaxOptimizer(base_optimizer.BaseOptimizer):

def _backend_apply_gradients(self, grads, trainable_variables):
if self.gradient_accumulation_steps:
is_update_step = (
Expand Down
7 changes: 3 additions & 4 deletions keras/src/backend/jax/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,9 @@ def fit(
# Create the validation data using the training data. Only supported
# for TF/numpy/jax arrays.
(
x,
y,
sample_weight,
), validation_data = array_slicing.train_validation_split(
(x, y, sample_weight),
validation_data,
) = array_slicing.train_validation_split(
(x, y, sample_weight), validation_split=validation_split
)

Expand Down
1 change: 0 additions & 1 deletion keras/src/backend/tensorflow/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ def _base_case():
)

def _recursive_case():

odd_elems = _scan(reduced_elems)

def _even_length_case():
Expand Down
1 change: 0 additions & 1 deletion keras/src/backend/tensorflow/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


class TFOptimizer(KerasAutoTrackable, base_optimizer.BaseOptimizer):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._distribution_strategy = tf.distribute.get_strategy()
Expand Down
4 changes: 0 additions & 4 deletions keras/src/backend/tensorflow/saved_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,18 @@ def call(self, inputs):
named_product(struct_type=["tuple", "array", "dict"])
)
def test_model_with_input_structure(self, struct_type):

class TupleModel(models.Model):

def call(self, inputs):
x, y = inputs
return x + ops.mean(y, axis=1)

class ArrayModel(models.Model):

def call(self, inputs):
x = inputs[0]
y = inputs[1]
return x + ops.mean(y, axis=1)

class DictModel(models.Model):

def call(self, inputs):
x = inputs["x"]
y = inputs["y"]
Expand Down
8 changes: 3 additions & 5 deletions keras/src/backend/tensorflow/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def predict_step(self, data):
return y_pred

def _make_function(self, step_function):

@tf.autograph.experimental.do_not_convert
def one_step_on_data(data):
"""Runs a single training step on a batch of data."""
Expand Down Expand Up @@ -271,10 +270,9 @@ def fit(
# Create the validation data using the training data. Only supported
# for TF/numpy/jax arrays.
(
x,
y,
sample_weight,
), validation_data = array_slicing.train_validation_split(
(x, y, sample_weight),
validation_data,
) = array_slicing.train_validation_split(
(x, y, sample_weight), validation_split=validation_split
)

Expand Down
7 changes: 3 additions & 4 deletions keras/src/backend/torch/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,9 @@ def fit(
# for TF/numpy/jax arrays.
# TODO: Support torch tensors for validation data.
(
x,
y,
sample_weight,
), validation_data = array_slicing.train_validation_split(
(x, y, sample_weight),
validation_data,
) = array_slicing.train_validation_split(
(x, y, sample_weight), validation_split=validation_split
)

Expand Down
5 changes: 3 additions & 2 deletions keras/src/datasets/reuters.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ def load_data(
xs = [[w for w in x if skip_top <= w < num_words] for x in xs]

idx = int(len(xs) * (1 - test_split))
x_train, y_train = np.array(xs[:idx], dtype="object"), np.array(
labels[:idx]
x_train, y_train = (
np.array(xs[:idx], dtype="object"),
np.array(labels[:idx]),
)
x_test, y_test = np.array(xs[idx:], dtype="object"), np.array(labels[idx:])

Expand Down
2 changes: 0 additions & 2 deletions keras/src/export/export_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ def track(self, resource):
# Variables in the lists below are actually part of the trackables
# that get saved, because the lists are created in __init__.
if backend.backend() == "jax":

trainable_variables = tree.flatten(resource.trainable_variables)
non_trainable_variables = tree.flatten(
resource.non_trainable_variables
Expand Down Expand Up @@ -328,7 +327,6 @@ def serving_fn(x):
fn, input_signature=input_signature, autograph=False
)
else: # JAX backend

# 1. Create a stateless wrapper for `fn`
# 2. jax2tf the stateless wrapper
# 3. Create a stateful function that binds the variables with
Expand Down
9 changes: 0 additions & 9 deletions keras/src/export/export_lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def test_standard_model_export(self, model_type):
named_product(model_type=["sequential", "functional", "subclass"])
)
def test_model_with_rng_export(self, model_type):

class RandomLayer(layers.Layer):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -104,7 +103,6 @@ def call(self, inputs):
named_product(model_type=["sequential", "functional", "subclass"])
)
def test_model_with_non_trainable_state_export(self, model_type):

class StateLayer(layers.Layer):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -151,22 +149,18 @@ def test_model_with_tf_data_layer(self, model_type):
named_product(struct_type=["tuple", "array", "dict"])
)
def test_model_with_input_structure(self, struct_type):

class TupleModel(models.Model):

def call(self, inputs):
x, y = inputs
return ops.add(x, y)

class ArrayModel(models.Model):

def call(self, inputs):
x = inputs[0]
y = inputs[1]
return ops.add(x, y)

class DictModel(models.Model):

def call(self, inputs):
x = inputs["x"]
y = inputs["y"]
Expand Down Expand Up @@ -214,7 +208,6 @@ def call(self, inputs):
export_lib.export_model(revived_model, self.get_temp_dir())

def test_model_with_multiple_inputs(self):

class TwoInputsModel(models.Model):
def call(self, x, y):
return x + y
Expand Down Expand Up @@ -302,7 +295,6 @@ def test_low_level_model_export_with_alias(self):
named_product(model_type=["sequential", "functional", "subclass"])
)
def test_low_level_model_export_with_dynamic_dims(self, model_type):

class ReductionLayer(layers.Layer):
def call(self, inputs):
return ops.max(inputs, axis=1)
Expand Down Expand Up @@ -382,7 +374,6 @@ def test_low_level_model_export_with_jax2tf_kwargs(self):
reason="This test is only for the JAX backend.",
)
def test_low_level_model_export_with_jax2tf_polymorphic_shapes(self):

class SquareLayer(layers.Layer):
def call(self, inputs):
return ops.matmul(inputs, inputs)
Expand Down
2 changes: 1 addition & 1 deletion keras/src/layers/activations/prelu.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
alpha_regularizer=None,
alpha_constraint=None,
shared_axes=None,
**kwargs
**kwargs,
):
super().__init__(**kwargs)
self.supports_masking = True
Expand Down
4 changes: 2 additions & 2 deletions keras/src/layers/convolutional/conv1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs
**kwargs,
):
super().__init__(
rank=1,
Expand All @@ -130,7 +130,7 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)

def _compute_causal_padding(self):
Expand Down
4 changes: 2 additions & 2 deletions keras/src/layers/convolutional/conv1d_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs
**kwargs,
):
super().__init__(
rank=1,
Expand All @@ -127,5 +127,5 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions keras/src/layers/convolutional/conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs
**kwargs,
):
super().__init__(
rank=2,
Expand All @@ -124,5 +124,5 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions keras/src/layers/convolutional/conv2d_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs
**kwargs,
):
super().__init__(
rank=2,
Expand All @@ -129,5 +129,5 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)
4 changes: 2 additions & 2 deletions keras/src/layers/convolutional/conv3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs
**kwargs,
):
super().__init__(
rank=3,
Expand All @@ -130,5 +130,5 @@ def __init__(
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
**kwargs,
)
Loading