The Understand Python API gives scripts access to everything Understand knows about your code — entities, references, metrics, and dependencies — and powers custom checks, graphs, and reports.

Violation#

class understand.Violation#

Bases: object

A violation

Violations are errors and warnings. They have a location that can be accessed with file, line, and column. Use text to get a description of the error or warning. The check_id method will return the check id the violation came from. Analysis errors and warnings have the check ids “UND_ERROR” and “UND_WARNING” respectively.

A violation can also carry extra detail from its check: highlights returns ViolationHighlight ranges to mark, notes returns ViolationNote explanations, which can be in other files, and fixit_hints returns FixItHint edits that fix the violation.

Violation line and column numbers are 1-based. They are 0 when the violation has no line or column.

Methods Summary

check_id

Return the id of the check this violation came from.

column

Return the column number of the violation.

ent_uniquename

Return the unique name of the entity the violation is associated with.

file

Return the file the violation is located in.

fixit_hints

Return the suggested edits that fix the violation.

highlights

Return the highlighted ranges of text in the violation's file.

ignores

Return a list of (source, note) ignore tuples.

line

Return the line number of the violation.

notes

Return the notes that explain the violation.

text

Return the text describing the violation.

Classes Summary

FixItHint

A suggested edit that fixes a violation

ViolationHighlight

A highlighted range of text

ViolationNote

A note attached to a violation

Methods Documentation

check_id() → str#

Return the id of the check this violation came from.

Return type:

str

Returns:

the violation’s check id

column() → int#

Return the column number of the violation.

Columns are 1-based. The column is 0 when the check did not report one.

Return type:

int

Returns:

the column number

ent_uniquename() → str#

Return the unique name of the entity the violation is associated with.

This will be an empty string if there is no associated entity. To retrieve the entity, if it still exists, use db.lookup_uniquename.

Return type:

str

Returns:

the entity’s unique name or empty string

file() → str#

Return the file the violation is located in.

Return type:

str

Returns:

the violation’s file

fixit_hints() → list[understand.FixItHint]#

Return the suggested edits that fix the violation.

Return type:

list[understand.FixItHint]

Returns:

the list of fix-it hints, which may be empty

highlights() → list[understand.ViolationHighlight]#

Return the highlighted ranges of text in the violation’s file.

Return type:

list[understand.ViolationHighlight]

Returns:

the list of highlights, which may be empty

ignores() → list[tuple[str, str]]#

Return a list of (source, note) ignore tuples.

The source of the ignore will be one of the following:

  • “Annotation”: undcc keyword in an annotation.

  • “Comment”: undcc keyword in a comment.

  • “Baseline”: violation found in baseline.

  • “User”: false positive rule stored in project

  • “Excluded”: violation outside inspected regions. This can happen when a global check references files outside the target files, or when Git is used and the violation was not on a line changed since the Git commit.

  • “Unknown”: possible with violations imported from another tool

Some ignore sources support notes. The second value in the tuple will be the note or empty string if no note was given. It’s possible for the same source to appear in the list multiple times, for example, if multiple comments matched the violation.

Return type:

list[tuple[str, str]]

Returns:

the list of ignore tuples

line() → int#

Return the line number of the violation.

Lines are 1-based. The line is 0 when the violation is not on a line.

Return type:

int

Returns:

the line number

notes() → list[understand.ViolationNote]#

Return the notes that explain the violation.

Return type:

list[understand.ViolationNote]

Returns:

the list of notes, which may be empty

text() → str#

Return the text describing the violation.

Return type:

str

Returns:

the violation’s text

FixItHint#

class understand.FixItHint#

Bases: object

A suggested edit that fixes a violation

Fix-it hints are returned from Violation.fixit_hints. Apply a hint by replacing the text from the beginning position up to the end position with replacement_text. A hint whose beginning and end are the same position inserts text, and a hint with empty replacement text deletes text.

Hint line and column numbers are 1-based. The beginning is inclusive and the end is exclusive, the same as ViolationHighlight. A hint applies to the source as it was when the violation was found.

