Skip to content

Checkers

Base

base

BaseChecker

BaseChecker(module: PythonModule)

Bases: ABC

Source code in docstr_health/checkers/base.py
13
14
15
16
17
def __init__(self, module: PythonModule) -> None:
    self.module = module
    if BaseChecker._shared_output is None:
        BaseChecker._shared_output = RichOutput(quiet=config.parameters.get("ci", False))
    self.output = BaseChecker._shared_output

module instance-attribute

module = module

output instance-attribute

output = BaseChecker._shared_output

check_module abstractmethod

check_module() -> ModuleReport | None

Check the module and return a report.

Subclasses must implement this method to define module-level analysis logic.

Returns:

Name Type Description
ModuleReport ModuleReport | None

Report with all metrics for statistics.

Source code in docstr_health/checkers/base.py
19
20
21
22
23
24
25
26
27
28
@abstractmethod
def check_module(self) -> ModuleReport | None:
    """Check the module and return a report.

    Subclasses must implement this method to define
    module-level analysis logic.

    Returns:
        ModuleReport: Report with all metrics for statistics.
    """

Docstring checker

docstring

DocstringChecker

DocstringChecker(
    module: PythonModule, settings: AppSettings
)

Bases: BaseChecker

Source code in docstr_health/checkers/docstring.py
13
14
15
16
17
18
19
20
21
22
def __init__(self, module: PythonModule, settings: AppSettings) -> None:
    super().__init__(module)
    self.inspected_statuses = {
        "bad": 0,
        "good": 0,
        "special": 0,
        "epic": 0,
        "skipped": 0,
    }
    self.settings: AppSettings = settings

inspected_statuses instance-attribute

inspected_statuses = {
    "bad": 0,
    "good": 0,
    "special": 0,
    "epic": 0,
    "skipped": 0,
}

settings instance-attribute

settings: AppSettings = settings

total_inspected_statuses property

total_inspected_statuses

module_status property

module_status: str

Module status is calculated as follows: All bad OR bad > threshold_bad% default: 80 -> bad bad > threshold_warning% default: 60 -> warning epic > threshold_epic% default: 50 -> epic special > threshold_special% default: 50 OR tied -> special otherwise -> good

Rounds down when special and epic are tied.

get_statistics

get_statistics() -> list[Text]
Source code in docstr_health/checkers/docstring.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def get_statistics(self) -> list[Text]:
    statistics = []
    statistics.append(Text("Statistics:", justify="center"))
    total = self.total_inspected_statuses

    for status, value in self.inspected_statuses.items():
        if value > 0:
            symbol = config.parameters[f"{status}_symbol"]
            color = config.parameters[f"{status}_color"]
            rate = round(value / total * 100, 1)
            statistics.append(
                Text(f"{symbol} {status} - {value} ({rate}%)", style=color)
            )
    return statistics

check_module

check_module() -> ModuleReport | None
Source code in docstr_health/checkers/docstring.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def check_module(self) -> ModuleReport | None:
    if not self.module.functions_to_check:
        return None

    if self.settings.doc_check:
        module_docstring = self.module.get_module_docstring()
        self.check_docstring(module_docstring)

    if not self.settings.compact:
        self._display()
    else:
        for func in self.module.functions_to_check:
            self.inspect_func_status(func)

    return ModuleReport(
        module_status=self.module_status,
        inspected_functions=self.module.functions_to_check,
    )

inspect_func_status

inspect_func_status(
    func: PythonFunction,
) -> StatusDocstring

Checking the state of a function docstring.

Source code in docstr_health/checkers/docstring.py
133
134
135
136
def inspect_func_status(self, func: PythonFunction) -> StatusDocstring:
    """Checking the state of a function docstring."""
    docstring = func.get_docstring()
    return self.check_docstring(docstring)

get_func_status_text

get_func_status_text(func: PythonFunction) -> Text
Source code in docstr_health/checkers/docstring.py
138
139
140
def get_func_status_text(self, func: PythonFunction) -> Text:
    status = self.inspect_func_status(func)
    return self.output.func_docstring_status(func_name=func.name, status=status)

check_docstring

check_docstring(
    docstring: str | object | None,
) -> StatusDocstring

Evaluate the quality of a docstring and assign a status.

The evaluation follows a priority based on content presence and keyword matching: 1. BAD: Docstring is missing or empty. 2. GOOD: Has content but lacks EPIC/SPECIAL keywords. 3. SPECIAL: Contains basic parameter keywords. 4. EPIC: Contains advanced keywords.

