The type of a value is compared with == or !=

tx0/python:exact-type-testrecommendedwarningsmell

What it finds

type(x) == Foo asks the class Foo whether it equals another class, which a metaclass can answer as it likes, and it rejects subclasses. isinstance(x, Foo) accepts subclasses, and type(x) is Foo tests for exactly that class. Reported are comparisons where one side is a call of the builtin type with one argument.

From the rule's source, exact_type_test.rs.

Example from the rule's tests
def same(a, b):    if type(a) == int:tx0 reports: types are compared with ==        return 1    if type(a) != type(b):tx0 reports: types are compared with ==        return 2    if type(a) is int:        return 3    return isinstance(a, int) or kind(a) == int
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:exact-type-test" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:exact-type-test] reason="..."

tx0 explain tx0/python:exact-type-test shows the same in the terminal. Found a false positive? Report it.