# python:S5953 walrus operator inside list with reused variable

**URL:** <https://community.sonarsource.com/t/python-s5953-walrus-operator-inside-list-with-reused-variable/74648>\
**Category:** Report False-positive / False-negative...\
**Tags:** python\
**Created:** [October 17, 2022, 5:44pm UTC](https://community.sonarsource.com/t/python-s5953-walrus-operator-inside-list-with-reused-variable/74648 "2022-10-17T17:44:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hubert\_Wlodarczyk](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@Hubert\_Wlodarczyk](https://community.sonarsource.com/u/Hubert_Wlodarczyk)\
**Post date:** [October 17, 2022, 5:44pm UTC](https://community.sonarsource.com/t/python-s5953-walrus-operator-inside-list-with-reused-variable/74648/1 "2022-10-17T17:44:04Z")

</div>

Hello.

Problematic rule is [Variables, classes and functions should be either defined or imported](https://rules.sonarsource.com/python/RSPEC-5953). In code view it says _“items is not defined. Change its name or define it before using it”_.

SonarQube  
Version: 8.9.9.56886  
Date: 2022-10-17

Code looks like this:

```auto
@pytest.mark.parametrize(
    "input_items, expected_items",
    [
        [
            items := [
                ClassName(
                    **{
                        ...
                    },
                ),
            ],
            items, # Bug here.
    ],
    ....
)
def test_func(input_items, expected_items):
    ...

```

I don’t see any other solution for this other than marking this bug as “false positive”.

Surprisingly it only marks this occurrence. Later in code we have similar “reuse” of variable with the difference that class arguments aren’t passed through `dict` unpacking. I’m not sure if that’s the cause.

---

<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:** [October 25, 2022, 3:30pm UTC](https://community.sonarsource.com/t/python-s5953-walrus-operator-inside-list-with-reused-variable/74648/4 "2022-10-25T15:30:57Z")

</div>

Hello @Hubert_Wlodarczyk,

Thank you for reporting this false positive!

The good news is that, while I managed to reproduce it on the current SonarQube LTS version (8.9.10), I could not reproduce it on the latest SonarQube version (9.7), so the false positive has already been fixed.

If you can live with flagging those issues as false positives until the release of the next LTS (which should happen early next year), then that may be the simplest course of action for you. Otherwise, you can also use the `# NOSONAR` comment on those patterns, or simply temporarily disable the rule.

Also, please note that this rule (like many others), is not designed to be run on test code. By specifying what your main code is and what your test code is (through the `sonar.sources` and `sonar.tests` properties - see [here](https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/)), you may get more precise analysis results.

Hope that helps,  
Guillaume

---

<div class="post-metadata">

**Author:** ![Hubert\_Wlodarczyk](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@Hubert\_Wlodarczyk](https://community.sonarsource.com/u/Hubert_Wlodarczyk)\
**Post date:** [October 25, 2022, 3:49pm UTC](https://community.sonarsource.com/t/python-s5953-walrus-operator-inside-list-with-reused-variable/74648/5 "2022-10-25T15:49:14Z")

</div>

Hello Guillaume.

Thank you for checking that out. Good to hear that it has been fixed in newer versions.

Also thank you for pointing out `sonar.tests` property. I will look into it and verify how it fits in out project in such cases.

Cheers!

---

<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:** [November 7, 2022, 10:35am UTC](https://community.sonarsource.com/t/python-s5953-walrus-operator-inside-list-with-reused-variable/74648/7 "2022-11-07T10:35:13Z")

</div>

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