"E721: do not compare types, use isinstance()" 错误

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

python PEP 8 linter 不喜欢这样:

assert type(a) == type(b)

它告诉我改用 "isinstance()"。但是要使用 isinstance 我必须做类似

的事情
assert isinstance(a, type(b)) and isinstance(b, type(a))

这看起来更笨拙,我真的不明白这一点。

linter 是否以某种我看不到的方式变得明智?还是我在 linter 看不到的某些方面很聪明?

来自评论中添加的上下文:

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

在这种情况下,您应该忽略 linter,因为它不会向您提供任何有用的建议。 E721 旨在通过类型检查向人们发出有关问题的警告,例如:

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

上面的例子可能不小心打乱了逻辑流程,忽略了 aA.

子类的实例的可能性。