# Python unused local variables

**URL:** <https://community.sonarsource.com/t/python-unused-local-variables/63446>\
**Category:** Report False-positive / False-negative...\
**Tags:** python\
**Created:** [May 18, 2022, 3:10am UTC](https://community.sonarsource.com/t/python-unused-local-variables/63446 "2022-05-18T03:10:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brma](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@brma](https://community.sonarsource.com/u/brma)\
**Post date:** [May 18, 2022, 3:10am UTC](https://community.sonarsource.com/t/python-unused-local-variables/63446/1 "2022-05-18T03:10:10Z")

</div>

Must-share information (formatted with [Markdown](https://commonmark.org/help/)):

- which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
- what are you trying to achieve
- what have you tried so far to achieve this

SonarQube 9.3 and SonarLint 7.4

the error "remove the unused local variable’ is raised when a dummy local variable is used.  
Sometimes this is unavoidable, as in

for \_i in Range(10):

the workaround is to just use “\_” but we already have a global \_() used for translation purposes (gettext) so we get UnbouldLocalVariable warnings.

Is it possible to specify a regex to define ignored unused locals? In pylint for example it may be anything beginning with \_, or dummy.

---

<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:** [June 9, 2022, 12:50pm UTC](https://community.sonarsource.com/t/python-unused-local-variables/63446/4 "2022-06-09T12:50:14Z")

</div>

Hello @brma,

Thank you for reporting this. It is true that the current implementation only silences issues for variables named “\_” and that this is a bit too narrow. Unfortunately, at this time it is not possible to specify a regex to define allowed names.

I created the following [ticket](https://jira.sonarsource.com/browse/SONARPY-1037) to improve this.

Though I know this is not a very satisfying workaround, in the meantime, you can silence the issue by using the `NOSONAR` pragma comment:

```auto
for _i in range(10): # NOSONAR
  print("Hello")

```

Cheers,  
Guillaume

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [June 16, 2022, 12:50pm UTC](https://community.sonarsource.com/t/python-unused-local-variables/63446/5 "2022-06-16T12:50:59Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
