Skip to main content

Checks for hasattr, which is considered harmful in Plone projects

Project description

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

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

flake8_plone_hasattr-1.2.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_plone_hasattr-1.2.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file flake8_plone_hasattr-1.2.0.tar.gz.

File metadata

  • Download URL: flake8_plone_hasattr-1.2.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for flake8_plone_hasattr-1.2.0.tar.gz
Algorithm Hash digest
SHA256 7ab10489392a1d3cbea34424093e21b3a4ff5ecfd75a915cda0ba79140c032c4
MD5 793d6095d948355b413496fa78183353
BLAKE2b-256 9c413d81980010207e30d396d050278375ff5eef98b73f948677de7281560329

See more details on using hashes here.

File details

Details for the file flake8_plone_hasattr-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_plone_hasattr-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 668a8109768e4caad59207ba591fc429968fbf218d40e665d839f7230188a5c8
MD5 b3a948539a3ce9296614a4b33b395ed6
BLAKE2b-256 48e0c67089042cfe27175fbcb663da42af060b32c3408acd6d887d894e3ad212

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page