Skip to content

New metric: dynamic complexity #393

Description

@sobolevn

Rule request

Thesis

Original idea belongs to: Tin Markovic.

We need to detect functions that use too much dynamic features of python or so called "magic".
So, it is pretty much the same as McCabe complexity but for dynamic structures.
We just count uses of these structures and if it tops the threshold then we raise a violation.

What do we count as "magic"? Here's the list to be extended:

  1. *args and **kwargs in function parameters
  2. getattr, setattr, hasattr functions
  3. all direct magic methods: .__setattr__, .__div__, etc
  4. all magic constants: __name__, __file__, etc
  5. @property
  6. * and ** for argument expansion
  7. decorators, despite being tracked individually
  8. catching AttributeError, KeyError, IndexError

We can also try to implement the same metric for classes. This way we can control:

  1. metaclasses
  2. __slots__ and other magic fields
  3. magic methods
  4. decorators, despite being tracked individually

Reasoning

We need to track thing that might fail. We need to be extra careful with magic, since it is hard to use.

Status

This is an early draft. Please, feel free to suggest any ideas about this topic.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions