-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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][B004] hasattr replace callable #52096
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
@@ -301,7 +301,7 @@ def dyfunc(x): | |||
# NOTE: func may have been decorated. | |||
converted_call = None | |||
|
|||
elif hasattr(func, '__class__') and hasattr(func.__class__, '__call__'): | |||
elif hasattr(func, '__class__') and callable(func.__class__): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aurelius84 求帮看看这里的修改是否算是一种改进,或者是否会导致出现一些边界 case,我这边看了下觉得改不改区别不大
callable(x)
和 hasattr(x, "__call__")
对比可见 https://stackoverflow.com/questions/16388059/using-callablex-vs-hasattrx-call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以改为callable,似乎比hasattr语义更明确一些:
class A(object):
def __getattr__(self, name):
return name
a = A()
print(hasattr(a, "__call__")) # True
print(callable(a)) # False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR types
Others
PR changes
Others
Describe
将
hasattr(x, '__call__')
改写为callable(x)
前者是不可靠的,(相关文档 builtins - hasattr 、builtins - callable)hasattr(x, '__call__')
to test ifx
is callable is unreliable. Ifx
implements custom__getattr__
or its__call__
is itself not callable, you might get misleading results. Usecallable(x)
for consistent results.是否可以引入本 rule:✅ 如上所述,可以引入
是否可引入自动修复:❌手动修复
修复方法如下:
将
python/paddle/jit/dy2static/convert_call_func.py
文件下的修改为
Related links
introducing ruff
community#412