coverage 7.11.3__cp314-cp314-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of coverage might be problematic. Click here for more details.

Files changed (59) hide show
  1. coverage/__init__.py +40 -0
  2. coverage/__main__.py +12 -0
  3. coverage/annotate.py +114 -0
  4. coverage/bytecode.py +196 -0
  5. coverage/cmdline.py +1184 -0
  6. coverage/collector.py +486 -0
  7. coverage/config.py +731 -0
  8. coverage/context.py +74 -0
  9. coverage/control.py +1481 -0
  10. coverage/core.py +139 -0
  11. coverage/data.py +227 -0
  12. coverage/debug.py +669 -0
  13. coverage/disposition.py +59 -0
  14. coverage/env.py +135 -0
  15. coverage/exceptions.py +85 -0
  16. coverage/execfile.py +329 -0
  17. coverage/files.py +553 -0
  18. coverage/html.py +856 -0
  19. coverage/htmlfiles/coverage_html.js +733 -0
  20. coverage/htmlfiles/favicon_32.png +0 -0
  21. coverage/htmlfiles/index.html +164 -0
  22. coverage/htmlfiles/keybd_closed.png +0 -0
  23. coverage/htmlfiles/pyfile.html +149 -0
  24. coverage/htmlfiles/style.css +377 -0
  25. coverage/htmlfiles/style.scss +824 -0
  26. coverage/inorout.py +614 -0
  27. coverage/jsonreport.py +188 -0
  28. coverage/lcovreport.py +219 -0
  29. coverage/misc.py +373 -0
  30. coverage/multiproc.py +120 -0
  31. coverage/numbits.py +146 -0
  32. coverage/parser.py +1213 -0
  33. coverage/patch.py +166 -0
  34. coverage/phystokens.py +197 -0
  35. coverage/plugin.py +617 -0
  36. coverage/plugin_support.py +299 -0
  37. coverage/py.typed +1 -0
  38. coverage/python.py +269 -0
  39. coverage/pytracer.py +369 -0
  40. coverage/regions.py +127 -0
  41. coverage/report.py +298 -0
  42. coverage/report_core.py +117 -0
  43. coverage/results.py +471 -0
  44. coverage/sqldata.py +1153 -0
  45. coverage/sqlitedb.py +239 -0
  46. coverage/sysmon.py +482 -0
  47. coverage/templite.py +306 -0
  48. coverage/tomlconfig.py +210 -0
  49. coverage/tracer.cpython-314-darwin.so +0 -0
  50. coverage/tracer.pyi +43 -0
  51. coverage/types.py +206 -0
  52. coverage/version.py +35 -0
  53. coverage/xmlreport.py +264 -0
  54. coverage-7.11.3.dist-info/METADATA +221 -0
  55. coverage-7.11.3.dist-info/RECORD +59 -0
  56. coverage-7.11.3.dist-info/WHEEL +6 -0
  57. coverage-7.11.3.dist-info/entry_points.txt +4 -0
  58. coverage-7.11.3.dist-info/licenses/LICENSE.txt +177 -0
  59. coverage-7.11.3.dist-info/top_level.txt +1 -0
coverage/results.py ADDED
@@ -0,0 +1,471 @@
1
+ # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2
+ # For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
3
+
4
+ """Results of coverage measurement."""
5
+
6
+ from __future__ import annotations
7
+
8
+ import collections
9
+ import dataclasses
10
+ from collections.abc import Iterable
11
+ from typing import TYPE_CHECKING
12
+
13
+ from coverage.exceptions import ConfigError
14
+ from coverage.misc import nice_pair
15
+ from coverage.types import TArc, TLineNo
16
+
17
+ if TYPE_CHECKING:
18
+ from coverage.data import CoverageData
19
+ from coverage.plugin import FileReporter
20
+
21
+
22
+ def analysis_from_file_reporter(
23
+ data: CoverageData,
24
+ precision: int,
25
+ file_reporter: FileReporter,
26
+ filename: str,
27
+ ) -> Analysis:
28
+ """Create an Analysis from a FileReporter."""
29
+ has_arcs = data.has_arcs()
30
+ statements = file_reporter.lines()
31
+ excluded = file_reporter.excluded_lines()
32
+ executed = file_reporter.translate_lines(data.lines(filename) or [])
33
+
34
+ if has_arcs:
35
+ arc_possibilities_set = file_reporter.arcs()
36
+ arcs: Iterable[TArc] = data.arcs(filename) or []
37
+ arcs = file_reporter.translate_arcs(arcs)
38
+
39
+ # Reduce the set of arcs to the ones that could be branches.
40
+ dests = collections.defaultdict(set)
41
+ for fromno, tono in arc_possibilities_set:
42
+ dests[fromno].add(tono)
43
+ single_dests = {
44
+ fromno: list(tonos)[0] for fromno, tonos in dests.items() if len(tonos) == 1
45
+ }
46
+ new_arcs = set()
47
+ for fromno, tono in arcs:
48
+ if fromno != tono:
49
+ new_arcs.add((fromno, tono))
50
+ else:
51
+ if fromno in single_dests:
52
+ new_arcs.add((fromno, single_dests[fromno]))
53
+
54
+ arcs_executed_set = file_reporter.translate_arcs(new_arcs)
55
+ exit_counts = file_reporter.exit_counts()
56
+ no_branch = file_reporter.no_branch_lines()
57
+ else:
58
+ arc_possibilities_set = set()
59
+ arcs_executed_set = set()
60
+ exit_counts = {}
61
+ no_branch = set()
62
+
63
+ return Analysis(
64
+ precision=precision,
65
+ filename=filename,
66
+ has_arcs=has_arcs,
67
+ statements=statements,
68
+ excluded=excluded,
69
+ executed=executed,
70
+ arc_possibilities_set=arc_possibilities_set,
71
+ arcs_executed_set=arcs_executed_set,
72
+ exit_counts=exit_counts,
73
+ no_branch=no_branch,
74
+ )
75
+
76
+
77
+ @dataclasses.dataclass
78
+ class Analysis:
79
+ """The results of analyzing a FileReporter."""
80
+
81
+ precision: int
82
+ filename: str
83
+ has_arcs: bool
84
+ statements: set[TLineNo]
85
+ excluded: set[TLineNo]
86
+ executed: set[TLineNo]
87
+ arc_possibilities_set: set[TArc]
88
+ arcs_executed_set: set[TArc]
89
+ exit_counts: dict[TLineNo, int]
90
+ no_branch: set[TLineNo]
91
+
92
+ def __post_init__(self) -> None:
93
+ self.arc_possibilities = sorted(self.arc_possibilities_set)
94
+ self.arcs_executed = sorted(self.arcs_executed_set)
95
+ self.missing = self.statements - self.executed
96
+
97
+ if self.has_arcs:
98
+ n_branches = self._total_branches()
99
+ mba = self.missing_branch_arcs()
100
+ n_partial_branches = sum(len(v) for k, v in mba.items() if k not in self.missing)
101
+ n_missing_branches = sum(len(v) for k, v in mba.items())
102
+ else:
103
+ n_branches = n_partial_branches = n_missing_branches = 0
104
+
105
+ self.numbers = Numbers(
106
+ precision=self.precision,
107
+ n_files=1,
108
+ n_statements=len(self.statements),
109
+ n_excluded=len(self.excluded),
110
+ n_missing=len(self.missing),
111
+ n_branches=n_branches,
112
+ n_partial_branches=n_partial_branches,
113
+ n_missing_branches=n_missing_branches,
114
+ )
115
+
116
+ def missing_formatted(self, branches: bool = False) -> str:
117
+ """The missing line numbers, formatted nicely.
118
+
119
+ Returns a string like "1-2, 5-11, 13-14".
120
+
121
+ If `branches` is true, includes the missing branch arcs also.
122
+
123
+ """
124
+ if branches and self.has_arcs:
125
+ arcs = self.missing_branch_arcs().items()
126
+ else:
127
+ arcs = None
128
+
129
+ return format_lines(self.statements, self.missing, arcs=arcs)
130
+
131
+ def arcs_missing(self) -> list[TArc]:
132
+ """Returns a sorted list of the un-executed arcs in the code."""
133
+ missing = (
134
+ p
135
+ for p in self.arc_possibilities
136
+ if p not in self.arcs_executed_set
137
+ and p[0] not in self.no_branch
138
+ and p[1] not in self.excluded
139
+ )
140
+ return sorted(missing)
141
+
142
+ def _branch_lines(self) -> list[TLineNo]:
143
+ """Returns a list of line numbers that have more than one exit."""
144
+ return [l1 for l1, count in self.exit_counts.items() if count > 1]
145
+
146
+ def _total_branches(self) -> int:
147
+ """How many total branches are there?"""
148
+ return sum(count for count in self.exit_counts.values() if count > 1)
149
+
150
+ def missing_branch_arcs(self) -> dict[TLineNo, list[TLineNo]]:
151
+ """Return arcs that weren't executed from branch lines.
152
+
153
+ Returns {l1:[l2a,l2b,...], ...}
154
+
155
+ """
156
+ missing = self.arcs_missing()
157
+ branch_lines = set(self._branch_lines())
158
+ mba = collections.defaultdict(list)
159
+ for l1, l2 in missing:
160
+ assert l1 != l2, f"In {self.filename}, didn't expect {l1} == {l2}"
161
+ if l1 in branch_lines:
162
+ mba[l1].append(l2)
163
+ return mba
164
+
165
+ def executed_branch_arcs(self) -> dict[TLineNo, list[TLineNo]]:
166
+ """Return arcs that were executed from branch lines.
167
+
168
+ Only include ones that we considered possible.
169
+
170
+ Returns {l1:[l2a,l2b,...], ...}
171
+
172
+ """
173
+ branch_lines = set(self._branch_lines())
174
+ eba = collections.defaultdict(list)
175
+ for l1, l2 in self.arcs_executed:
176
+ assert l1 != l2, f"Oops: Didn't think this could happen: {l1 = }, {l2 = }"
177
+ if (l1, l2) not in self.arc_possibilities_set:
178
+ continue
179
+ if l1 in branch_lines:
180
+ eba[l1].append(l2)
181
+ return eba
182
+
183
+ def branch_stats(self) -> dict[TLineNo, tuple[int, int]]:
184
+ """Get stats about branches.
185
+
186
+ Returns a dict mapping line numbers to a tuple:
187
+ (total_exits, taken_exits).
188
+
189
+ """
190
+
191
+ missing_arcs = self.missing_branch_arcs()
192
+ stats = {}
193
+ for lnum in self._branch_lines():
194
+ exits = self.exit_counts[lnum]
195
+ missing = len(missing_arcs[lnum])
196
+ stats[lnum] = (exits, exits - missing)
197
+ return stats
198
+
199
+
200
+ TRegionLines = frozenset[TLineNo]
201
+
202
+
203
+ class AnalysisNarrower:
204
+ """
205
+ For reducing an `Analysis` to a subset of its lines.
206
+
207
+ Originally this was a simpler method on Analysis, but that led to quadratic
208
+ behavior. This class does the bulk of the work up-front to provide the
209
+ same results in linear time.
210
+
211
+ Create an AnalysisNarrower from an Analysis, bulk-add region lines to it
212
+ with `add_regions`, then individually request new narrowed Analysis objects
213
+ for each region with `narrow`. Doing most of the work in limited calls to
214
+ `add_regions` lets us avoid poor performance.
215
+ """
216
+
217
+ # In this class, regions are represented by a frozenset of their lines.
218
+
219
+ def __init__(self, analysis: Analysis) -> None:
220
+ self.analysis = analysis
221
+ self.region2arc_possibilities: dict[TRegionLines, set[TArc]] = collections.defaultdict(set)
222
+ self.region2arc_executed: dict[TRegionLines, set[TArc]] = collections.defaultdict(set)
223
+ self.region2exit_counts: dict[TRegionLines, dict[TLineNo, int]] = collections.defaultdict(
224
+ dict
225
+ )
226
+
227
+ def add_regions(self, liness: Iterable[set[TLineNo]]) -> None:
228
+ """
229
+ Pre-process a number of sets of line numbers. Later calls to `narrow`
230
+ with one of these sets will provide a narrowed Analysis.
231
+ """
232
+ if self.analysis.has_arcs:
233
+ line2region: dict[TLineNo, TRegionLines] = {}
234
+
235
+ for lines in liness:
236
+ fzlines = frozenset(lines)
237
+ for line in lines:
238
+ line2region[line] = fzlines
239
+
240
+ def collect_arcs(
241
+ arc_set: set[TArc],
242
+ region2arcs: dict[TRegionLines, set[TArc]],
243
+ ) -> None:
244
+ for a, b in arc_set:
245
+ if r := line2region.get(a):
246
+ region2arcs[r].add((a, b))
247
+ if r := line2region.get(b):
248
+ region2arcs[r].add((a, b))
249
+
250
+ collect_arcs(self.analysis.arc_possibilities_set, self.region2arc_possibilities)
251
+ collect_arcs(self.analysis.arcs_executed_set, self.region2arc_executed)
252
+
253
+ for lno, num in self.analysis.exit_counts.items():
254
+ if r := line2region.get(lno):
255
+ self.region2exit_counts[r][lno] = num
256
+
257
+ def narrow(self, lines: set[TLineNo]) -> Analysis:
258
+ """Create a narrowed Analysis.
259
+
260
+ The current analysis is copied to make a new one that only considers
261
+ the lines in `lines`.
262
+ """
263
+
264
+ # Technically, the set intersections in this method are still O(N**2)
265
+ # since this method is called N times, but they're very fast and moving
266
+ # them to `add_regions` won't avoid the quadratic time.
267
+
268
+ statements = self.analysis.statements & lines
269
+ excluded = self.analysis.excluded & lines
270
+ executed = self.analysis.executed & lines
271
+
272
+ if self.analysis.has_arcs:
273
+ fzlines = frozenset(lines)
274
+ arc_possibilities_set = self.region2arc_possibilities[fzlines]
275
+ arcs_executed_set = self.region2arc_executed[fzlines]
276
+ exit_counts = self.region2exit_counts[fzlines]
277
+ no_branch = self.analysis.no_branch & lines
278
+ else:
279
+ arc_possibilities_set = set()
280
+ arcs_executed_set = set()
281
+ exit_counts = {}
282
+ no_branch = set()
283
+
284
+ return Analysis(
285
+ precision=self.analysis.precision,
286
+ filename=self.analysis.filename,
287
+ has_arcs=self.analysis.has_arcs,
288
+ statements=statements,
289
+ excluded=excluded,
290
+ executed=executed,
291
+ arc_possibilities_set=arc_possibilities_set,
292
+ arcs_executed_set=arcs_executed_set,
293
+ exit_counts=exit_counts,
294
+ no_branch=no_branch,
295
+ )
296
+
297
+
298
+ @dataclasses.dataclass
299
+ class Numbers:
300
+ """The numerical results of measuring coverage.
301
+
302
+ This holds the basic statistics from `Analysis`, and is used to roll
303
+ up statistics across files.
304
+
305
+ """
306
+
307
+ precision: int = 0
308
+ n_files: int = 0
309
+ n_statements: int = 0
310
+ n_excluded: int = 0
311
+ n_missing: int = 0
312
+ n_branches: int = 0
313
+ n_partial_branches: int = 0
314
+ n_missing_branches: int = 0
315
+
316
+ @property
317
+ def n_executed(self) -> int:
318
+ """Returns the number of executed statements."""
319
+ return self.n_statements - self.n_missing
320
+
321
+ @property
322
+ def n_executed_branches(self) -> int:
323
+ """Returns the number of executed branches."""
324
+ return self.n_branches - self.n_missing_branches
325
+
326
+ @property
327
+ def pc_covered(self) -> float:
328
+ """Returns a single percentage value for coverage."""
329
+ if self.n_statements > 0:
330
+ numerator, denominator = self.ratio_covered
331
+ pc_cov = (100.0 * numerator) / denominator
332
+ else:
333
+ pc_cov = 100.0
334
+ return pc_cov
335
+
336
+ @property
337
+ def pc_covered_str(self) -> str:
338
+ """Returns the percent covered, as a string, without a percent sign.
339
+
340
+ Note that "0" is only returned when the value is truly zero, and "100"
341
+ is only returned when the value is truly 100. Rounding can never
342
+ result in either "0" or "100".
343
+
344
+ """
345
+ return display_covered(self.pc_covered, self.precision)
346
+
347
+ @property
348
+ def ratio_covered(self) -> tuple[int, int]:
349
+ """Return a numerator and denominator for the coverage ratio."""
350
+ numerator = self.n_executed + self.n_executed_branches
351
+ denominator = self.n_statements + self.n_branches
352
+ return numerator, denominator
353
+
354
+ def __add__(self, other: Numbers) -> Numbers:
355
+ return Numbers(
356
+ self.precision,
357
+ self.n_files + other.n_files,
358
+ self.n_statements + other.n_statements,
359
+ self.n_excluded + other.n_excluded,
360
+ self.n_missing + other.n_missing,
361
+ self.n_branches + other.n_branches,
362
+ self.n_partial_branches + other.n_partial_branches,
363
+ self.n_missing_branches + other.n_missing_branches,
364
+ )
365
+
366
+ def __radd__(self, other: int) -> Numbers:
367
+ # Implementing 0+Numbers allows us to sum() a list of Numbers.
368
+ assert other == 0 # we only ever call it this way.
369
+ return self
370
+
371
+
372
+ def display_covered(pc: float, precision: int) -> str:
373
+ """Return a displayable total percentage, as a string.
374
+
375
+ Note that "0" is only returned when the value is truly zero, and "100"
376
+ is only returned when the value is truly 100. Rounding can never
377
+ result in either "0" or "100".
378
+
379
+ """
380
+ near0 = 1.0 / 10**precision
381
+ if 0 < pc < near0:
382
+ pc = near0
383
+ elif (100.0 - near0) < pc < 100:
384
+ pc = 100.0 - near0
385
+ else:
386
+ pc = round(pc, precision)
387
+ return f"{pc:.{precision}f}"
388
+
389
+
390
+ def _line_ranges(
391
+ statements: Iterable[TLineNo],
392
+ lines: Iterable[TLineNo],
393
+ ) -> list[tuple[TLineNo, TLineNo]]:
394
+ """Produce a list of ranges for `format_lines`."""
395
+ statements = sorted(statements)
396
+ lines = sorted(lines)
397
+
398
+ pairs = []
399
+ start: TLineNo | None = None
400
+ lidx = 0
401
+ for stmt in statements:
402
+ if lidx >= len(lines):
403
+ break
404
+ if stmt == lines[lidx]:
405
+ lidx += 1
406
+ if not start:
407
+ start = stmt
408
+ end = stmt
409
+ elif start:
410
+ pairs.append((start, end))
411
+ start = None
412
+ if start:
413
+ pairs.append((start, end))
414
+ return pairs
415
+
416
+
417
+ def format_lines(
418
+ statements: Iterable[TLineNo],
419
+ lines: Iterable[TLineNo],
420
+ arcs: Iterable[tuple[TLineNo, list[TLineNo]]] | None = None,
421
+ ) -> str:
422
+ """Nicely format a list of line numbers.
423
+
424
+ Format a list of line numbers for printing by coalescing groups of lines as
425
+ long as the lines represent consecutive statements. This will coalesce
426
+ even if there are gaps between statements.
427
+
428
+ For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
429
+ `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
430
+
431
+ Both `lines` and `statements` can be any iterable. All of the elements of
432
+ `lines` must be in `statements`, and all of the values must be positive
433
+ integers.
434
+
435
+ If `arcs` is provided, they are (start,[end,end,end]) pairs that will be
436
+ included in the output as long as start isn't in `lines`.
437
+
438
+ """
439
+ line_items = [(pair[0], nice_pair(pair)) for pair in _line_ranges(statements, lines)]
440
+ if arcs is not None:
441
+ line_exits = sorted(arcs)
442
+ for line, exits in line_exits:
443
+ for ex in sorted(exits):
444
+ if line not in lines and ex not in lines:
445
+ dest = ex if ex > 0 else "exit"
446
+ line_items.append((line, f"{line}->{dest}"))
447
+
448
+ ret = ", ".join(t[-1] for t in sorted(line_items))
449
+ return ret
450
+
451
+
452
+ def should_fail_under(total: float, fail_under: float, precision: int) -> bool:
453
+ """Determine if a total should fail due to fail-under.
454
+
455
+ `total` is a float, the coverage measurement total. `fail_under` is the
456
+ fail_under setting to compare with. `precision` is the number of digits
457
+ to consider after the decimal point.
458
+
459
+ Returns True if the total should fail.
460
+
461
+ """
462
+ # We can never achieve higher than 100% coverage, or less than zero.
463
+ if not (0 <= fail_under <= 100.0):
464
+ msg = f"fail_under={fail_under} is invalid. Must be between 0 and 100."
465
+ raise ConfigError(msg)
466
+
467
+ # Special case for fail_under=100, it must really be 100.
468
+ if fail_under == 100.0 and total != 100.0:
469
+ return True
470
+
471
+ return round(total, precision) < fail_under