File: main.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 (388 lines) | stat: -rw-r--r-- 10,585 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import json
import re
import time
from collections.abc import Callable, Sequence
from contextlib import suppress
from functools import cache, partial
from importlib import metadata
from io import StringIO
from pathlib import Path
from tempfile import mkstemp

from mypy.build import build
from mypy.errors import CompileError
from mypy.main import process_options

from .error import Error, ErrorCode
from .explain import explain
from .gen import main as generate
from .loader import load_checks
from .settings import Settings, load_settings
from .visitor import RefurbVisitor


def usage() -> None:
    print(
        """\
usage: refurb [--ignore err] [--load path] [--debug] [--quiet] [--enable err]
              [--disable err] [--enable-all] [--disable-all]
              [--config-file path] [--python-version version] [--verbose | -v]
              [--format format] [--sort sort] [--timing-stats file]
              SRC [SRCS...] [-- MYPY_ARGS]
       refurb [--help | -h]
       refurb [--version]
       refurb --explain err
       refurb gen

Command Line Options:

--help, -h            This help menu.
--version             Print version information.
--ignore err          Ignore an error. Can be repeated.
--load module         Add a module to the list of paths to be searched when looking for checks. Can be repeated.
--debug               Print the AST representation of all files that where checked.
--quiet               Suppress default "--explain" suggestion when an error occurs.
--enable err          Load a check which is disabled by default.
--disable err         Disable loading a check which is enabled by default.
--config-file file    Load "file" instead of the default config file.
--explain err         Print the explanation/documentation from a given error code.
--disable-all         Disable all checks by default.
--enable-all          Enable all checks by default.
--python-version x.y  Version of the Python code being checked.
--verbose             Increase verbosity.
--format format       Output errors in specified format. Can be "text" or "github".
--sort sort           Sort errors by sort. Can be "filename" or "error".
--timing-stats file   Export timing information (as JSON) to file.

Positional Args:

SRC                   A list of files or folders to check.
MYPY_ARGS             Extra args to be passed directly to Mypy.


Subcommands:

gen              Generate boilerplate code for a new check. Useful for developers.
"""
    )


def version() -> str:  # pragma: no cover
    refurb_version = metadata.version("refurb")
    mypy_version = metadata.version("mypy")

    return f"Refurb: v{refurb_version}\nMypy: v{mypy_version}"


@cache
def get_source_lines(filepath: str) -> list[str]:
    return Path(filepath).read_text("utf8").splitlines()


def is_ignored_via_comment(error: Error) -> bool:
    assert error.filename

    line = get_source_lines(error.filename)[error.line - 1].rstrip()

    if comment := re.search(r"""# noqa(: [^'"]*)?$""", line):
        ignore = str(ErrorCode.from_error(type(error)))
        error_codes = comment.group(1)

        return not error_codes or any(
            error_code == ignore for error_code in error_codes[2:].replace(",", " ").split(" ")
        )

    return False


def is_ignored_via_amend(error: Error, settings: Settings) -> bool:
    assert error.filename

    path = Path(error.filename).resolve()
    error_code = ErrorCode.from_error(type(error))
    config_root = Path(settings.config_file).parent if settings.config_file else Path()

    for ignore in settings.ignore:
        if ignore.path:
            ignore_path = (config_root / ignore.path).resolve()

            if path.is_relative_to(ignore_path):
                if isinstance(ignore, ErrorCode):
                    return str(ignore) == str(error_code)

                return ignore.value in error.categories

    return False


def should_ignore_error(error: Error | str, settings: Settings) -> bool:
    if isinstance(error, str):
        return False

    return (
        not error.filename
        or is_ignored_via_comment(error)
        or is_ignored_via_amend(error, settings)
    )


def run_refurb(settings: Settings) -> Sequence[Error | str]:
    stdout = StringIO()
    stderr = StringIO()

    try:
        args = [
            *settings.files,
            *settings.mypy_args,
            "--exclude",
            ".*\\.pyi",
            "--explicit-package-bases",
            "--namespace-packages",
        ]

        files, opt = process_options(args, stdout=stdout, stderr=stderr)

    except SystemExit:
        lines = ["refurb: " + err for err in stderr.getvalue().splitlines()]

        return lines + stdout.getvalue().splitlines()

    finally:
        stdout.close()
        stderr.close()

    opt.incremental = True
    opt.fine_grained_incremental = True
    opt.cache_fine_grained = True
    opt.allow_redefinition = True
    opt.local_partial_types = True
    opt.python_version = settings.get_python_version()

    mypy_timing_stats = Path(mkstemp()[1]) if settings.timing_stats else None
    opt.timing_stats = str(mypy_timing_stats) if mypy_timing_stats else None

    try:
        start = time.time()

        result = build(files, options=opt)

        mypy_build_time = time.time() - start

    except CompileError as e:
        return [re.sub("^mypy: ", "refurb: ", msg) for msg in e.messages]

    errors: list[Error | str] = []
    checks = load_checks(settings)

    refurb_timing_stats_in_ms: dict[str, int] = {}

    for file in files:
        tree = result.graph[file.module].tree

        assert tree

        if settings.debug:
            errors.append(str(tree))

        start = time.time()

        visitor = RefurbVisitor(checks, settings)

        # See: https://github.com/dosisod/refurb/issues/302
        with suppress(RecursionError):
            visitor.accept(tree)

        elapsed = time.time() - start

        refurb_timing_stats_in_ms[file.module] = int(elapsed * 1_000)

        for error in visitor.errors:
            error.filename = file.path

        errors += visitor.errors

    output_timing_stats(
        settings,
        mypy_build_time,
        mypy_timing_stats,
        refurb_timing_stats_in_ms,
    )

    if mypy_timing_stats:
        mypy_timing_stats.unlink()

    return sorted(
        [error for error in errors if not should_ignore_error(error, settings)],
        key=partial(sort_errors, settings=settings),
    )


