What am I trying to do? Python's type hinting is useful for spotting certain coding mistakes, for instance if you accidentally try to call a type-hint decorated function with a mistake in the arguments. In statically typed languages we are also able to spot mistakes in operator mismatches, for example if you try to compare apples to oranges. Is it possible to extend the set of such warnings to include mismatched operators, for instance the equality operator ==
, when coding python?
Specifically, I'm using PyCharm and would like to see a highlighted warning "Expected type 'A', got 'B' instead" when using the equality operator.
What have I tried? Of course, the first thing I tried was a type-mismatched equality operator and PyCharm gave no feedback. Next, I considered using function calls. As mentioned, function calls exhibit the desired behaviour already, so I tried overriding the __eq__()
. When I use the dunder directly (a.__eq__(b)
) then I see the warning I want, however if I just perform the operator in the usual fashion...no dice.
Here is a screen shot of the code showing the warning only on the dunder: