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][UP004] remove useless object inheritance #51771

Merged
merged 4 commits into from
Mar 20, 2023

Conversation

Ainavo
Copy link
Contributor

@Ainavo Ainavo commented Mar 16, 2023

PR types

Others

PR changes

Others

Describe

UP004 去除无用对象继承,将 class Name(object) 形式的代码替换成 class Name:,这两者在 python3 中等价,后者写法更加简洁。

class object
Return a new featureless object. 

参考文档:Python 2.x 中默认都是经典类,只有显式继承了 object 才是新式类函数;Python 3.x 中默认都是新式类,经典类被移除,没必要显式继承 object。

  • 是否可以引入本 relu:如上所述,可以引入
  • 是否可以引入自动修复: 转写后不会影响代码的语义,该规则可视为可靠的自动转写规则,可以引入
    修复所使用的命令如下:
# 安装 ruff 0.0.254
pip install ruff==0.0.254
# 确定本 rule 自动修复方案合适,因此直接使用命令自动修复
ruff --select UP004 . --fix

#51729

@SigureMo
Copy link
Member

SigureMo commented Mar 16, 2023

首先要在描述添加 tracking issue 和文档相关链接

object is a base for all classes. It has methods that are common to all instances of Python classes. This function does not accept any arguments.

这段并不能表明为什么可以直接替换,可参考 https://stackoverflow.com/a/54873/17656881 ,以及我们做过的 https://github.com/PaddlePaddle/community/blob/master/pfcc/call-for-contributions/code_style/legacy_python2.md#python-2-%E6%A8%A1%E5%9D%97 也阐明了详细的原因

另外没有看到配置的修改,记得在其他 PR 里修改(比如多个错误码配置修改可以放在一个 PR 里)

以及可以进一步清理一下文档里的此种情况(如 paddle/fluid/imperative/README.md),利用正则 class .+\(object\): 全局搜索即可,看起来不多,直接这个 PR 一起清理即可,或者和之后改配置的 PR 一起吧

@paddle-bot paddle-bot bot added the contributor External developers label Mar 16, 2023
@Ainavo
Copy link
Contributor Author

Ainavo commented Mar 18, 2023

首先要在描述添加 tracking issue 和文档相关链接

object is a base for all classes. It has methods that are common to all instances of Python classes. This function does not accept any arguments.

这段并不能表明为什么可以直接替换,可参考 https://stackoverflow.com/a/54873/17656881 ,以及我们做过的 https://github.com/PaddlePaddle/community/blob/master/pfcc/call-for-contributions/code_style/legacy_python2.md#python-2-%E6%A8%A1%E5%9D%97 也阐明了详细的原因

另外没有看到配置的修改,记得在其他 PR 里修改(比如多个错误码配置修改可以放在一个 PR 里)

以及可以进一步清理一下文档里的此种情况(如 paddle/fluid/imperative/README.md),利用正则 class .+\(object\): 全局搜索即可,看起来不多,直接这个 PR 一起清理即可,或者和之后改配置的 PR 一起吧

好的

@Ainavo Ainavo changed the title [CodeStyle][UP004] useless-object-inheritance [CodeStyle][UP004] useless object inheritance Mar 18, 2023
@Ainavo Ainavo changed the title [CodeStyle][UP004] useless object inheritance [CodeStyle][UP004] remove useless object inheritance Mar 18, 2023
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTM~

@luotao1 luotao1 merged commit 9983892 into PaddlePaddle:develop Mar 20, 2023
@Ainavo Ainavo deleted the add_UP004_for_ruff branch March 20, 2023 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants