# SonarCloud detects wrong list or dictionary access leading to IndexError/KeyError

**URL:** <https://community.sonarsource.com/t/sonarcloud-detects-wrong-list-or-dictionary-access-leading-to-indexerror-keyerror/75938>\
**Category:** Sonar Updates\
**Tags:** python, bug\
**Created:** [November 14, 2022, 8:52am UTC](https://community.sonarsource.com/t/sonarcloud-detects-wrong-list-or-dictionary-access-leading-to-indexerror-keyerror/75938 "2022-11-14T08:52:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandre\_Gigleux](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alexandre_gigleux/32/26_2.png) [@Alexandre\_Gigleux](https://community.sonarsource.com/u/Alexandre_Gigleux)\
**Post date:** [November 14, 2022, 8:52am UTC](https://community.sonarsource.com/t/sonarcloud-detects-wrong-list-or-dictionary-access-leading-to-indexerror-keyerror/75938/1 "2022-11-14T08:52:00Z")

</div>

Hello Python developers,

Python analysis has been upgraded to detect bugs that make your application crash with KeyError or IndexError exceptions.

The 3 added rules are:

- [S6464](https://rules.sonarsource.com/python/RSPEC-6464): Non existent dictionary keys should not be accessed
- [S6465](https://rules.sonarsource.com/python/RSPEC-6465): Unpacking should have the same number of elements of the iterable
- [S6466](https://rules.sonarsource.com/python/RSPEC-6464): Accessing list elements should not trigger an IndexError

Here is an example of a mistake that can be detected with such rules:

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/5/4/54d19708eee4ce8c67c943dc86cab0a811f9f0d7.png)

The key `server_platform` doesn’t exist in the `certificate` dictionary and we’ll lead to a KeyError if the condition on line 473 is true.

These rules are available now on [SonarCloud.io](https://sonarcloud.io/) and will be part of SonarQube 9.8.

Alex

---

<div class="post-metadata">

**Author:** ![Alexandre\_Gigleux](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alexandre_gigleux/32/26_2.png) [@Alexandre\_Gigleux](https://community.sonarsource.com/u/Alexandre_Gigleux)\
**Post date:** [November 11, 2022, 3:59pm UTC](https://community.sonarsource.com/t/sonarcloud-detects-wrong-list-or-dictionary-access-leading-to-indexerror-keyerror/75938/2 "2022-11-11T15:59:05Z")

</div>

Unlisted on November 11, 2022, 3:59pm UTC



---

<div class="post-metadata">

**Author:** ![Alexandre\_Gigleux](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alexandre_gigleux/32/26_2.png) [@Alexandre\_Gigleux](https://community.sonarsource.com/u/Alexandre_Gigleux)\
**Post date:** [November 14, 2022, 8:51am UTC](https://community.sonarsource.com/t/sonarcloud-detects-wrong-list-or-dictionary-access-leading-to-indexerror-keyerror/75938/3 "2022-11-14T08:51:10Z")

</div>

Listed on November 14, 2022, 8:51am UTC



---

<div class="post-metadata">

**Author:** ![LeivaMayonga](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/leivamayonga/32/39326_2.png) [@LeivaMayonga](https://community.sonarsource.com/u/LeivaMayonga)\
**Post date:** [November 21, 2022, 12:09am UTC](https://community.sonarsource.com/t/sonarcloud-detects-wrong-list-or-dictionary-access-leading-to-indexerror-keyerror/75938/4 "2022-11-21T00:09:43Z")

</div>

Wow, thanks for these detailed elements!
