Inconsistent determination of statements with no effect and possible suppression

In Python, I am implementing a class that implements certain operators, including __gt__ (>) and __add__ (+). Both have side effects. Using IntelliJ with the Python plugin, I get a warning that the statement with > does not have an effect (which is true for the usual non-side effect implementation). This does not happen for +.

So there seem to be two problems: PyCharm can't detect when dunder functions cause side effects and thus wrongly assumed no effect when no assignment takes place. Secondly, it is inconsistent between different operators, unless I am mistaken and __add__ normally does cause side effects.

Since I hope to distribute this class to other people, I was wondering if there is a solution for the first problem, so that they don't get false warnings.

Here is a minimal viable code example.

class A:
def __gt__(self, other):
self.gt = other

def __add__(self, other):
self.add = other


a = A()
a > a # Warning: statement seems to have no effect
a + a # No warning
0
2 comments

I've modified your code a bit to better demonstrate the issue:

 

class A:
def __gt__(self, other):
self.gt = other
print('gt!')

def __add__(self, other):
self.add = other
print('add!')


a = A()
a > a # Warning: statement seems to have no effect
a + a # No warning

Running the code outputs both prints so the statement does have an effect, while PyCharm shows the false-positive. I've submitted a bug report based on your case: https://youtrack.jetbrains.com/issue/PY-39323

Feel free to comment.

0
Avatar
Permanently deleted user

Thank you for submitting the report! It was not accepted but at least considered.

0

Please sign in to leave a comment.