None check: false positive with True.sonarlint(python:S5727)

I believe SonarLint is making overly strong assumptions about the “+” operator, although I am not exactly sure what it is doing. Here is a minrep:

class X():
    def __pos__(self):
        return None # more complex logic here might sometimes return other types as well

x = +X() 

if x is not None: # Remove this identity check; it will always be True. sonarlint(python:S5727)
  • Operating system: MacOS 13.13.1
  • Visual Studio version: 1.84.2
  • SonarLint plugin version: 4.3.0
  • Programming language you’re coding in: Python 3.10.10
  • Is connected mode used: SonarCube 9.7 I think

Hello @klavtone,

Thank you for reporting this false positive.

Indeed, our type inference engine assumes that unary plus will not change the type of a variable. I created the following ticket to fix this.


1 Like