Skip to main content

An evaluator that verifies all methods in abstract base classes are properly marked as abstract

Project description

Swarmauri Logo

PyPI - Downloads PyPI - Python Version PyPI - License PyPI - swarmauri_evaluator_abstractmethods


Swarmauri Evaluator Abstractmethods

AbstractMethodsEvaluator inspects Python source files to verify that abstract base classes decorate their contract methods with @abstractmethod. The evaluator is designed for automated program analysis within the Swarmauri ecosystem and reports the exact lines that need attention together with summary metrics.

Features

  • Parses Python modules into an AST to discover classes inheriting from ABC or any custom bases supplied through abc_base_classes.
  • Flags methods on abstract classes that are missing the @abstractmethod decorator and records fully compliant methods for context.
  • Ignores private (_method) and dunder (__method__) members by default to focus on the public interface; these rules can be toggled with ignore_private and ignore_dunder.
  • Returns a detailed metadata payload containing compliance counts, per-method diagnostics, and an overall percentage score.
  • Provides aggregate_scores to average multiple evaluation runs while combining their metadata such as total issues and compliance percentages.

Installation

Install the evaluator with your preferred packaging tool:

pip install swarmauri_evaluator_abstractmethods
poetry add swarmauri_evaluator_abstractmethods
uv pip install swarmauri_evaluator_abstractmethods

Configuration

Instantiate the evaluator with optional keyword arguments:

Option Default Description
ignore_private True Skip methods whose names begin with a single underscore.
ignore_dunder True Skip dunder methods such as __init__.
abc_base_classes ["ABC", "abc.ABC"] Additional base class names that should be treated as abstract.

Evaluation Output

Calling evaluate returns (score, metadata) where score is the ratio of properly decorated methods to the total number of methods that should be abstract. metadata contains:

  • issues: A list of dictionaries with file, line, class name, method name, whether the decorator is present, and a descriptive message. Fully compliant methods are included for context.
  • total_abstract_classes, total_methods, total_abstract_methods, and total_missing_decorators: Counters summarizing the analysis.
  • percentage_compliant: Convenience metric equal to score * 100.

Usage Example

import textwrap
from typing import Dict

from swarmauri_core.programs.IProgram import DiffType, IProgram
from swarmauri_evaluator_abstractmethods import AbstractMethodsEvaluator


class InMemoryProgram(IProgram):
    def __init__(self, files: Dict[str, str]):
        self._files = files

    def diff(self, other: IProgram) -> DiffType:
        return {}

    def apply_diff(self, diff: DiffType) -> IProgram:
        return self

    def validate(self) -> bool:
        return True

    def clone(self) -> IProgram:
        return InMemoryProgram(dict(self._files))

    def get_source_files(self) -> Dict[str, str]:
        return self._files


def main() -> None:
    program = InMemoryProgram(
        {
            "shapes.py": textwrap.dedent(
                """
                from abc import ABC, abstractmethod

                class Shape(ABC):
                    @abstractmethod
                    def area(self):
                        ...
                """
            ).strip(),
            "incomplete.py": textwrap.dedent(
                """
                from abc import ABC

                class InvalidShape(ABC):
                    def area(self):
                        ...
                """
            ).strip(),
        }
    )

    evaluator = AbstractMethodsEvaluator()
    score, metadata = evaluator.evaluate(program)

    print(f"Score: {score:.2f}")
    for issue in metadata["issues"]:
        if not issue["has_abstractmethod"]:
            print(f"{issue['file']}:{issue['line']} -> {issue['message']}")


if __name__ == "__main__":
    main()

Output

Score: 0.50
incomplete.py:4 -> Method 'area' in abstract class 'InvalidShape' should be decorated with @abstractmethod

The evaluator reports both compliant and non-compliant methods in metadata["issues"], allowing you to surface violations while still retaining context about classes that already satisfy the contract.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_evaluator_abstractmethods-0.3.0.dev31.tar.gz.

File metadata

  • Download URL: swarmauri_evaluator_abstractmethods-0.3.0.dev31.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_evaluator_abstractmethods-0.3.0.dev31.tar.gz
Algorithm Hash digest
SHA256 e3dfd520ceaa4e5b19546ccc654542154b2c3ad98391812b1c5a5f8c3df188ba
MD5 82e8b8a413c0c45a346a7b60be4afdc6
BLAKE2b-256 9a8c7b5d20aeeaba988df72b782c71c6cc450a294788c133a7a9b02cddbb2b1d

See more details on using hashes here.

File details

Details for the file swarmauri_evaluator_abstractmethods-0.3.0.dev31-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_evaluator_abstractmethods-0.3.0.dev31-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_evaluator_abstractmethods-0.3.0.dev31-py3-none-any.whl
Algorithm Hash digest
SHA256 cf62b441c4ca12b90b8e0d40e57d8a4986a5e7f1390c324fab5fabfcf38ea588
MD5 486124c8507f63caf57df5c5956304b6
BLAKE2b-256 a65095a1f42ed9591559c16cb60c83b75cffda670b71e28acdf20b8e093697e2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page