def sort_errors(error: Error | str, settings: Settings) -> tuple[str | int, ...]:
    if isinstance(error, str):
        return ("", error)

    if settings.sort_by == "error":
        return (
            error.prefix,
            error.code,
            error.filename or "",
            error.line,
            error.column,
        )

    return (
        error.filename or "",
        error.line,
        error.column,
        error.prefix,
        error.code,
    )


def format_as_github_annotation(error: Error | str) -> str:
    if isinstance(error, str):
        return f"::error title=Refurb Error::{error}"

    assert error.filename

    file = Path(error.filename).resolve().relative_to(Path.cwd())

    return "::error " + ",".join(
        [
            f"line={error.line}",
            f"col={error.column + 1}",
            f"title=Refurb {error.prefix}{error.code}",
            f"file={file}::{error.msg}",
        ]
    )


ERROR_DIFF_PATTERN = re.compile(r"`([^`]*)`([^`]*)`([^`]*)`")


def format_with_color(error: Error | str) -> str:
    if isinstance(error, str):
        return error

    blue = "\x1b[94m"
    yellow = "\x1b[33m"
    gray = "\x1b[90m"
    green = "\x1b[92m"
    red = "\x1b[91m"
    reset = "\x1b[0m"

    # Add red/green color for diffs, assuming the 2 pairs of backticks are in the form:
    # Replace `old` with `new`
    if error.msg.count("`") == 4:
        parts = [
            f"{gray}`{red}\\1{gray}`{reset}",
            "\\2",
            f"{gray}`{green}\\3{gray}`{reset}",
        ]

        error.msg = ERROR_DIFF_PATTERN.sub("".join(parts), error.msg)

    parts = [
        f"{blue}{error.filename}{reset}",
        f"{gray}:{error.line}:{error.column + 1}{reset}",
        " ",
        f"{yellow}[{error.prefix}{error.code}]{reset}",
        f"{gray}:{reset}",
        " ",
        error.msg,
    ]

    return "".join(parts)


def format_errors(errors: Sequence[Error | str], settings: Settings) -> str:
    if settings.format == "github":
        formatter: Callable[[Error | str], str] = format_as_github_annotation
    elif settings.color:
        formatter = format_with_color
    else:
        formatter = str

    done = "\n".join(formatter(error) for error in errors)

    if not settings.quiet and any(isinstance(err, Error) for err in errors):
        done += "\n\nRun `refurb --explain ERR` to further explain an error. Use `--quiet` to silence this message"

    return done


def output_timing_stats(
    settings: Settings,
    mypy_total_time_spent: float,
    mypy_timing_stats: Path | None,
    refurb_timing_stats_in_ms: dict[str, int],
) -> None:
    if not settings.timing_stats:
        return

    assert mypy_timing_stats

    mypy_stats: dict[str, int] = {}
    lines = mypy_timing_stats.read_text().splitlines()

    for line in lines:
        module, micro_seconds = line.split()

        mypy_stats[module] = int(micro_seconds) // 1_000

    data = {
        "mypy_total_time_spent_in_ms": int(mypy_total_time_spent * 1_000),
        "mypy_time_spent_parsing_modules_in_ms": dict(
            sorted(mypy_stats.items(), key=lambda x: x[1], reverse=True)
        ),
        "refurb_time_spent_checking_file_in_ms": dict(
            sorted(
                refurb_timing_stats_in_ms.items(),
                key=lambda x: x[1],
                reverse=True,
            )
        ),
    }

    settings.timing_stats.write_text(json.dumps(data, separators=(",", ":")))


def main(args: list[str]) -> int:
    try:
        settings = load_settings(args)

    except ValueError as e:
        print(e)
        return 1

    if settings.help:
        usage()

        return 0

    if settings.version:
        print(version())

        return 0

    if settings.generate:
        generate()

        return 0

    if settings.explain:
        print(explain(settings))

        return 0

    try:
        errors = run_refurb(settings)

    except TypeError as e:
        print(e)
        return 1

    if formatted_errors := format_errors(errors, settings):
        print(formatted_errors)

    return 1 if errors else 0