Tuning Python analysis

Tuning allows you to make Python analysis respect specifics of the project and bring it closer to the project context. You can tune your python analysis by giving the parameters to the engine, so it can produce better analysis results about your code system.

The tuning mechanism is implemented by passing arguments to a particular <checker>. Every Python category section has a list of parameters that are supported by related checkers. You can define these parameters and import it into your project configuration.

Adding parameters and importing them into Klocwork involves the following steps:

  • Find the documentation of the checker that you want to tune.

  • Find the documentation of the relevant checker category; there is a list of parameters that can be tuned. If there are no parameters listed, then the checker cannot be tuned.

  • Create a '.pconf' checker configuration file and add parameters for the checker section that matches a category name. The parameter should be named with the arg- prefix.

  • Import the '.pconf' file into the project.

Example

Let's tune the checker 'PY3.R0903':

  1. The checker category is 'Design'. The 'Design' category has a number of parameters you can tune. Let's take the following two parameters and tune them:

    • exclude-too-few-public-methods (default: )
    • min-public-methods (default: 2)
  2. Next, create 'python.pconf', with the above parameters included, and add your custom values ('abs.*' and '5'), as seen in the example below:

    Copy
    <?xml version="1.0" encoding="UTF-8"?>
    <errors language="Python" version="1.4">
        <checker id="PY3.DESIGN">
            <parameter name="arg-exclude-too-few-public-methods" value="abs.*"/>
            <parameter name="arg-min-public-methods" value="5"/>
        </checker>
    </errors>

  3. Import 'python.pconf' into your project. For example, by using the kwadmin import-config command

    kwadmin import-config myProject C:\Klocwork\python.pconf

Tuning a disabled checker

To tune a disabled checker, you need to import two .pconf files: one to enable the error id, and another to provide the checker's arguments.

For example, if the checker 'PY3.R0903' is disabled, follow these steps to tune it:

  1. Create two configuration files, for instance, 'enable_error_id.pconf' and 'checker_params.pconf':

    • enable_error_id.pconf

      Copy
      <?xml version="1.0" encoding="UTF-8"?>
      <errors language="Python" version="1.4">
          <error enabled="true" id="PY3.R0903"/>
      </errors>
    • checker_params.pconf

      Copy
      <?xml version="1.0" encoding="UTF-8"?>
      <errors language="Python" version="1.4">
          <checker id="PY3.R0903">
              <parameter name="arg-exclude-too-few-public-methods" value="abs.*"/>
              <parameter name="arg-min-public-methods" value="5"/>
          </checker>
      </errors>
  2. Merge the two configuration files by running the import command with the --merge-config option:

    kwadmin import-config <project-name> enable_error_id.pconf
    					
    kwadmin import-config <project-name> checker_params.pconf --merge-config