Skip to main content
https://github.com/gforcada/flake8-plone-hasattr/actions/workflows/testing.yml/badge.svg?branch=main https://coveralls.io/repos/gforcada/flake8-plone-hasattr/badge.svg?branch=main

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

Release files for flake8-plone-hasattr 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-plone-hasattr 1.2.0
File Size Uploaded
flake8_plone_hasattr-1.2.0.tar.gz 13.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-plone-hasattr 1.2.0
File Interpreter ABI Platform
flake8_plone_hasattr-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.3 kB

Release files / flake8_plone_hasattr-1.2.0.tar.gz

Download URL flake8_plone_hasattr-1.2.0.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7ab10489392a1d3cbea34424093e21b3a4ff5ecfd75a915cda0ba79140c032c4
BLAKE2b-256 checksum
How to use checksums
9c413d81980010207e30d396d050278375ff5eef98b73f948677de7281560329
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / flake8_plone_hasattr-1.2.0-py3-none-any.whl

Download URL flake8_plone_hasattr-1.2.0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
668a8109768e4caad59207ba591fc429968fbf218d40e665d839f7230188a5c8
BLAKE2b-256 checksum
How to use checksums
48e0c67089042cfe27175fbcb663da42af060b32c3408acd6d887d894e3ad212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page