From 85e207558c190db5c7b1bf854764bf92c3e9e714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Sat, 25 Mar 2023 11:28:52 +0800 Subject: [PATCH] [CodeStyle][UP028] using yield from (#52059) --- pyproject.toml | 2 +- python/paddle/dataset/imdb.py | 3 +-- .../fleet/data_generator/data_generator.py | 3 +-- .../unittests/dist_text_classification.py | 3 +-- .../fluid/tests/unittests/feed_data_reader.py | 3 +-- .../unittests/test_imperative_decorator.py | 18 ++++++------------ .../test_parallel_executor_transformer.py | 3 +-- python/paddle/nn/layer/layers.py | 3 +-- python/paddle/reader/decorator.py | 9 +++------ python/paddle/utils/layers_utils.py | 3 +-- 10 files changed, 17 insertions(+), 33 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a0a0ee35ec8..40525a694ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,7 +70,7 @@ select = [ "UP025", "UP026", # "UP027", - # "UP028", + "UP028", "UP029", # "UP030", # "UP031", diff --git a/python/paddle/dataset/imdb.py b/python/paddle/dataset/imdb.py index e1aeb7b04df..a83dc2b2b31 100644 --- a/python/paddle/dataset/imdb.py +++ b/python/paddle/dataset/imdb.py @@ -93,8 +93,7 @@ def reader_creator(pos_pattern, neg_pattern, word_idx): load(neg_pattern, INS, 1) def reader(): - for doc, label in INS: - yield doc, label + yield from INS return reader diff --git a/python/paddle/distributed/fleet/data_generator/data_generator.py b/python/paddle/distributed/fleet/data_generator/data_generator.py index 64f8e377b94..a376151e9b6 100644 --- a/python/paddle/distributed/fleet/data_generator/data_generator.py +++ b/python/paddle/distributed/fleet/data_generator/data_generator.py @@ -228,8 +228,7 @@ class DataGenerator: ''' def local_iter(): - for sample in samples: - yield sample + yield from samples return local_iter diff --git a/python/paddle/fluid/tests/unittests/dist_text_classification.py b/python/paddle/fluid/tests/unittests/dist_text_classification.py index d29997ef8a0..d85f7f325ba 100644 --- a/python/paddle/fluid/tests/unittests/dist_text_classification.py +++ b/python/paddle/fluid/tests/unittests/dist_text_classification.py @@ -192,8 +192,7 @@ def reader_creator(pos_pattern, neg_pattern, word_idx): load(neg_pattern, INS, 1) def reader(): - for doc, label in INS: - yield doc, label + yield from INS return reader diff --git a/python/paddle/fluid/tests/unittests/feed_data_reader.py b/python/paddle/fluid/tests/unittests/feed_data_reader.py index 91530ca89a3..88039eb68c4 100644 --- a/python/paddle/fluid/tests/unittests/feed_data_reader.py +++ b/python/paddle/fluid/tests/unittests/feed_data_reader.py @@ -19,8 +19,7 @@ from paddle.fluid.framework import Variable def cyclic_reader(reader): def __reader__(): while True: - for data in reader(): - yield data + yield from reader() return __reader__ diff --git a/python/paddle/fluid/tests/unittests/test_imperative_decorator.py b/python/paddle/fluid/tests/unittests/test_imperative_decorator.py index 07c11514787..c3b93cc3143 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_decorator.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_decorator.py @@ -105,8 +105,7 @@ class TestNoGradClass(unittest.TestCase): ) def test_gen(): - for i in range(3): - yield i + yield from range(3) a = 0 for i in test_gen(): @@ -114,8 +113,7 @@ class TestNoGradClass(unittest.TestCase): @paddle.no_grad() def test_wrapped_gen(): - for i in range(3): - yield i + yield from range(3) b = 0 for i in test_wrapped_gen(): @@ -151,8 +149,7 @@ class TestEnableGradClass(unittest.TestCase): ) def test_gen(): - for i in range(3): - yield i + yield from range(3) a = 0 for i in test_gen(): @@ -160,8 +157,7 @@ class TestEnableGradClass(unittest.TestCase): @paddle.enable_grad() def test_wrapped_gen(): - for i in range(3): - yield i + yield from range(3) b = 0 for i in test_wrapped_gen(): @@ -215,8 +211,7 @@ class TestSetGradEnabledClass(unittest.TestCase): ) def test_gen(): - for i in range(3): - yield i + yield from range(3) a = 0 for i in test_gen(): @@ -224,8 +219,7 @@ class TestSetGradEnabledClass(unittest.TestCase): @paddle.set_grad_enabled(True) def test_wrapped_gen(): - for i in range(3): - yield i + yield from range(3) b = 0 for i in test_wrapped_gen(): diff --git a/python/paddle/fluid/tests/unittests/test_parallel_executor_transformer.py b/python/paddle/fluid/tests/unittests/test_parallel_executor_transformer.py index e7c40e8a7eb..6cdd015b114 100644 --- a/python/paddle/fluid/tests/unittests/test_parallel_executor_transformer.py +++ b/python/paddle/fluid/tests/unittests/test_parallel_executor_transformer.py @@ -212,8 +212,7 @@ def get_feed_data_reader(): all_batch_tensors.append(tensors) def __reader__(): - for t in all_batch_tensors: - yield t + yield from all_batch_tensors feed_data_reader = FeedDataReader( feed_list=transformer_model.build_inputs( diff --git a/python/paddle/nn/layer/layers.py b/python/paddle/nn/layer/layers.py index 54ed18c1cb8..9eae9432c58 100644 --- a/python/paddle/nn/layer/layers.py +++ b/python/paddle/nn/layer/layers.py @@ -206,8 +206,7 @@ class LayerObjectHelper(LayerHelperBase): inputs = inputs_in if (inputs_in is not None) else [] inputs = self._multiple_input(inputs) param_attrs = self._multiple_param_attr(len(inputs), param_attr_in) - for ipt, param_attr in zip(inputs, param_attrs): - yield ipt, param_attr + yield from zip(inputs, param_attrs) def input_dtype(self, inputs_in): """Get input data type diff --git a/python/paddle/reader/decorator.py b/python/paddle/reader/decorator.py index 5902c88130e..e5c47ebdb34 100644 --- a/python/paddle/reader/decorator.py +++ b/python/paddle/reader/decorator.py @@ -76,8 +76,7 @@ def cache(reader): all_data = tuple(reader()) def __impl__(): - for item in all_data: - yield item + yield from all_data return __impl__ @@ -118,8 +117,7 @@ def map_readers(func, *readers): rs = [] for r in readers: rs.append(r()) - for e in map(func, *rs): - yield e + yield from map(func, *rs) return reader @@ -228,8 +226,7 @@ def chain(*readers): for r in readers: rs.append(r()) - for e in itertools.chain(*rs): - yield e + yield from itertools.chain(*rs) return reader diff --git a/python/paddle/utils/layers_utils.py b/python/paddle/utils/layers_utils.py index 98b9089d83f..b1fb8460237 100644 --- a/python/paddle/utils/layers_utils.py +++ b/python/paddle/utils/layers_utils.py @@ -152,8 +152,7 @@ def _yield_value(iterable): for key in _sorted(iterable): yield iterable[key] else: - for value in iterable: - yield value + yield from iterable def _yield_flat_nest(nest): -- GitLab