Methods Summary

column_begin

Return the column of the first character to replace.

column_end

Return the column just after the last character to replace.

file

Return the file the hint applies to.

line_begin

Return the line of the first character to replace.

line_end

Return the line of the position just after the last character to replace.

replacement_text

Return the text that replaces the hint's range.

Methods Documentation

column_begin() → int#

Return the column of the first character to replace.

Columns are 1-based and the beginning is inclusive.

Return type:

int

Returns:

the beginning column

column_end() → int#

Return the column just after the last character to replace.

Columns are 1-based and the end is exclusive. For an insertion, the end is the same position as the beginning.

Return type:

int

Returns:

the ending column

file() → str#

Return the file the hint applies to.

Return type:

str

Returns:

the hint’s file

line_begin() → int#

Return the line of the first character to replace.

Lines are 1-based and the beginning is inclusive.

Return type:

int

Returns:

the beginning line

line_end() → int#

Return the line of the position just after the last character to replace.

Lines are 1-based and the end is exclusive.

Return type:

int

Returns:

the ending line

replacement_text() → str#

Return the text that replaces the hint’s range.

Return type:

str

Returns:

the replacement text, which is empty for a deletion

ViolationHighlight#

class understand.ViolationHighlight#

Bases: object

A highlighted range of text

Highlights are returned from Violation.highlights and ViolationNote.highlights. A highlight is in the file of the violation or note it came from.

Highlight line and column numbers are 1-based. The beginning is inclusive and the end is exclusive: line_end and column_end give the position just after the last highlighted character. This differs from Lexeme.column_end, which is the last column of the lexeme.

A highlight that covers whole lines may use column 0, beginning at column 0 of its first line and ending at column 0 of the line after its last line.

Methods Summary

column_begin

Return the column of the first highlighted character.

column_end

Return the column just after the last highlighted character.

line_begin

Return the line of the first highlighted character.

line_end

Return the line of the position just after the last highlighted character.

Methods Documentation

column_begin() → int#

Return the column of the first highlighted character.

Columns are 1-based and the beginning is inclusive. A highlight that covers whole lines may begin at column 0.

Return type:

int

Returns:

the beginning column

column_end() → int#

Return the column just after the last highlighted character.

Columns are 1-based and the end is exclusive. For example, a highlight of a two-character name at column 9 ends at column 11. A highlight that covers whole lines may end at column 0.

Return type:

int

Returns:

the ending column

line_begin() → int#

Return the line of the first highlighted character.

Lines are 1-based and the beginning is inclusive.

Return type:

int

Returns:

the beginning line

line_end() → int#

Return the line of the position just after the last highlighted character.

Lines are 1-based and the end is exclusive. This is usually the line of the last highlighted character, but a highlight that covers whole lines ends at column 0 of the line after its last line.

Return type:

int

Returns:

the ending line

ViolationNote#

class understand.ViolationNote#

Bases: object

A note attached to a violation

Notes are returned from Violation.notes. A note explains part of a violation and can be in a different file than the violation.

Note line and column numbers are 1-based.

Methods Summary

column

Return the column number of the note.

file

Return the file the note is located in.

highlights

Return the highlighted ranges of text in the note's file.

line

Return the line number of the note.

text

Return the text of the note.

Methods Documentation

column() → int#

Return the column number of the note.

Columns are 1-based. The column is 0 when the note has no column.

Return type:

int

Returns:

the column number

file() → str#

Return the file the note is located in.

Return type:

str

Returns:

the note’s file

highlights() → list[understand.ViolationHighlight]#

Return the highlighted ranges of text in the note’s file.

Return type:

list[understand.ViolationHighlight]

Returns:

the list of highlights, which may be empty

line() → int#

Return the line number of the note.

Lines are 1-based. The line is 0 when the note is not on a line.

Return type:

int

Returns:

the line number

text() → str#

Return the text of the note.

Return type:

str

Returns:

the note’s text