review.bot.misc#

Miscellaneous functions.

Module Contents#

Functions#

get_client(→ Union[openai.OpenAI, openai.AzureOpenAI])

Get the OpenAI client with the configuration file initialization.

open_logger([loglevel, formatstr])

Start logging to standard output.

add_line_numbers(patch)

Add line numbers to the added lines in a given patch string.

clean_string(input_text)

Clean type and lines strings.

clean_content(raw_content[, text_block])

Join the list of the content.

parse_suggestions(text_block)

Parse a given text block containing suggestions.

Attributes#

LOG

review.bot.misc.LOG#
review.bot.misc.get_client(config_file: str = None) openai.OpenAI | openai.AzureOpenAI#

Get the OpenAI client with the configuration file initialization.

Parameters:
config_filestr, optional

Initialization parameters of the client, by default None.

Returns:
Union[OpenAI, AzureOpenAI]

Initialized OpenAI client.

Raises:
OSError

Thrown if access token is missing.

review.bot.misc.open_logger(loglevel='DEBUG', formatstr='%(name)-20s - %(levelname)-8s - %(message)s')#

Start logging to standard output.

Parameters:
loglevelstr, optional

Standard logging level. One of the following:

  • "DEBUG" (default)

  • "INFO"

  • "WARNING"

  • "ERROR"

  • "CRITICAL"

formatstrstr, optional

Format string. See logging.Formatter.

Returns:
logging.RootLogger

Root logging object.

Examples

Output logging to stdout at the 'INFO' level.

>>> import review.bot as review_bot
>>> review_bot.open_logger('INFO')
review.bot.misc.add_line_numbers(patch)#

Add line numbers to the added lines in a given patch string.

The function takes a patch string and adds line numbers to the lines that start with a ‘+’. It returns a new patch string with added line numbers. Line numbers are added immediately to the left of any ‘+’.

Parameters:
patchstr

The patch string containing the changes in the file.

Returns:
str

The modified patch string with line numbers added to the added lines.

Examples

>>> patch = '''@@ -1,3 +1,5 @@
... +from itertools import permutations
... +
... import numpy as np
... import pytest'''
>>> add_line_numbers(patch)
'@@ -1,3 +1,5 @@
1   +from itertools import permutations
... +
... import numpy as np
... import pytest'
review.bot.misc.clean_string(input_text: str)#

Clean type and lines strings.

Clean type and lines strings in the LLM output, in case some unwanted characters are mixed with the desired content.

Parameters:
input_textstr

Raw text from the LLM.

Returns:
str

Cleaned text.

review.bot.misc.clean_content(raw_content: List, text_block=None)#

Join the list of the content.

Join the list of the content, that might be split due to preprocessing, and cleans starting commas if they appear.

Parameters:
raw_contentlist

List with the content of the suggestion.

Returns:
str

Content processed.

review.bot.misc.parse_suggestions(text_block: str)#

Parse a given text block containing suggestions.

Returns a list of dictionaries with keys: filename, lines, type, and text.

Parameters:
text_blockstr

The text block containing suggestions.

Returns:
list of dict

A list of dictionaries containing information about each suggestion.

Examples

>>> tblock = '''
... [tests/test_geometric_objects.py], [259-260], [SUGGESTION]: Replace `Rectangle` with `Quadrilateral` for clarity and consistency with the name of the class being tested.
... '''
>>> parse_suggestions(tblock)
[{'filename': 'tests/test_geometric_objects.py', 'lines': '259-260', 'type': 'SUGGESTION', 'text': 'Replace `Rectangle` with `Quadrilateral` for clarity and consistency with the name of the class being tested.'}]