Checks for hasattr, which is considered harmful in Plone projects
Project description
Flake8 Plone hasattr plugin
Python standard hasattr is considered harmful (within Plone projects).
The (hidden) problem with hasattr is that it swallows exceptions, which in your normal business logic you really don’t want to.
Specially in Plone context that could mean swallowing a database error, or a permission exception, etc.
Take, for instance, the following code:
>>> class Foo(object):
... @property
... def my_attr(self):
... raise ValueError('nope, nope, nope')
...
>>> bar = Foo()
>>> bar.my_attr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in my_attr
ValueError: nope, nope, nope
>>> hasattr(Foo, 'my_attr')
True
>>> hasattr(bar, 'my_attr')
False
One should rather do:
>>> getattr(bar, 'my_attr', None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in my_attr
ValueError: nope, nope, nope
Or in case you want to handle an exception:
>>> try:
... value = getattr(bar, 'my_attr', None)
... exception ValueError:
... value = None
This plugin is based on a python checker that was in plone.recipe.codeanalysis.
Install
Install with pip:
$ pip install flake8-plone-hasattr
Requirements
Python 3.8, 3.9, 3.10, 3.11 and pypy3
flake8
License
GPL 2.0
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8_plone_hasattr-1.1.1.tar.gz
.
File metadata
- Download URL: flake8_plone_hasattr-1.1.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e2f277739a903fdb7c9cd9d43c778f83bc2768ded3fb5228345f98408ece336 |
|
MD5 | 650d1ba0eedf99d4aca99f7f47d52faf |
|
BLAKE2b-256 | c39c8ddcd46c3b5886719c44d2c0a9a6ed09ed2f217eb1753f4ee29ad92f76b2 |
File details
Details for the file flake8_plone_hasattr-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: flake8_plone_hasattr-1.1.1-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cba36aa7547fa309a61da65a4b4276401ca791a69c717320c690688711676bb |
|
MD5 | d6756fb0fe8d8f0de9d9423dd7319ad3 |
|
BLAKE2b-256 | e40cdbc94807ad4aeae99772c351ecb81137d6fccd852b99c112f63ba7258b85 |