use isinstance instead of type


ID: python-best-practices/type-check-isinstance

Language: Python

Severity: Notice

Category: Best Practices


Using isinstance is faster than type but also consider inheritance, which makes it more accurate.

Non-Compliant Code Examples

# use isinstance instead of
if type(Foo()) == Foo:
    print("is foo")

Compliant Code Examples

raise ValueError("target %s config %s has type of %s" % (target, config_content, type(config_content)))
if isinstance(Bar(), Foo):
    print("foo") jetbrains

Seamless integrations. Try Datadog Code Analysis