question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow hasattr() function

See original GitHub issue

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 using hasattr() 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 than if 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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Dresdncommented, Jan 31, 2022

These are good points. However, getattr outputs wow just like hasattr does. By that logic, getattr should be added to the FUNCTIONS_BLACKLIST as well then, yes?

To be clear, I don’t advocate blacklisting getattr as there are semantic and performance reasons for using it.

Semantically, I think that if hasattr(A, 'foo') is clearer than if getattr(A, 'foo', False), which was the reasoning behind opening this issue. Do you agree?

0reactions
rubancarcommented, Jul 20, 2022

what @Dresdn says makes sense, one more reason in favor of allow hasattr is that the logic behind this one is performed by calling getattr and catching AttributteError, so, at the end both funcions are base on the same logic, It does not make sense to allow getattr and and issue a warning on hasattr

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is hasattr() a method? [closed] - Stack Overflow
Python functions, added to a class, become methods automatically when you access them as an attribute on an instance of the class:
Read more >
Python hasattr() method - GeeksforGeeks
Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return...
Read more >
How to use Python hasattr() method? - AskPython
The hasattr() method returns a boolean value i.e. either True or False depending on the presence of the attribute in the class.
Read more >
Python hasattr() Builtin Function - Examples - Tutorial Kart
Python hasattr() builtin function is used to check if a Python object has a specific named attribute. In this tutorial, we will learn...
Read more >
Why do we use the Python hasattr() function? - Toppr
The Python hasattr() built-in function is used to check if the specified object has the named attribute present within a class or not....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found