-
-
Notifications
You must be signed in to change notification settings - Fork 388
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
Allow hasattr() function #2228
Comments
Your link to a video is https://wemake-python-stylegui.de/en/latest/pages/usage/violations/best_practices.html#wemake_python_styleguide.violations.best_practices.WrongFunctionCallViolation Can you please share a correct one? |
Oops, my wording was awful. I meant to say that the video referenced in the See Also documentation for WPS421 ... The link to the video is https://www.youtube.com/watch?v=YjHsOrOOSuI |
I don't mind submitting a PR, but before going through the motions, any thoughts around a yay or nay on this? |
I confirm that this no longer produce unexpected result: class A:
@property
def x(self):
print('wow')
hasattr(A, 'x') # True
But, when class GetSet:
def __get__(self, *args):
print('wow')
class A:
x = GetSet()
hasattr(A, 'x') # prints "wow" Why is that important? Because semantically So, I am still not completely sold on allowing it. |
These are good points. However, To be clear, I don't advocate blacklisting Semantically, I think that |
what @Dresdn says makes sense, one more reason in favor of allow |
I feel that the existing alternative to Using In light of this, couldn't |
What's wrong
WPS421 forbids calling some built-in functions, which includes
hasattr()
.How it should be
The function
hasattr()
should be allowed. There are certain scenarios where usinghasattr()
makes sense, and sometimes is even faster (as even outlined in the referenced video @ 13:34-14:15 mark)For code readability,
if hasattr(foo, 'bar'):
is cleaner thanif getattr(foo, 'bar', None):
.I believe there was an argument against using
hasattr()
in python2, but I don't believe the same argument is valid for python3.Flake8 version and plugins
n/a
pip information
n/a
OS information
n/a
The text was updated successfully, but these errors were encountered: