redline
LowDesignhasattr-probe

Probes for a shape

Probes for a shape. Checks at runtime whether a value has some attribute (hasattr(x, '). The code should know.

This is how the comment reads inline, under the flagged line.

Why it matters

Feature-probing your own objects means the types don't say what they hold. It usually covers two incompatible versions of a thing.

What to do

Give the value one type and remove the probe.

Examples

Flagged
if hasattr(job, 'retry_count'):
Better
job.retry_count  # always set by Job()

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

if hasattr(x, 'y'):
    pass

Left alone

if x.y:
    pass

Research

Fallbacks and guards that hide missing data (`?? []`, `.get(k, "")`, `hasattr` probes, catch that returns empty, long `?.` chains). A timeout looks like an empty result, and broken assumptions travel downstream.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "hasattr-probe"
    ],
    "severity": {
      "hasattr-probe": "medium"
    }
  }
}

Read the source or report a false positive.