summaryrefslogtreecommitdiff
path: root/coverage/html.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2023-03-22 13:12:16 -0400
committerNed Batchelder <ned@nedbatchelder.com>2023-03-22 13:58:58 -0400
commit63f3e495b184ebd9aa4c9d757fb78ed148580d29 (patch)
treeaf4c68f94d50fafe94d56fce46cf92739cd8bd3f /coverage/html.py
parenta5dc01131b1a38bb7c7908ad968488d87e3664ed (diff)
downloadpython-coveragepy-git-63f3e495b184ebd9aa4c9d757fb78ed148580d29.tar.gz
style: clean up #1587
Diffstat (limited to 'coverage/html.py')
-rw-r--r--coverage/html.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/coverage/html.py b/coverage/html.py
index 23fba3f0..d172e896 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -5,12 +5,12 @@
from __future__ import annotations
+import collections
import datetime
import json
import os
import re
import shutil
-from collections import Counter
from dataclasses import dataclass
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING, cast
@@ -370,9 +370,12 @@ class HtmlReporter:
# Write the HTML page for this file.
file_data = self.datagen.data_for_file(ftr.fr, ftr.analysis)
- contexts = Counter(c for cline in file_data.lines for c in cline.contexts)
+ contexts = collections.Counter(c for cline in file_data.lines for c in cline.contexts)
context_codes = {y: i for (i, y) in enumerate(x[0] for x in contexts.most_common())}
- contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)
+ if context_codes:
+ contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)
+ else:
+ contexts_json = None
for ldata in file_data.lines:
# Build the HTML for the line.
@@ -382,13 +385,11 @@ class HtmlReporter:
html_parts.append(escape(tok_text))
else:
tok_html = escape(tok_text) or '&nbsp;'
- html_parts.append(
- f'<span class="{tok_type}">{tok_html}</span>'
- )
+ html_parts.append(f'<span class="{tok_type}">{tok_html}</span>')
ldata.html = ''.join(html_parts)
-
ldata.context_str = ",".join(
- str(context_codes[c_context]) for c_context in ldata.context_list)
+ str(context_codes[c_context]) for c_context in ldata.context_list
+ )
if ldata.short_annotations:
# 202F is NARROW NO-BREAK SPACE.
@@ -422,13 +423,10 @@ class HtmlReporter:
)
ldata.css_class = ' '.join(css_classes) or "pln"
- if context_codes:
- file_data.__dict__["contexts_json"] = contexts_json
- else:
- file_data.__dict__["contexts_json"] = None
html_path = os.path.join(self.directory, ftr.html_filename)
html = self.source_tmpl.render({
**file_data.__dict__,
+ "contexts_json": contexts_json,
'prev_html': prev_html,
'next_html': next_html,
})