wemake-services / wemake-services/wemake-python-styleguide

New metric: dynamic complexity

Open
#393 11 comments 0 reactions 1 assignee View on GitHub

@sobolevn is already working on this.

Since Oct 7, 2019.

level:advanced rule request
Dominant language
Python
Stars
2.9k
Forks
430
Avg merge
5h 14m
Merged PRs (30d)
34

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.