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][PLC3002][PLE1205] simplify lambda and add missing placeholder to logger template #52133

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ select = [
# "B030",
"B032",
# "B904",

# Pylint
"PLC3002",
"PLE1205"
]
unfixable = [
"NPY001"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def set_table_config(msg, config_name, configs, index=0):
getattr(msg, field.name), name, configs
)
else:
logger.debug("not message:", name)
Copy link
Member

Choose a reason for hiding this comment

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

该 rule 再多调研一下吧,如果调研结果合适可以引入

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

对日志输出优化

https://docs.python.org/3/howto/logging.html#optimization

这里并不是优化,而是避免出现错误的使用方式,目前代码里的使用方式是错误的

image

应引用文档 https://docs.python.org/3/howto/logging.html#logging-variable-data ,参数数量 > 占位符数量,会触发运行时错误

本 rule 可引入,可避免 bug
本 rule 无自动修复功能,需要开发者手动修复

logger.debug("not message: %s", name)
if name not in configs:
continue
if field.label == FieldDescriptor.LABEL_REPEATED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def call_lambda_as_func(x):
def call_lambda_directly(x):
x = fluid.dygraph.to_variable(x)

y = (lambda x, y: x + y)(x, x)
out = (lambda x: paddle.mean(x))(y)
Copy link
Member

Choose a reason for hiding this comment

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

根据文件名和动转静的特殊性,这里明显就是为了测试 lambda 语法是否能够正确转写的,因此不可以修改,该 rule 不适合引入

Copy link
Member Author

Choose a reason for hiding this comment

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

以下是一些关于 lambdas 的讨论:
https://stackoverflow.com/questions/890128/how-are-lambdas-useful/890188

https://stackoverflow.com/questions/20026382/python-callback-vs-node-callback-point-to-point-comparisons/20026692#20026692

我的看法是,像(lambda x: x**2)(y)这种完全可以简化, 使用 lambda 会让阅读变得复杂

所以个人建议是使用[tool.ruff.per-file-ignores]排除当前文件, 来应用该 rule

Copy link
Member

Choose a reason for hiding this comment

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

所以个人建议是使用[tool.ruff.per-file-ignores]排除当前文件, 来应用该 rule

可以

y = x + x
out = paddle.mean(y)

return out

Expand All @@ -48,8 +48,8 @@ def call_lambda_in_func(x):

add_func = lambda x: x + 1

y = paddle.mean((lambda x: F.relu(x))(x))
out = add_func(y) if y > 1 and y < 2 else (lambda x: x**2)(y)
y = paddle.mean((F.relu(x)))
out = add_func(y) if y > 1 and y < 2 else y**2

return out

Expand All @@ -60,7 +60,7 @@ def call_lambda_with_ifExpr(x):
add_func = lambda x: x + 1

y = paddle.mean(x)
out = add_func(y) if y or y < 2 else (lambda x: x**2)(y)
out = add_func(y) if y or y < 2 else y**2

return out

Expand All @@ -74,7 +74,7 @@ def call_lambda_with_ifExpr2(x):

# NOTE: y is Variable, but z<2 is python bool value
z = 0
out = add_func(y) if y or z < 2 else (lambda x: x**2)(y)
out = add_func(y) if y or z < 2 else y**2

return out

Expand Down