Repository navigation
[CodeStyle][B004] hasattr replace callable - #52096
Merged
Merged
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
SigureMo
reviewed
Mar 28, 2023
| converted_call = None | ||
|
|
||
| elif hasattr(func, '__class__') and hasattr(func.__class__, '__call__'): | ||
| elif hasattr(func, '__class__') and callable(func.__class__): |
Collaborator
There was a problem hiding this comment.
@Aurelius84 求帮看看这里的修改是否算是一种改进,或者是否会导致出现一些边界 case,我这边看了下觉得改不改区别不大
callable(x) 和 hasattr(x, "__call__") 对比可见 https://stackoverflow.com/questions/16388059/using-callablex-vs-hasattrx-call
Collaborator
There was a problem hiding this comment.
可以改为callable,似乎比hasattr语义更明确一些:
class A(object):
def __getattr__(self, name):
return name
a = A()
print(hasattr(a, "__call__")) # True
print(callable(a)) # False
Aurelius84
approved these changes
Mar 28, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR types
Others
PR changes
Others
Describe
将
hasattr(x, '__call__')改写为callable(x)前者是不可靠的,(相关文档 builtins - hasattr 、builtins - callable)hasattr(x, '__call__')to test ifxis callable is unreliable. Ifximplements 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 ruffcommunity#412