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

[CodeStyle][task 10] enable ruff B017 rule in python/paddle/base #58185

Merged
merged 8 commits into from
Oct 23, 2023
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
11 changes: 0 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,3 @@ ignore = [
"test/dygraph_to_static/test_loop.py" = ["C416", "F821"]
# Ignore unnecessary lambda in dy2st unittest test_lambda
"test/dygraph_to_static/test_lambda.py" = ["PLC3002"]

# B017
"test/auto_parallel/spmd_rules/test_reshape_rule.py" = ["B017"]
"test/dygraph_to_static/test_assert.py" = ["B017"]
"test/legacy_test/test_cuda_max_memory_allocated.py" = ["B017"]
"test/legacy_test/test_cuda_max_memory_reserved.py" = ["B017"]
"test/legacy_test/test_cuda_memory_allocated.py" = ["B017"]
"test/legacy_test/test_cuda_memory_reserved.py" = ["B017"]
"test/legacy_test/test_eigvals_op.py" = ["B017"]
"test/legacy_test/test_tensordot.py" = ["B017"]
"test/legacy_test/test_top_k_v2_op.py" = ["B017"]
2 changes: 1 addition & 1 deletion test/auto_parallel/spmd_rules/test_reshape_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_reshape_infer_forward(self):
# shape: [6, 12, 48, 24] --> [3, 24, 6, -1, -1]
# raise error
self.attrs["shape"] = [3, 24, 6, -1, -1]
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
self.rule.infer_forward(
self.x_dist_tensor_spec, self.attrs['shape']
)
Expand Down
2 changes: 1 addition & 1 deletion test/dygraph_to_static/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TestAssertVariable(Dy2StTestBase):
def _run(self, func, x, with_exception, to_static):
paddle.jit.enable_to_static(to_static)
if with_exception:
with self.assertRaises(BaseException):
with self.assertRaises(BaseException): # noqa: B017
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(BaseException): # noqa: B017
with self.assertRaises(AssertionError):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里从语义上来看,就是应该捕获各种异常的,noqa 是可以的,除非重构代码,不是传入 True/False 而是传入异常类型

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

with base.dygraph.guard():
func(x)
else:
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_cuda_max_memory_allocated.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def test_max_memory_allocated_exception(self):
"gpu1",
]
for device in wrong_device:
with self.assertRaises(BaseException):
with self.assertRaises(BaseException): # noqa: B017
Copy link
Member

@gouzil gouzil Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(BaseException): # noqa: B017
with self.assertRaises((ValueError, AssertionError)):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这四个 memory 的为什么是 ValueError, AssertionError 呢?这是能全部捕获的么?

max_memory_allocated(device)
else:
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
max_memory_allocated()


Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_cuda_max_memory_reserved.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def test_max_memory_reserved_exception(self):
"gpu1",
]
for device in wrong_device:
with self.assertRaises(BaseException):
with self.assertRaises(BaseException): # noqa: B017
Copy link
Member

@gouzil gouzil Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(BaseException): # noqa: B017
with self.assertRaises((ValueError, AssertionError)):

max_memory_reserved(device)
else:
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
max_memory_reserved()


Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_cuda_memory_allocated.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def test_memory_allocated_exception(self):
"gpu1",
]
for device in wrong_device:
with self.assertRaises(BaseException):
with self.assertRaises(BaseException): # noqa: B017
Copy link
Member

@gouzil gouzil Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(BaseException): # noqa: B017
with self.assertRaises((ValueError, AssertionError)):

memory_allocated(device)
else:
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
memory_allocated()


Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_cuda_memory_reserved.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def test_memory_reserved_exception(self):
"gpu1",
]
for device in wrong_device:
with self.assertRaises(BaseException):
with self.assertRaises(BaseException): # noqa: B017
Copy link
Member

@gouzil gouzil Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(BaseException): # noqa: B017
with self.assertRaises((ValueError, AssertionError)):

memory_reserved(device)
else:
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
memory_reserved()


Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_eigvals_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,13 @@ def test_cases(self):
def test_error(self):
paddle.disable_static()
x = paddle.to_tensor([1])
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
paddle.linalg.eigvals(x)

self.input_dims = [1, 2, 3, 4]
self.set_input_data()
x = paddle.to_tensor(self.input_data)
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
paddle.linalg.eigvals(x)


Expand Down
18 changes: 15 additions & 3 deletions test/legacy_test/test_tensordot.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,21 @@ def test_error(self):
paddle.disable_static()
x = paddle.to_tensor(self.x)
y = paddle.to_tensor(self.y)
for axes in self.all_axes:
with self.assertRaises(BaseException):
paddle.tensordot(x, y, axes)

with self.assertRaises(TypeError):
paddle.tensordot(x, y, axes=self.all_axes[0])
with self.assertRaises(TypeError):
paddle.tensordot(x, y, axes=self.all_axes[1])
with self.assertRaises(AssertionError):
paddle.tensordot(x, y, axes=self.all_axes[2])
with self.assertRaises(IndexError):
paddle.tensordot(x, y, axes=self.all_axes[3])
with self.assertRaises(ValueError):
paddle.tensordot(x, y, axes=self.all_axes[4])
with self.assertRaises(AssertionError):
paddle.tensordot(x, y, axes=self.all_axes[5])
with self.assertRaises(AssertionError):
paddle.tensordot(x, y, axes=self.all_axes[6])


class TestTensordotAPIAxesTypeFloat64(TestTensordotAPIAxesType):
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_top_k_v2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,10 @@ def test_cases(self):
def test_errors(self):
with paddle.base.dygraph.guard():
x = paddle.to_tensor([1, 2, 3])
with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
paddle.topk(x, k=-1)

with self.assertRaises(BaseException):
with self.assertRaises(ValueError):
paddle.topk(x, k=0)


Expand Down