This method also increments the corresponding counter in self.inspected_statuses.

Parameters:

Name Type Description Default
docstring str | object | None

The docstring text to evaluate, or None.

required

Returns:

Name Type Description
StatusDocstring StatusDocstring

The matched status enum value.

Source code in docstr_health/checkers/docstring.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def check_docstring(self, docstring: str | object | None) -> StatusDocstring:
    """Evaluate the quality of a docstring and assign a status.

    The evaluation follows a priority based on content presence
    and keyword matching:
        1. BAD: Docstring is missing or empty.
        2. GOOD: Has content but lacks EPIC/SPECIAL keywords.
        3. SPECIAL: Contains basic parameter keywords.
        4. EPIC: Contains advanced keywords.

    This method also increments the corresponding counter in
    ``self.inspected_statuses``.

    Args:
        docstring: The docstring text to evaluate, or None.

    Returns:
        StatusDocstring: The matched status enum value.
    """
    if not docstring:
        self.inspected_statuses["bad"] += 1
        return StatusDocstring.BAD

    if any(req in str(docstring) for req in config.requires["epic"]):
        self.inspected_statuses["epic"] += 1
        return StatusDocstring.EPIC
    if any(req in str(docstring) for req in config.requires["special"]):
        self.inspected_statuses["special"] += 1
        return StatusDocstring.SPECIAL

    self.inspected_statuses["good"] += 1
    return StatusDocstring.GOOD

Project checker

project

ProjectChecker

ProjectChecker(source: BaseSource, settings: AppSettings)
Source code in docstr_health/checkers/project.py
14
15
16
17
18
19
20
def __init__(self, source: BaseSource, settings: AppSettings) -> None:
    self.source: BaseSource = source
    self.settings: AppSettings = settings
    self._python_files = self._scan_python_files()
    self.modules = [PythonModule(file_path=file) for file in self._python_files]
    self._reports: list[ModuleReport] = []
    self._skipped_modules: list[tuple[Path, str]] = []

source instance-attribute

source: BaseSource = source

settings instance-attribute

settings: AppSettings = settings

modules instance-attribute

modules = [
    PythonModule(file_path=file)
    for file in self._python_files
]

skipped_modules property

skipped_modules: list[tuple[Path, str]]

get_quantity_of_func_type

get_quantity_of_func_type() -> dict
Source code in docstr_health/checkers/project.py
26
27
28
29
30
31
def get_quantity_of_func_type(self) -> dict:
    counters = [
        Counter(report.get_func_type_quantity()) for report in self._reports
    ]
    total_counter = sum(counters, Counter())
    return dict(total_counter)

get_count_modules

get_count_modules() -> int
Source code in docstr_health/checkers/project.py
33
34
def get_count_modules(self) -> int:
    return len(self._reports)

get_statuses_stat

get_statuses_stat() -> Counter
Source code in docstr_health/checkers/project.py
36
37
38
def get_statuses_stat(self) -> Counter:
    statuses: list = [report.module_status for report in self._reports]
    return Counter(statuses)

get_project_report

get_project_report()
Source code in docstr_health/checkers/project.py
40
41
def get_project_report(self):
    return ProjectReport(statuses=self.get_statuses_stat())

docstring_check

docstring_check() -> Generator[PythonModule, None, None]
Source code in docstr_health/checkers/project.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def docstring_check(
    self,
) -> Generator[PythonModule, None, None]:
    for module in self.modules:
        try:
            checker = DocstringChecker(module, settings=self.settings)
            module_report = checker.check_module()
            if module_report is not None:
                self._reports.append(module_report)
        except PythonParseError as e:
            self._skipped_modules.append((module.file_path, str(e)))

        module.release_tree()
        yield module

CI failure checker

ci_failure

CiFailureChecker

CiFailureChecker(project_report: ProjectReport)
Source code in docstr_health/checkers/ci_failure.py
10
11
def __init__(self, project_report: ProjectReport) -> None:
    self._project_status = project_report.get_project_status()

assertion

assertion() -> None
Source code in docstr_health/checkers/ci_failure.py
13
14
15
16
17
18
19
20
21
22
23
24
def assertion(self) -> None:
    if config.parameters.get("ci", False):
        current_name = StatusProject(self._project_status).name
        failure_name = StatusProject(config.parameters.get("failure_level")).name

        project_priority = StatusProjectPrioritized[current_name].value
        failure_priority = StatusProjectPrioritized[failure_name].value

        logger.debug(f"{project_priority=}, {failure_priority=}")

        if project_priority <= failure_priority:
            sys.exit(1)