# Cognitive complexity wrong behavior on python code?

**URL:** <https://community.sonarsource.com/t/cognitive-complexity-wrong-behavior-on-python-code/94643>\
**Category:** VS Code\
**Created:** [June 26, 2023, 9:32am UTC](https://community.sonarsource.com/t/cognitive-complexity-wrong-behavior-on-python-code/94643 "2023-06-26T09:32:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkolfy](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/lkolfy/32/46571_2.png) [@lkolfy](https://community.sonarsource.com/u/lkolfy)\
**Post date:** [June 26, 2023, 9:32am UTC](https://community.sonarsource.com/t/cognitive-complexity-wrong-behavior-on-python-code/94643/1 "2023-06-26T09:32:52Z")

</div>

Hi all, I have this question that I searched no one raised, is it correct that the Cognitive Complexity doesn’t take in consideration nested function definition?  
I’m using:  
\* Windows 10  
\* Sonarlint v3.19.0  
\* Python  
\* Not Connected to SonarCloud or SonarQube

 ![immagine](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/d/9/d9558430571453e32fe1ff457a95c0cabe081979.png)

As you can see in the attached image, Sonarlint start count the location for Cognitive complexity point even if they are in nested function definition so the body of the main function starts with a high value and not 1.  
thanks for any reply  
Luca

---

<div class="post-metadata">

**Author:** ![Guillaume\_Dequenne](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/guillaume_dequenne/32/7472_2.png) [@Guillaume\_Dequenne](https://community.sonarsource.com/u/Guillaume_Dequenne)\
**Post date:** [July 5, 2023, 7:49am UTC](https://community.sonarsource.com/t/cognitive-complexity-wrong-behavior-on-python-code/94643/4 "2023-07-05T07:49:36Z")

</div>

Hello @lkolfy,

First, sorry for the late answer.  
As you can see in the white paper linked on this [page](https://www.sonarsource.com/resources/cognitive-complexity/#:~:text=Cognitive%20Complexity%20breaks%20from%20using,with%20how%20developers%20perceive%20maintainability.), cognitive complexity **does** take into account nested function definition, except for decorators.

To qualify for the decorator exception, a function may contain only a nested function and a return  
statement. Note that the exception only removes the nesting penalty, though.

In your case, there are multiple nested functions, so the exception does not apply.

Hope that helps,  
Guillaume
