PyCQA / PyCQA/mccabe

Complexity in list comprehensions

Open
#69 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
681
Forks
65
PR merge metrics
No merged PRs in 30d

Description

Having asdf.py:

elements = [0, 1, 2, 3, 4]
ignore = [1, 2]


def loop():
    for x in elements:
        if x in ignore:
            continue
        print(x)


def comprehension():
    filtered = [x for x in elements if x not in ignore]
    for x in filtered:
        print(x)


loop()
print('----')
comprehension()

The McCabe complexity in loop() seems to be higher than in comprehension():

$ python -m mccabe asdf.py 
5:0: 'loop' 3
12:0: 'comprehension' 2

Is that really expected? Should not fors and ifs in list comprehensions count towards total complexity?

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Reproduce the reported output with the asdf.py example by running python -m mccabe asdf.py. Then inspect how McCabe calculates complexity for loops, conditionals, and list comprehensions, and determine the expected counting behavior before adding focused coverage for the chosen result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.