From 05973b2d6c2f69985afa701239a54179803a3942 Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Fri, 15 Sep 2023 19:58:50 +0800 Subject: [PATCH] enable UP028 --- pyproject.toml | 15 +++++---------- python/paddle/base/dygraph/base.py | 3 +-- python/paddle/base/framework.py | 3 +-- .../base/incubate/checkpoint/auto_checkpoint.py | 3 +-- python/paddle/base/layer_helper.py | 3 +-- python/paddle/dataset/cifar.py | 3 +-- python/paddle/dataset/common.py | 3 +-- python/paddle/io/dataloader/dataset.py | 3 +-- python/paddle/nn/layer/layers.py | 5 ++--- python/paddle/utils/layers_utils.py | 3 +-- test/legacy_test/test_lstm_cudnn_op.py | 3 +-- test/rnn/rnn_numpy.py | 3 +-- 12 files changed, 17 insertions(+), 33 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index edc174738481b..96190ae26fae5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -113,7 +113,6 @@ ignore = [ "F522", "B010", "PLR1722", - "UP028", "C403", "F811", "UP034", @@ -165,15 +164,6 @@ ignore = [ "test/legacy_test/test_tensordot.py" = ["B017"] "test/legacy_test/test_top_k_v2_op.py" = ["B017"] -# UP028 -"python/paddle/dataset/cifar.py" = ["UP028"] -"python/paddle/dataset/common.py" = ["UP028", "UP032"] -"python/paddle/io/dataloader/dataset.py" = ["UP028"] -"python/paddle/nn/layer/layers.py" = ["UP028", "UP032"] -"python/paddle/utils/layers_utils.py" = ["UP028", "UP032"] -"test/legacy_test/test_lstm_cudnn_op.py" = ["UP028", "UP032"] -"test/rnn/rnn_numpy.py" = ["UP028", "UP032"] - # UP032 "paddle/fluid/ir/dialect/op_generator/api_gen.py" = ["UP032"] "paddle/fluid/ir/dialect/op_generator/op_gen.py" = ["UP032"] @@ -190,6 +180,7 @@ ignore = [ "python/paddle/audio/backends/init_backend.py" = ["UP032"] "python/paddle/audio/backends/wave_backend.py" = ["UP032"] "python/paddle/batch.py" = ["UP032"] +"python/paddle/dataset/common.py" = ["UP032"] "python/paddle/device/cuda/__init__.py" = ["UP032"] "python/paddle/distributed/auto_parallel/interface.py" = ["UP032"] "python/paddle/distributed/auto_parallel/static/completion.py" = ["UP032"] @@ -312,6 +303,7 @@ ignore = [ "python/paddle/nn/layer/activation.py" = ["UP032"] "python/paddle/nn/layer/common.py" = ["UP032"] "python/paddle/nn/layer/container.py" = ["UP032"] +"python/paddle/nn/layer/layers.py" = ["UP032"] "python/paddle/nn/layer/loss.py" = ["UP032"] "python/paddle/nn/layer/norm.py" = ["UP032"] "python/paddle/nn/layer/pooling.py" = ["UP032"] @@ -346,6 +338,7 @@ ignore = [ "python/paddle/utils/dlpack.py" = ["UP032"] "python/paddle/utils/download.py" = ["UP032"] "python/paddle/utils/install_check.py" = ["UP032"] +"python/paddle/utils/layers_utils.py" = ["UP032"] "python/paddle/vision/datasets/cifar.py" = ["UP032"] "python/paddle/vision/datasets/flowers.py" = ["UP032"] "python/paddle/vision/datasets/mnist.py" = ["UP032"] @@ -437,6 +430,7 @@ ignore = [ "test/legacy_test/test_imperative_se_resnext.py" = ["UP032"] "test/legacy_test/test_inplace.py" = ["UP032"] "test/legacy_test/test_layers.py" = ["UP032"] +"test/legacy_test/test_lstm_cudnn_op.py" = ["UP032"] "test/legacy_test/test_multi_dot_op.py" = ["UP032"] "test/legacy_test/test_multiprocess_dataloader_iterable_dataset_static.py" = ["UP032"] "test/legacy_test/test_multiprocess_dataloader_static.py" = ["UP032"] @@ -464,6 +458,7 @@ ignore = [ "test/quantization/test_post_training_quantization_while.py" = ["UP032"] "test/quantization/test_quant_post_quant_aware.py" = ["UP032"] "test/quantization/test_weight_quantization_mobilenetv1.py" = ["UP032"] +"test/rnn/rnn_numpy.py" = ["UP032"] "test/tokenizer/bert_tokenizer.py" = ["UP032"] "test/tokenizer/tokenizer_utils.py" = ["UP032"] "test/xpu/test_generate_proposals_v2_op_xpu.py" = ["UP032"] diff --git a/python/paddle/base/dygraph/base.py b/python/paddle/base/dygraph/base.py index 6490a6152cef1..85ae69419eeed 100644 --- a/python/paddle/base/dygraph/base.py +++ b/python/paddle/base/dygraph/base.py @@ -352,8 +352,7 @@ def _decorate_function(func, *args, **kwargs): def _decorate_generator(func, *args, **kwargs): gen = func(*args, **kwargs) with self: - for x in gen: - yield x + yield from gen if inspect.isgeneratorfunction(func): return _decorate_generator(func) diff --git a/python/paddle/base/framework.py b/python/paddle/base/framework.py index 7709599c25468..b54f32d5bdc73 100644 --- a/python/paddle/base/framework.py +++ b/python/paddle/base/framework.py @@ -6956,8 +6956,7 @@ def list_vars(self): >>> # var label : LOD_TENSOR.shape(-1, 1).dtype(int64).stop_gradient(True) """ for each_block in self.blocks: - for each_var in list(each_block.vars.values()): - yield each_var + yield from list(each_block.vars.values()) def all_parameters(self): """ diff --git a/python/paddle/base/incubate/checkpoint/auto_checkpoint.py b/python/paddle/base/incubate/checkpoint/auto_checkpoint.py index 7d07c4594acb4..ca5cfa0d56cb2 100644 --- a/python/paddle/base/incubate/checkpoint/auto_checkpoint.py +++ b/python/paddle/base/incubate/checkpoint/auto_checkpoint.py @@ -632,8 +632,7 @@ def _get_checker(): def _normal_yield(max_epoch_num): if max_epoch_num < 0: max_epoch_num = sys.maxint - for i in range(0, max_epoch_num): - yield i + yield from range(0, max_epoch_num) return diff --git a/python/paddle/base/layer_helper.py b/python/paddle/base/layer_helper.py index 96722b231acb5..38a5a51aaa110 100644 --- a/python/paddle/base/layer_helper.py +++ b/python/paddle/base/layer_helper.py @@ -86,8 +86,7 @@ def multiple_param_attr(self, length): def iter_inputs_and_params(self, input_param_name='input'): inputs = self.multiple_input(input_param_name) param_attrs = self.multiple_param_attr(len(inputs)) - for ipt, param_attr in zip(inputs, param_attrs): - yield ipt, param_attr + yield from zip(inputs, param_attrs) def input_dtype(self, input_param_name='input'): inputs = self.multiple_input(input_param_name) diff --git a/python/paddle/dataset/cifar.py b/python/paddle/dataset/cifar.py index ede0144a07223..f04acdfcc4b2b 100644 --- a/python/paddle/dataset/cifar.py +++ b/python/paddle/dataset/cifar.py @@ -63,8 +63,7 @@ def reader(): for name in names: batch = pickle.load(f.extractfile(name), encoding='bytes') - for item in read_batch(batch): - yield item + yield from read_batch(batch) if not cycle: break diff --git a/python/paddle/dataset/common.py b/python/paddle/dataset/common.py index c7a6d1b0b19ab..6b51b27c66712 100644 --- a/python/paddle/dataset/common.py +++ b/python/paddle/dataset/common.py @@ -211,8 +211,7 @@ def reader(): for fn in my_file_list: with open(fn, "r") as f: lines = loader(f) - for line in lines: - yield line + yield from lines return reader diff --git a/python/paddle/io/dataloader/dataset.py b/python/paddle/io/dataloader/dataset.py index 4daf410a31836..f8153eb917070 100755 --- a/python/paddle/io/dataloader/dataset.py +++ b/python/paddle/io/dataloader/dataset.py @@ -434,8 +434,7 @@ def __init__(self, datasets): def __iter__(self): for dataset in self.datasets: - for sample in dataset: - yield sample + yield from dataset class Subset(Dataset): diff --git a/python/paddle/nn/layer/layers.py b/python/paddle/nn/layer/layers.py index 2d88f2e1e81bd..c2b507e1a15c0 100644 --- a/python/paddle/nn/layer/layers.py +++ b/python/paddle/nn/layer/layers.py @@ -1110,10 +1110,9 @@ def named_sublayers(self, prefix='', include_self=False, layers_set=None): if layer is None: continue layer_prefix = prefix + ('.' if prefix else '') + key - for p, l in layer.named_sublayers( + yield from layer.named_sublayers( prefix=layer_prefix, include_self=True, layers_set=layers_set - ): - yield p, l + ) def register_buffer(self, name, tensor, persistable=True): """ diff --git a/python/paddle/utils/layers_utils.py b/python/paddle/utils/layers_utils.py index 1a9c56f218d73..fd44f4c5b47c7 100644 --- a/python/paddle/utils/layers_utils.py +++ b/python/paddle/utils/layers_utils.py @@ -158,8 +158,7 @@ def _yield_value(iterable): def _yield_flat_nest(nest): for n in _yield_value(nest): if is_sequence(n): - for ni in _yield_flat_nest(n): - yield ni + yield from _yield_flat_nest(n) else: yield n diff --git a/test/legacy_test/test_lstm_cudnn_op.py b/test/legacy_test/test_lstm_cudnn_op.py index 2ac1457a59651..e36d4b01a866f 100644 --- a/test/legacy_test/test_lstm_cudnn_op.py +++ b/test/legacy_test/test_lstm_cudnn_op.py @@ -218,8 +218,7 @@ def flatten(nested): def _flatten(nested): for item in nested: if isinstance(item, (list, tuple)): - for subitem in _flatten(item): - yield subitem + yield from _flatten(item) else: yield item diff --git a/test/rnn/rnn_numpy.py b/test/rnn/rnn_numpy.py index 2c8cb26e4b4c2..dc115d0734ba5 100644 --- a/test/rnn/rnn_numpy.py +++ b/test/rnn/rnn_numpy.py @@ -298,8 +298,7 @@ def flatten(nested): def _flatten(nested): for item in nested: if isinstance(item, (list, tuple)): - for subitem in _flatten(item): - yield subitem + yield from _flatten(item) else: yield item