File: explain.py

package info (click to toggle)
python-refurb 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,700 kB
  • sloc: python: 9,468; makefile: 40; sh: 6
file content (40 lines) | stat: -rw-r--r-- 1,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pathlib import Path
from textwrap import dedent

from refurb.loader import get_error_class, get_modules
from refurb.settings import Settings

from .error import ErrorCode


def explain(settings: Settings) -> str:
    lookup = settings.explain

    for module in get_modules(settings.load):
        error = get_error_class(module)

        if error and ErrorCode.from_error(error) == lookup:
            docstring = error.__doc__ or ""

            if docstring.startswith(f"{error.__name__}("):
                return f'refurb: Explanation for "{lookup}" not found'

            output = ""

            if settings.verbose:
                root = Path(__file__).parent.parent
                file = Path(module.__file__ or "").relative_to(root)

                output += f"Filename: {file}\n\n"

            docstring = dedent(error.__doc__ or "").strip()

            name = error.name or "<name unknown>"
            error_code = ErrorCode.from_error(error)
            categories = " ".join(f"[{x}]" for x in error.categories)

            output += f"{error_code}: {name} {categories}\n\n{docstring}"

            return output

    return f'refurb: Error code "{lookup}" not found'