# S1940 should not propose code that leads to infinite recursion (e.g., avoid in \`\_\_ne\_\_\`)

**URL:** <https://community.sonarsource.com/t/s1940-should-not-propose-code-that-leads-to-infinite-recursion-e-g-avoid-in-ne/128341>\
**Category:** Report False-positive / False-negative...\
**Created:** [October 17, 2024, 11:57pm UTC](https://community.sonarsource.com/t/s1940-should-not-propose-code-that-leads-to-infinite-recursion-e-g-avoid-in-ne/128341 "2024-10-17T23:57:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bers](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@bers](https://community.sonarsource.com/u/bers)\
**Post date:** [October 17, 2024, 11:57pm UTC](https://community.sonarsource.com/t/s1940-should-not-propose-code-that-leads-to-infinite-recursion-e-g-avoid-in-ne/128341/1 "2024-10-17T23:57:30Z")

</div>

- What language is this for? Python
- Which rule? S1940
- Why do you believe it’s a false-positive/false-negative? Because following the advice leads to infinite recursion.
- Are you using
  - SonarLint? VS Code, v4.11.1

- How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

```python
class Int:
    def __init__ (self, value: int) -> None:
        super(). __init__ ()
        self.value = value

    def __eq__ (self, other: "Int") -> bool:
        return (self.value + 10) ** 2 == other.value

    def __ne__ (self, other: "Int") -> bool:
        return not self == other

print(Int(90) != Int(10000))

```

Now, following the advice to use `self != other` in ` __ne__ ` leads to

> RecursionError: maximum recursion depth exceeded

---

<div class="post-metadata">

**Author:** ![bers](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@bers](https://community.sonarsource.com/u/bers)\
**Post date:** [October 18, 2024, 12:00am UTC](https://community.sonarsource.com/t/s1940-should-not-propose-code-that-leads-to-infinite-recursion-e-g-avoid-in-ne/128341/2 "2024-10-18T00:00:20Z")

</div>

> [@bers](#):
>
> ` __ne__ `

(And you may ask, why is he implementing ` __ne__ ` when implementing ` __eq__ ` usually sufficies? Because when inheriting from a `builtin`, one inherits both ` __eq__ ` and ` __ne__ ` and has to override both.)

---

<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 24, 2024, 7:40am UTC](https://community.sonarsource.com/t/s1940-should-not-propose-code-that-leads-to-infinite-recursion-e-g-avoid-in-ne/128341/5 "2024-10-24T07:40:48Z")

</div>

Hello @bers,

Thank you for reporting this false positive. It makes a lot of sense that the rule should not trigger in this case. I created [SONARPY-2247](https://sonarsource.atlassian.net/browse/SONARPY-2247) to fix this.

Cheers,  
Guillaume
