Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"E271: do not compare types, use isinstance()" error

Tags:

python

pep8

The python PEP 8 linter doesn't like this:

assert type(a) == type(b)

It tells me to use "isinstance()" instead. But to use isinstance I would have to do something like

assert isinstance(a, type(b)) and isinstance(b, type(a))

which seems much more unwiedly, and I don't really see the point.

Is the linter being wise in some way that I can't see? Or am I being wise in some way the linter can't see?

like image 416
Labrador Avatar asked Sep 18 '18 21:09

Labrador


1 Answers

From context added in the comments:

according to my program's logic, one should have type(a) == type(b) at this point in the code, and I just want to assert that to see that everything is running smoothly

In this context, you should just ignore the linter because it's not suggesting anything useful to you. E721 was intended to warn people about issues via type-checks such as:

if type(a) == A:
    ...

The example above may be accidentally bugging the logical flow, by neglecting to consider the possibility that a is an instance of a subclass of A.

like image 70
wim Avatar answered Oct 23 '22 01:10

wim