loki.lint.utils

Functions

get_filename_from_parent(obj)

Try to determine filename of the source file for an IR object

get_location_hash(location)

Utility routine that produces an identifier hash for a location in the IR

is_rule_disabled(ir, identifiers[, ...])

Check if a Linter rule is disabled in the provided context via user annotations

Classes

Fixer()

Operater class to fix problems reported by fixable rules.

class Fixer

Bases: object

Operater class to fix problems reported by fixable rules.

classmethod fix_module(module, reports, config, sourcefile)

Call fix_module for all rules and apply the transformations.

classmethod fix_subroutine(subroutine, reports, config, sourcefile)

Call fix_subroutine for all rules and apply the transformations.

classmethod fix_sourcefile(sourcefile, reports, config)

Call fix_sourcefile for all rules and apply the transformations.

classmethod fix(ast, reports, config)

Attempt to fix problems flagged by fixable rules in the given IR object.

This routine calls fix_module, fix_subroutine and fix_file as applicable for all rules on all entities in the given IR object.

Parameters:
  • ast (Sourcefile, Module, or Subroutine) – the IR object to be fixed.

  • reports (list) – the fixable RuleReport reports.

  • config (dict) – a dict with the config values.

Returns:

the modified AST object.

get_filename_from_parent(obj)

Try to determine filename of the source file for an IR object

It follows parent attributes until Sourcefile is encountered.

Parameters:

obj (Sourcefile, Subroutine or Module) – A source file, module or subroutine object.

Returns:

The filename if found, else None.

Return type:

str or NoneType

get_location_hash(location)

Utility routine that produces an identifier hash for a location in the IR

Parameters:

location (Node or ProgramUnit) – The IR object for which to produce the hash

Returns:

The hash as a string or, if no hash can be created for location, None is returned.

Return type:

str or None

is_rule_disabled(ir, identifiers, disabled_line_hashes=None)

Check if a Linter rule is disabled in the provided context via user annotations

This looks for comments of the form

Where RuleName is one of the provided identifiers.

If ir is a LeafNode, only any attached in-line comments are checked. If ir is any other IR object, the entire subtree below this object is searched.

Parameters:
  • ir (Node or ProgramUnit) – The IR object for which to check if a rule is disabled

  • identifiers (list) – A list of string identifiers via which the rule can be disabled

  • disabled_line_hashes (list, optional) – A list of hashes. If the first line of ir corresponding to the violation matches a hash in this list, the rule is disabled

Returns:

Returns True if a rule is disabled, otherwise False

Return type:

bool