"""The module containing the CallableSourceFingerprinter class."""

from __future__ import annotations

import hashlib
import inspect
from typing import Callable

from .base_fingerprinter import BaseFingerprinter


class CallableSourceFingerprinter(BaseFingerprinter):
    """A fingerprinter for Callables."""

    def fingerprint(self, data: Callable) -> str:
        """Generate a fingerprint for a Python Callable.

        Note:
            The fingerprint is generated by hashing the source code of the Callable.
            A side effect of this is that the fingerprint will change if the source code
            of the Callable changes. However, any changes to variables outside of the
            Callable's scope will not affect the fingerprint.

        Args:
            data: The Callable to be fingerprinted.

        Returns:
            The fingerprint as a hexadecimal string.
        """
        fingerprint = hashlib.md5(inspect.getsource(data).encode()).hexdigest()
        return fingerprint
