Say I have two variables:
a = 123
b = 234
I want to compare their type. Clearly the obvious way is:
type(a) == type(b)
However, pylint gives me a warning, which I would like to avoid if possible:
Using type() instead of isinstance() for a typecheck. (unidiomatic-typecheck)
(I believe this isn't specifically warning about my use.)
In the case of comparing two variable types, isinstance can't be used, I believe.
How do I compare the type of two variables without generating PyLint warnings?
Just turn off the pylint warning.
On a single line, you can do that thusly:
types_match = type(a) is type(b) # pylint: disable=unidiomatic-typecheck
See https://pylint.readthedocs.io/en/latest/user_guide/message-control.html
People get too hung up about linters. It's like the PEP 8 style guide. They are guidelines, and you have to use your own judgment.
If you need to know whether the type of something is the same as the type of something else, then absolutely the straightforward
type(a) == type(b)
is the most Pythonic way. It's not idiomatic Python to jump through crazy hoops to do simple things if you can avoid it.
All that said, it is usually not the case in Python that you really need to know whether the types of two things are exactly the same. (See comments by BrenBarn and Chad S.) So the linter may be pointing to a larger "code smell" than just that one line which compares the two types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With