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][B004] hasattr replace callable #52096

Merged
merged 1 commit into from
Mar 28, 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ select = [
# Bugbear
"B002",
"B003",
# "B004",
"B004",
# "B005",
# "B006",
# "B007",
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/convert_call_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__):
Copy link
Member

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

Copy link
Contributor

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

from paddle.nn import Layer

if hasattr(func, 'forward') and isinstance(func, Layer):
Expand Down