Description
Current problem
Comparing two different types using the is
operator is always False
.
For example for this code:
A = 5
STR1 = ‘5’
if A is STR1:
pass
A
is of type int
and STR1
is of type str
.
is
compare between those types is always False
.
The user should be alert about it, and consider modifying his code.
As far as I checked there is no checker that checks that..
Desired solution
I suggest a new checker (or extension of an existing one) that raises a message for this case.
message name suggestion: is-different-types
or different-types-is-expression
message type: Warning
message content suggestion:
An 'is' comparison between different types always fails, consider using casting
Additional context
The check should be general to any comparison/expression, not necessarily an if
comparison.
I think we should raise a message even for cases like:
condition = '5' is 4
I thought about extending the checker to a ==
operator, but I am not really fond of the idea and I will explain why.
-
The
__eq__
of Python classes can be modified to returnTrue
for different comparison values. -
There is a (stupid) case where
==
actually returnsTrue
for different types. it happens forint
andfloat
, e.g.1.0==1
.
[note that the statement1.0 is 1
returnsFalse
, as1.0
is of typefloat
and1
is of typeint
]
[also note that this rare case only happens for small numbers]