use isinstance instead of type

Metadata

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

Language: Python

Severity: Notice

Category: Best Practices

Description

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")
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis