util.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. """Utility functions with no non-trivial dependencies."""
  2. from __future__ import annotations
  3. import hashlib
  4. import io
  5. import os
  6. import pathlib
  7. import re
  8. import shutil
  9. import sys
  10. import time
  11. from importlib import resources as importlib_resources
  12. from typing import IO, Callable, Container, Final, Iterable, Sequence, Sized, TypeVar
  13. from typing_extensions import Literal
  14. try:
  15. import curses
  16. import _curses # noqa: F401
  17. CURSES_ENABLED = True
  18. except ImportError:
  19. CURSES_ENABLED = False
  20. T = TypeVar("T")
  21. if sys.version_info >= (3, 9):
  22. TYPESHED_DIR: Final = str(importlib_resources.files("mypy") / "typeshed")
  23. else:
  24. with importlib_resources.path(
  25. "mypy", # mypy-c doesn't support __package__
  26. "py.typed", # a marker file for type information, we assume typeshed to live in the same dir
  27. ) as _resource:
  28. TYPESHED_DIR = str(_resource.parent / "typeshed")
  29. ENCODING_RE: Final = re.compile(rb"([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)")
  30. DEFAULT_SOURCE_OFFSET: Final = 4
  31. DEFAULT_COLUMNS: Final = 80
  32. # At least this number of columns will be shown on each side of
  33. # error location when printing source code snippet.
  34. MINIMUM_WIDTH: Final = 20
  35. # VT100 color code processing was added in Windows 10, but only the second major update,
  36. # Threshold 2. Fortunately, everyone (even on LTSB, Long Term Support Branch) should
  37. # have a version of Windows 10 newer than this. Note that Windows 8 and below are not
  38. # supported, but are either going out of support, or make up only a few % of the market.
  39. MINIMUM_WINDOWS_MAJOR_VT100: Final = 10
  40. MINIMUM_WINDOWS_BUILD_VT100: Final = 10586
  41. SPECIAL_DUNDERS: Final = frozenset(
  42. ("__init__", "__new__", "__call__", "__init_subclass__", "__class_getitem__")
  43. )
  44. def is_dunder(name: str, exclude_special: bool = False) -> bool:
  45. """Returns whether name is a dunder name.
  46. Args:
  47. exclude_special: Whether to return False for a couple special dunder methods.
  48. """
  49. if exclude_special and name in SPECIAL_DUNDERS:
  50. return False
  51. return name.startswith("__") and name.endswith("__")
  52. def is_sunder(name: str) -> bool:
  53. return not is_dunder(name) and name.startswith("_") and name.endswith("_")
  54. def split_module_names(mod_name: str) -> list[str]:
  55. """Return the module and all parent module names.
  56. So, if `mod_name` is 'a.b.c', this function will return
  57. ['a.b.c', 'a.b', and 'a'].
  58. """
  59. out = [mod_name]
  60. while "." in mod_name:
  61. mod_name = mod_name.rsplit(".", 1)[0]
  62. out.append(mod_name)
  63. return out
  64. def module_prefix(modules: Iterable[str], target: str) -> str | None:
  65. result = split_target(modules, target)
  66. if result is None:
  67. return None
  68. return result[0]
  69. def split_target(modules: Iterable[str], target: str) -> tuple[str, str] | None:
  70. remaining: list[str] = []
  71. while True:
  72. if target in modules:
  73. return target, ".".join(remaining)
  74. components = target.rsplit(".", 1)
  75. if len(components) == 1:
  76. return None
  77. target = components[0]
  78. remaining.insert(0, components[1])
  79. def short_type(obj: object) -> str:
  80. """Return the last component of the type name of an object.
  81. If obj is None, return 'nil'. For example, if obj is 1, return 'int'.
  82. """
  83. if obj is None:
  84. return "nil"
  85. t = str(type(obj))
  86. return t.split(".")[-1].rstrip("'>")
  87. def find_python_encoding(text: bytes) -> tuple[str, int]:
  88. """PEP-263 for detecting Python file encoding"""
  89. result = ENCODING_RE.match(text)
  90. if result:
  91. line = 2 if result.group(1) else 1
  92. encoding = result.group(3).decode("ascii")
  93. # Handle some aliases that Python is happy to accept and that are used in the wild.
  94. if encoding.startswith(("iso-latin-1-", "latin-1-")) or encoding == "iso-latin-1":
  95. encoding = "latin-1"
  96. return encoding, line
  97. else:
  98. default_encoding = "utf8"
  99. return default_encoding, -1
  100. def bytes_to_human_readable_repr(b: bytes) -> str:
  101. """Converts bytes into some human-readable representation. Unprintable
  102. bytes such as the nul byte are escaped. For example:
  103. >>> b = bytes([102, 111, 111, 10, 0])
  104. >>> s = bytes_to_human_readable_repr(b)
  105. >>> print(s)
  106. foo\n\x00
  107. >>> print(repr(s))
  108. 'foo\\n\\x00'
  109. """
  110. return repr(b)[2:-1]
  111. class DecodeError(Exception):
  112. """Exception raised when a file cannot be decoded due to an unknown encoding type.
  113. Essentially a wrapper for the LookupError raised by `bytearray.decode`
  114. """
  115. def decode_python_encoding(source: bytes) -> str:
  116. """Read the Python file with while obeying PEP-263 encoding detection.
  117. Returns the source as a string.
  118. """
  119. # check for BOM UTF-8 encoding and strip it out if present
  120. if source.startswith(b"\xef\xbb\xbf"):
  121. encoding = "utf8"
  122. source = source[3:]
  123. else:
  124. # look at first two lines and check if PEP-263 coding is present
  125. encoding, _ = find_python_encoding(source)
  126. try:
  127. source_text = source.decode(encoding)
  128. except LookupError as lookuperr:
  129. raise DecodeError(str(lookuperr)) from lookuperr
  130. return source_text
  131. def read_py_file(path: str, read: Callable[[str], bytes]) -> list[str] | None:
  132. """Try reading a Python file as list of source lines.
  133. Return None if something goes wrong.
  134. """
  135. try:
  136. source = read(path)
  137. except OSError:
  138. return None
  139. else:
  140. try:
  141. source_lines = decode_python_encoding(source).splitlines()
  142. except DecodeError:
  143. return None
  144. return source_lines
  145. def trim_source_line(line: str, max_len: int, col: int, min_width: int) -> tuple[str, int]:
  146. """Trim a line of source code to fit into max_len.
  147. Show 'min_width' characters on each side of 'col' (an error location). If either
  148. start or end is trimmed, this is indicated by adding '...' there.
  149. A typical result looks like this:
  150. ...some_variable = function_to_call(one_arg, other_arg) or...
  151. Return the trimmed string and the column offset to to adjust error location.
  152. """
  153. if max_len < 2 * min_width + 1:
  154. # In case the window is too tiny it is better to still show something.
  155. max_len = 2 * min_width + 1
  156. # Trivial case: line already fits in.
  157. if len(line) <= max_len:
  158. return line, 0
  159. # If column is not too large so that there is still min_width after it,
  160. # the line doesn't need to be trimmed at the start.
  161. if col + min_width < max_len:
  162. return line[:max_len] + "...", 0
  163. # Otherwise, if the column is not too close to the end, trim both sides.
  164. if col < len(line) - min_width - 1:
  165. offset = col - max_len + min_width + 1
  166. return "..." + line[offset : col + min_width + 1] + "...", offset - 3
  167. # Finally, if the column is near the end, just trim the start.
  168. return "..." + line[-max_len:], len(line) - max_len - 3
  169. def get_mypy_comments(source: str) -> list[tuple[int, str]]:
  170. PREFIX = "# mypy: "
  171. # Don't bother splitting up the lines unless we know it is useful
  172. if PREFIX not in source:
  173. return []
  174. lines = source.split("\n")
  175. results = []
  176. for i, line in enumerate(lines):
  177. if line.startswith(PREFIX):
  178. results.append((i + 1, line[len(PREFIX) :]))
  179. return results
  180. PASS_TEMPLATE: Final = """<?xml version="1.0" encoding="utf-8"?>
  181. <testsuite errors="0" failures="0" name="mypy" skips="0" tests="1" time="{time:.3f}">
  182. <testcase classname="mypy" file="mypy" line="1" name="mypy-py{ver}-{platform}" time="{time:.3f}">
  183. </testcase>
  184. </testsuite>
  185. """
  186. FAIL_TEMPLATE: Final = """<?xml version="1.0" encoding="utf-8"?>
  187. <testsuite errors="0" failures="1" name="mypy" skips="0" tests="1" time="{time:.3f}">
  188. <testcase classname="mypy" file="mypy" line="1" name="mypy-py{ver}-{platform}" time="{time:.3f}">
  189. <failure message="mypy produced messages">{text}</failure>
  190. </testcase>
  191. </testsuite>
  192. """
  193. ERROR_TEMPLATE: Final = """<?xml version="1.0" encoding="utf-8"?>
  194. <testsuite errors="1" failures="0" name="mypy" skips="0" tests="1" time="{time:.3f}">
  195. <testcase classname="mypy" file="mypy" line="1" name="mypy-py{ver}-{platform}" time="{time:.3f}">
  196. <error message="mypy produced errors">{text}</error>
  197. </testcase>
  198. </testsuite>
  199. """
  200. def write_junit_xml(
  201. dt: float, serious: bool, messages: list[str], path: str, version: str, platform: str
  202. ) -> None:
  203. from xml.sax.saxutils import escape
  204. if not messages and not serious:
  205. xml = PASS_TEMPLATE.format(time=dt, ver=version, platform=platform)
  206. elif not serious:
  207. xml = FAIL_TEMPLATE.format(
  208. text=escape("\n".join(messages)), time=dt, ver=version, platform=platform
  209. )
  210. else:
  211. xml = ERROR_TEMPLATE.format(
  212. text=escape("\n".join(messages)), time=dt, ver=version, platform=platform
  213. )
  214. # checks for a directory structure in path and creates folders if needed
  215. xml_dirs = os.path.dirname(os.path.abspath(path))
  216. if not os.path.isdir(xml_dirs):
  217. os.makedirs(xml_dirs)
  218. with open(path, "wb") as f:
  219. f.write(xml.encode("utf-8"))
  220. class IdMapper:
  221. """Generate integer ids for objects.
  222. Unlike id(), these start from 0 and increment by 1, and ids won't
  223. get reused across the life-time of IdMapper.
  224. Assume objects don't redefine __eq__ or __hash__.
  225. """
  226. def __init__(self) -> None:
  227. self.id_map: dict[object, int] = {}
  228. self.next_id = 0
  229. def id(self, o: object) -> int:
  230. if o not in self.id_map:
  231. self.id_map[o] = self.next_id
  232. self.next_id += 1
  233. return self.id_map[o]
  234. def get_prefix(fullname: str) -> str:
  235. """Drop the final component of a qualified name (e.g. ('x.y' -> 'x')."""
  236. return fullname.rsplit(".", 1)[0]
  237. def get_top_two_prefixes(fullname: str) -> tuple[str, str]:
  238. """Return one and two component prefixes of a fully qualified name.
  239. Given 'a.b.c.d', return ('a', 'a.b').
  240. If fullname has only one component, return (fullname, fullname).
  241. """
  242. components = fullname.split(".", 3)
  243. return components[0], ".".join(components[:2])
  244. def correct_relative_import(
  245. cur_mod_id: str, relative: int, target: str, is_cur_package_init_file: bool
  246. ) -> tuple[str, bool]:
  247. if relative == 0:
  248. return target, True
  249. parts = cur_mod_id.split(".")
  250. rel = relative
  251. if is_cur_package_init_file:
  252. rel -= 1
  253. ok = len(parts) >= rel
  254. if rel != 0:
  255. cur_mod_id = ".".join(parts[:-rel])
  256. return cur_mod_id + (("." + target) if target else ""), ok
  257. fields_cache: Final[dict[type[object], list[str]]] = {}
  258. def get_class_descriptors(cls: type[object]) -> Sequence[str]:
  259. import inspect # Lazy import for minor startup speed win
  260. # Maintain a cache of type -> attributes defined by descriptors in the class
  261. # (that is, attributes from __slots__ and C extension classes)
  262. if cls not in fields_cache:
  263. members = inspect.getmembers(
  264. cls, lambda o: inspect.isgetsetdescriptor(o) or inspect.ismemberdescriptor(o)
  265. )
  266. fields_cache[cls] = [x for x, y in members if x != "__weakref__" and x != "__dict__"]
  267. return fields_cache[cls]
  268. def replace_object_state(
  269. new: object, old: object, copy_dict: bool = False, skip_slots: tuple[str, ...] = ()
  270. ) -> None:
  271. """Copy state of old node to the new node.
  272. This handles cases where there is __dict__ and/or attribute descriptors
  273. (either from slots or because the type is defined in a C extension module).
  274. Assume that both objects have the same __class__.
  275. """
  276. if hasattr(old, "__dict__"):
  277. if copy_dict:
  278. new.__dict__ = dict(old.__dict__)
  279. else:
  280. new.__dict__ = old.__dict__
  281. for attr in get_class_descriptors(old.__class__):
  282. if attr in skip_slots:
  283. continue
  284. try:
  285. if hasattr(old, attr):
  286. setattr(new, attr, getattr(old, attr))
  287. elif hasattr(new, attr):
  288. delattr(new, attr)
  289. # There is no way to distinguish getsetdescriptors that allow
  290. # writes from ones that don't (I think?), so we just ignore
  291. # AttributeErrors if we need to.
  292. # TODO: What about getsetdescriptors that act like properties???
  293. except AttributeError:
  294. pass
  295. def is_sub_path(path1: str, path2: str) -> bool:
  296. """Given two paths, return if path1 is a sub-path of path2."""
  297. return pathlib.Path(path2) in pathlib.Path(path1).parents
  298. def hard_exit(status: int = 0) -> None:
  299. """Kill the current process without fully cleaning up.
  300. This can be quite a bit faster than a normal exit() since objects are not freed.
  301. """
  302. sys.stdout.flush()
  303. sys.stderr.flush()
  304. os._exit(status)
  305. def unmangle(name: str) -> str:
  306. """Remove internal suffixes from a short name."""
  307. return name.rstrip("'")
  308. def get_unique_redefinition_name(name: str, existing: Container[str]) -> str:
  309. """Get a simple redefinition name not present among existing.
  310. For example, for name 'foo' we try 'foo-redefinition', 'foo-redefinition2',
  311. 'foo-redefinition3', etc. until we find one that is not in existing.
  312. """
  313. r_name = name + "-redefinition"
  314. if r_name not in existing:
  315. return r_name
  316. i = 2
  317. while r_name + str(i) in existing:
  318. i += 1
  319. return r_name + str(i)
  320. def check_python_version(program: str) -> None:
  321. """Report issues with the Python used to run mypy, dmypy, or stubgen"""
  322. # Check for known bad Python versions.
  323. if sys.version_info[:2] < (3, 8):
  324. sys.exit(
  325. "Running {name} with Python 3.7 or lower is not supported; "
  326. "please upgrade to 3.8 or newer".format(name=program)
  327. )
  328. def count_stats(messages: list[str]) -> tuple[int, int, int]:
  329. """Count total number of errors, notes and error_files in message list."""
  330. errors = [e for e in messages if ": error:" in e]
  331. error_files = {e.split(":")[0] for e in errors}
  332. notes = [e for e in messages if ": note:" in e]
  333. return len(errors), len(notes), len(error_files)
  334. def split_words(msg: str) -> list[str]:
  335. """Split line of text into words (but not within quoted groups)."""
  336. next_word = ""
  337. res: list[str] = []
  338. allow_break = True
  339. for c in msg:
  340. if c == " " and allow_break:
  341. res.append(next_word)
  342. next_word = ""
  343. continue
  344. if c == '"':
  345. allow_break = not allow_break
  346. next_word += c
  347. res.append(next_word)
  348. return res
  349. def get_terminal_width() -> int:
  350. """Get current terminal width if possible, otherwise return the default one."""
  351. return (
  352. int(os.getenv("MYPY_FORCE_TERMINAL_WIDTH", "0"))
  353. or shutil.get_terminal_size().columns
  354. or DEFAULT_COLUMNS
  355. )
  356. def soft_wrap(msg: str, max_len: int, first_offset: int, num_indent: int = 0) -> str:
  357. """Wrap a long error message into few lines.
  358. Breaks will only happen between words, and never inside a quoted group
  359. (to avoid breaking types such as "Union[int, str]"). The 'first_offset' is
  360. the width before the start of first line.
  361. Pad every next line with 'num_indent' spaces. Every line will be at most 'max_len'
  362. characters, except if it is a single word or quoted group.
  363. For example:
  364. first_offset
  365. ------------------------
  366. path/to/file: error: 58: Some very long error message
  367. that needs to be split in separate lines.
  368. "Long[Type, Names]" are never split.
  369. ^^^^--------------------------------------------------
  370. num_indent max_len
  371. """
  372. words = split_words(msg)
  373. next_line = words.pop(0)
  374. lines: list[str] = []
  375. while words:
  376. next_word = words.pop(0)
  377. max_line_len = max_len - num_indent if lines else max_len - first_offset
  378. # Add 1 to account for space between words.
  379. if len(next_line) + len(next_word) + 1 <= max_line_len:
  380. next_line += " " + next_word
  381. else:
  382. lines.append(next_line)
  383. next_line = next_word
  384. lines.append(next_line)
  385. padding = "\n" + " " * num_indent
  386. return padding.join(lines)
  387. def hash_digest(data: bytes) -> str:
  388. """Compute a hash digest of some data.
  389. We use a cryptographic hash because we want a low probability of
  390. accidental collision, but we don't really care about any of the
  391. cryptographic properties.
  392. """
  393. # Once we drop Python 3.5 support, we should consider using
  394. # blake2b, which is faster.
  395. return hashlib.sha256(data).hexdigest()
  396. def parse_gray_color(cup: bytes) -> str:
  397. """Reproduce a gray color in ANSI escape sequence"""
  398. if sys.platform == "win32":
  399. assert False, "curses is not available on Windows"
  400. set_color = "".join([cup[:-1].decode(), "m"])
  401. gray = curses.tparm(set_color.encode("utf-8"), 1, 9).decode()
  402. return gray
  403. def should_force_color() -> bool:
  404. env_var = os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0"))
  405. try:
  406. return bool(int(env_var))
  407. except ValueError:
  408. return bool(env_var)
  409. class FancyFormatter:
  410. """Apply color and bold font to terminal output.
  411. This currently only works on Linux and Mac.
  412. """
  413. def __init__(self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool) -> None:
  414. self.hide_error_codes = hide_error_codes
  415. # Check if we are in a human-facing terminal on a supported platform.
  416. if sys.platform not in ("linux", "darwin", "win32", "emscripten"):
  417. self.dummy_term = True
  418. return
  419. if not should_force_color() and (not f_out.isatty() or not f_err.isatty()):
  420. self.dummy_term = True
  421. return
  422. if sys.platform == "win32":
  423. self.dummy_term = not self.initialize_win_colors()
  424. elif sys.platform == "emscripten":
  425. self.dummy_term = not self.initialize_vt100_colors()
  426. else:
  427. self.dummy_term = not self.initialize_unix_colors()
  428. if not self.dummy_term:
  429. self.colors = {
  430. "red": self.RED,
  431. "green": self.GREEN,
  432. "blue": self.BLUE,
  433. "yellow": self.YELLOW,
  434. "none": "",
  435. }
  436. def initialize_vt100_colors(self) -> bool:
  437. """Return True if initialization was successful and we can use colors, False otherwise"""
  438. # Windows and Emscripten can both use ANSI/VT100 escape sequences for color
  439. assert sys.platform in ("win32", "emscripten")
  440. self.BOLD = "\033[1m"
  441. self.UNDER = "\033[4m"
  442. self.BLUE = "\033[94m"
  443. self.GREEN = "\033[92m"
  444. self.RED = "\033[91m"
  445. self.YELLOW = "\033[93m"
  446. self.NORMAL = "\033[0m"
  447. self.DIM = "\033[2m"
  448. return True
  449. def initialize_win_colors(self) -> bool:
  450. """Return True if initialization was successful and we can use colors, False otherwise"""
  451. # Windows ANSI escape sequences are only supported on Threshold 2 and above.
  452. # we check with an assert at runtime and an if check for mypy, as asserts do not
  453. # yet narrow platform
  454. assert sys.platform == "win32"
  455. if sys.platform == "win32":
  456. winver = sys.getwindowsversion()
  457. if (
  458. winver.major < MINIMUM_WINDOWS_MAJOR_VT100
  459. or winver.build < MINIMUM_WINDOWS_BUILD_VT100
  460. ):
  461. return False
  462. import ctypes
  463. kernel32 = ctypes.windll.kernel32
  464. ENABLE_PROCESSED_OUTPUT = 0x1
  465. ENABLE_WRAP_AT_EOL_OUTPUT = 0x2
  466. ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
  467. STD_OUTPUT_HANDLE = -11
  468. kernel32.SetConsoleMode(
  469. kernel32.GetStdHandle(STD_OUTPUT_HANDLE),
  470. ENABLE_PROCESSED_OUTPUT
  471. | ENABLE_WRAP_AT_EOL_OUTPUT
  472. | ENABLE_VIRTUAL_TERMINAL_PROCESSING,
  473. )
  474. self.initialize_vt100_colors()
  475. return True
  476. return False
  477. def initialize_unix_colors(self) -> bool:
  478. """Return True if initialization was successful and we can use colors, False otherwise"""
  479. if sys.platform == "win32" or not CURSES_ENABLED:
  480. return False
  481. try:
  482. # setupterm wants a fd to potentially write an "initialization sequence".
  483. # We override sys.stdout for the daemon API so if stdout doesn't have an fd,
  484. # just give it /dev/null.
  485. try:
  486. fd = sys.stdout.fileno()
  487. except io.UnsupportedOperation:
  488. with open("/dev/null", "rb") as f:
  489. curses.setupterm(fd=f.fileno())
  490. else:
  491. curses.setupterm(fd=fd)
  492. except curses.error:
  493. # Most likely terminfo not found.
  494. return False
  495. bold = curses.tigetstr("bold")
  496. under = curses.tigetstr("smul")
  497. set_color = curses.tigetstr("setaf")
  498. set_eseq = curses.tigetstr("cup")
  499. normal = curses.tigetstr("sgr0")
  500. if not (bold and under and set_color and set_eseq and normal):
  501. return False
  502. self.NORMAL = normal.decode()
  503. self.BOLD = bold.decode()
  504. self.UNDER = under.decode()
  505. self.DIM = parse_gray_color(set_eseq)
  506. self.BLUE = curses.tparm(set_color, curses.COLOR_BLUE).decode()
  507. self.GREEN = curses.tparm(set_color, curses.COLOR_GREEN).decode()
  508. self.RED = curses.tparm(set_color, curses.COLOR_RED).decode()
  509. self.YELLOW = curses.tparm(set_color, curses.COLOR_YELLOW).decode()
  510. return True
  511. def style(
  512. self,
  513. text: str,
  514. color: Literal["red", "green", "blue", "yellow", "none"],
  515. bold: bool = False,
  516. underline: bool = False,
  517. dim: bool = False,
  518. ) -> str:
  519. """Apply simple color and style (underlined or bold)."""
  520. if self.dummy_term:
  521. return text
  522. if bold:
  523. start = self.BOLD
  524. else:
  525. start = ""
  526. if underline:
  527. start += self.UNDER
  528. if dim:
  529. start += self.DIM
  530. return start + self.colors[color] + text + self.NORMAL
  531. def fit_in_terminal(
  532. self, messages: list[str], fixed_terminal_width: int | None = None
  533. ) -> list[str]:
  534. """Improve readability by wrapping error messages and trimming source code."""
  535. width = fixed_terminal_width or get_terminal_width()
  536. new_messages = messages.copy()
  537. for i, error in enumerate(messages):
  538. if ": error:" in error:
  539. loc, msg = error.split("error:", maxsplit=1)
  540. msg = soft_wrap(msg, width, first_offset=len(loc) + len("error: "))
  541. new_messages[i] = loc + "error:" + msg
  542. if error.startswith(" " * DEFAULT_SOURCE_OFFSET) and "^" not in error:
  543. # TODO: detecting source code highlights through an indent can be surprising.
  544. # Restore original error message and error location.
  545. error = error[DEFAULT_SOURCE_OFFSET:]
  546. marker_line = messages[i + 1]
  547. marker_column = marker_line.index("^")
  548. column = marker_column - DEFAULT_SOURCE_OFFSET
  549. if "~" not in marker_line:
  550. marker = "^"
  551. else:
  552. # +1 because both ends are included
  553. marker = marker_line[marker_column : marker_line.rindex("~") + 1]
  554. # Let source have some space also on the right side, plus 6
  555. # to accommodate ... on each side.
  556. max_len = width - DEFAULT_SOURCE_OFFSET - 6
  557. source_line, offset = trim_source_line(error, max_len, column, MINIMUM_WIDTH)
  558. new_messages[i] = " " * DEFAULT_SOURCE_OFFSET + source_line
  559. # Also adjust the error marker position and trim error marker is needed.
  560. new_marker_line = " " * (DEFAULT_SOURCE_OFFSET + column - offset) + marker
  561. if len(new_marker_line) > len(new_messages[i]) and len(marker) > 3:
  562. new_marker_line = new_marker_line[: len(new_messages[i]) - 3] + "..."
  563. new_messages[i + 1] = new_marker_line
  564. return new_messages
  565. def colorize(self, error: str) -> str:
  566. """Colorize an output line by highlighting the status and error code."""
  567. if ": error:" in error:
  568. loc, msg = error.split("error:", maxsplit=1)
  569. if self.hide_error_codes:
  570. return (
  571. loc + self.style("error:", "red", bold=True) + self.highlight_quote_groups(msg)
  572. )
  573. codepos = msg.rfind("[")
  574. if codepos != -1:
  575. code = msg[codepos:]
  576. msg = msg[:codepos]
  577. else:
  578. code = "" # no error code specified
  579. return (
  580. loc
  581. + self.style("error:", "red", bold=True)
  582. + self.highlight_quote_groups(msg)
  583. + self.style(code, "yellow")
  584. )
  585. elif ": note:" in error:
  586. loc, msg = error.split("note:", maxsplit=1)
  587. formatted = self.highlight_quote_groups(self.underline_link(msg))
  588. return loc + self.style("note:", "blue") + formatted
  589. elif error.startswith(" " * DEFAULT_SOURCE_OFFSET):
  590. # TODO: detecting source code highlights through an indent can be surprising.
  591. if "^" not in error:
  592. return self.style(error, "none", dim=True)
  593. return self.style(error, "red")
  594. else:
  595. return error
  596. def highlight_quote_groups(self, msg: str) -> str:
  597. """Make groups quoted with double quotes bold (including quotes).
  598. This is used to highlight types, attribute names etc.
  599. """
  600. if msg.count('"') % 2:
  601. # Broken error message, don't do any formatting.
  602. return msg
  603. parts = msg.split('"')
  604. out = ""
  605. for i, part in enumerate(parts):
  606. if i % 2 == 0:
  607. out += self.style(part, "none")
  608. else:
  609. out += self.style('"' + part + '"', "none", bold=True)
  610. return out
  611. def underline_link(self, note: str) -> str:
  612. """Underline a link in a note message (if any).
  613. This assumes there is at most one link in the message.
  614. """
  615. match = re.search(r"https?://\S*", note)
  616. if not match:
  617. return note
  618. start = match.start()
  619. end = match.end()
  620. return note[:start] + self.style(note[start:end], "none", underline=True) + note[end:]
  621. def format_success(self, n_sources: int, use_color: bool = True) -> str:
  622. """Format short summary in case of success.
  623. n_sources is total number of files passed directly on command line,
  624. i.e. excluding stubs and followed imports.
  625. """
  626. msg = f"Success: no issues found in {n_sources} source file{plural_s(n_sources)}"
  627. if not use_color:
  628. return msg
  629. return self.style(msg, "green", bold=True)
  630. def format_error(
  631. self,
  632. n_errors: int,
  633. n_files: int,
  634. n_sources: int,
  635. *,
  636. blockers: bool = False,
  637. use_color: bool = True,
  638. ) -> str:
  639. """Format a short summary in case of errors."""
  640. msg = f"Found {n_errors} error{plural_s(n_errors)} in {n_files} file{plural_s(n_files)}"
  641. if blockers:
  642. msg += " (errors prevented further checking)"
  643. else:
  644. msg += f" (checked {n_sources} source file{plural_s(n_sources)})"
  645. if not use_color:
  646. return msg
  647. return self.style(msg, "red", bold=True)
  648. def is_typeshed_file(typeshed_dir: str | None, file: str) -> bool:
  649. typeshed_dir = typeshed_dir if typeshed_dir is not None else TYPESHED_DIR
  650. try:
  651. return os.path.commonpath((typeshed_dir, os.path.abspath(file))) == typeshed_dir
  652. except ValueError: # Different drives on Windows
  653. return False
  654. def is_stub_package_file(file: str) -> bool:
  655. # Use hacky heuristics to check whether file is part of a PEP 561 stub package.
  656. if not file.endswith(".pyi"):
  657. return False
  658. return any(component.endswith("-stubs") for component in os.path.split(os.path.abspath(file)))
  659. def unnamed_function(name: str | None) -> bool:
  660. return name is not None and name == "_"
  661. time_ref = time.perf_counter_ns
  662. def time_spent_us(t0: int) -> int:
  663. return int((time.perf_counter_ns() - t0) / 1000)
  664. def plural_s(s: int | Sized) -> str:
  665. count = s if isinstance(s, int) else len(s)
  666. if count != 1:
  667. return "s"
  668. else:
  669. return ""