Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/black/report.py: 39%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Summarize Black runs to users.
3"""
5from dataclasses import dataclass
6from enum import Enum
7from pathlib import Path
9from black.output import err, out, style_output
10from black.parsing import InvalidInput
13class Changed(Enum):
14 NO = 0
15 CACHED = 1
16 YES = 2
19class NothingChanged(UserWarning):
20 """Raised when reformatted code is the same as source."""
23@dataclass
24class Report:
25 """Provides a reformatting counter. Can be rendered with `str(report)`."""
27 check: bool = False
28 diff: bool = False
29 quiet: bool = False
30 verbose: bool = False
31 change_count: int = 0
32 same_count: int = 0
33 failure_count: int = 0
35 def done(self, src: Path, changed: Changed) -> None:
36 """Increment the counter for successful reformatting. Write out a message."""
37 if changed is Changed.YES:
38 reformatted = "would reformat" if self.check or self.diff else "reformatted"
39 if self.verbose or not self.quiet:
40 out(f"{reformatted} {src}")
41 self.change_count += 1
42 else:
43 if self.verbose:
44 if changed is Changed.NO:
45 msg = f"{src} already well formatted, good job."
46 else:
47 msg = f"{src} wasn't modified on disk since last run."
48 out(msg, bold=False)
49 self.same_count += 1
51 def failed(self, src: Path, message: BaseException) -> None:
52 """Increment the counter for failed reformatting. Write out a message."""
53 if (
54 isinstance(message, InvalidInput)
55 and message.lineno is not None
56 and message.column is not None
57 and message.context
58 ):
59 details = message.details or ""
60 err(
61 f"error: {message.context}: {src}:{message.lineno}:"
62 f"{message.column}{details}"
63 )
64 else:
65 err(f"error: cannot format {src}: {message}")
66 self.failure_count += 1
68 def path_ignored(self, path: Path, message: str) -> None:
69 if self.verbose:
70 out(f"{path} ignored: {message}", bold=False)
72 @property
73 def return_code(self) -> int:
74 """Return the exit code that the app should use.
76 This considers the current state of changed files and failures:
77 - if there were any failures, return 123;
78 - if any files were changed and --check is being used, return 1;
79 - otherwise return 0.
80 """
81 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
82 # 126 we have special return codes reserved by the shell.
83 if self.failure_count:
84 return 123
86 elif self.change_count and self.check:
87 return 1
89 return 0
91 def __str__(self) -> str:
92 """Render a color report of the current state.
94 Use `click.unstyle` to remove colors.
95 """
96 if self.check or self.diff:
97 reformatted = "would be reformatted"
98 unchanged = "would be left unchanged"
99 failed = "would fail to reformat"
100 else:
101 reformatted = "reformatted"
102 unchanged = "left unchanged"
103 failed = "failed to reformat"
104 report = []
105 if self.change_count:
106 s = "s" if self.change_count > 1 else ""
107 report.append(
108 style_output(f"{self.change_count} file{s} ", bold=True, fg="blue")
109 + style_output(f"{reformatted}", bold=True)
110 )
112 if self.same_count:
113 s = "s" if self.same_count > 1 else ""
114 report.append(
115 style_output(f"{self.same_count} file{s} ", fg="blue") + unchanged
116 )
117 if self.failure_count:
118 s = "s" if self.failure_count > 1 else ""
119 report.append(
120 style_output(f"{self.failure_count} file{s} {failed}", fg="red")
121 )
122 return ", ".join(report) + "."