false positive for unidiomatic-typecheck?

I am seeing the following message from pylint:
foo.py:311: [C0123(unidiomatic-typecheck), _legend] Using type() instead of isinstance() for a typecheck.
My line is: if type(g) not in unique:
That is, I am maintaing a list of unique types in a set of objects. This isn't a typecheck. Shouldn't the warning only be produced when type(x) is compared with type(y)?
Thanks, Ben

From: Of Ben Elliston Sent: Wednesday, September 28, 2016 6:40 AM To: code-quality@python.org Subject: [code-quality] false positive for unidiomatic-typecheck?
I am seeing the following message from pylint:
foo.py:311: [C0123(unidiomatic-typecheck), _legend] Using type() instead
of
isinstance() for a typecheck.
My line is: if type(g) not in unique:
Do note that this is the same as if isinstance(g, tuple(unique)):
Since isinstance() accepts either a type or a tuple or types (not a set, list, or any other iterable, though).
That is, I am maintaing a list of unique types in a set of objects. This isn't a typecheck. Shouldn't the warning only be produced when type(x) is compared with type(y)?
Actually, that is a typecheck; it's just not one you're used to seeing. You're checking for the presence of the type of 'g' in a set. Using `type(x)` is often unidiomatic, and in almost every case (including this one), you have a better alternative :)
Thanks, Ben
-Emanuel

On 9/28/16 6:40 AM, Ben Elliston wrote:
I am seeing the following message from pylint:
foo.py:311: [C0123(unidiomatic-typecheck), _legend] Using type() instead of isinstance() for a typecheck.
My line is: if type(g) not in unique:
That is, I am maintaing a list of unique types in a set of objects. This isn't a typecheck. Shouldn't the warning only be produced when type(x) is compared with type(y)?
I agree with pylint here: your line is unidiomatic. That doesn't mean it's a bad line. Just add a pylint pragma. It's OK to know more about your code than pylint does.
--Ned.
participants (3)
-
Ben Elliston
-
Emanuel Barry
-
Ned Batchelder