3

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?

Paul
  • 1,874
  • 1
  • 19
  • 26
  • 2
    Why are you comparing the types? PyLint is warning you that comparing types may not be what you really want to do. – BrenBarn Jan 06 '16 at 17:37
  • 3
    Does `isinstance(a, type(b))` get rid of the warning? – Adam Funderburg Jan 06 '16 at 17:40
  • 3
    Comparing that two objects are the same type is not something commonly done in python. Why do you feel like you want to do that? – Chad S. Jan 06 '16 at 17:42
  • @BrenBarn I want to know if two dict entries have the same type so I can merge the values. – Paul Jan 06 '16 at 19:16
  • 1
    @AFunderburg Yes it does. – Paul Jan 06 '16 at 19:18
  • 1
    What do you mean by "merge"? What kinds of values are you expecting? In your example, you give integers. What does it mean to merge two integers? The comments by BrenBarn and Chad S. are asking you to take a step back. You might be missing the forest for the trees. – John Y Jan 06 '16 at 19:41
  • 1
    @AdamFunderburg While `isinstance(a,type(b)) and isinstance(b, type(a))` would be equivalent to a type comparison, it's also a whole lot slower. – Matthias Urlichs Mar 11 '18 at 09:02

3 Answers3

3

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

Matthias Urlichs
  • 2,301
  • 19
  • 29
2

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.

John Y
  • 14,123
  • 2
  • 48
  • 72
  • 5
    Yes. The point is not that `type(a) == type(b)` is an unidiomatic way to decide if two objects are the same type. The point is that deciding if two objects are the same type is not usually an idiomatic thing to do at all. – BrenBarn Jan 06 '16 at 20:59
1

If you absolutely must compare if two of anything are of the exact same type, and you cannot use type() for some reason, the last resort may be:

a.__class__ is b.__class__

Note also the limitation of this check on old-style classes.

Community
  • 1
  • 1
Brian
  • 7,394
  • 3
  • 25
  • 46