sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.5k stars 65 forks source link

Simplify comparison to boolean leads to false positive #349

Closed angrycaptain19 closed 1 year ago

angrycaptain19 commented 1 year ago

Given

class HasNoneAttr():
  def __init__(self):
        self.returns_none: None = None

instance = HasNoneAttr()

When

getattr(instance, 'returns_none', False) is False

Then

Suggested refactor given Sourcery - Simplify comparison to boolean not getattr(instance, 'returns_none', False)

Expected

No refactor suggestion A falsey comparison is not the same as an is False comparison. Using getattr with a default of False allows catching a case where the attr does in-fact exist, but returns None.

ruancomelli commented 1 year ago

Fixed in v1.4.0.