Skip to content

feature suggestion for is compare between different types #5763

Open
@orSolocate

Description

@orSolocate

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.

  1. The __eq__ of Python classes can be modified to return True for different comparison values.

  2. There is a (stupid) case where == actually returns True for different types. it happens for int and float, e.g. 1.0==1.
    [note that the statement 1.0 is 1 returns False, as 1.0 is of type float and 1 is of type int]
    [also note that this rare case only happens for small numbers]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Discussion 🤔Needs PRThis issue is accepted, sufficiently specified and now needs an implementationNeeds decision 🔒Needs a decision before implemention or rejectionNeeds investigation 🔬A bug or crash where it's not immediately obvious what is happenningProposal 📨

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions