Skip to content

Commit

Permalink
refactor: a slightly different way to deal with unserializable Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Mar 17, 2024
1 parent 121703b commit c02c967
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 10 additions & 3 deletions coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import collections
import dataclasses
import datetime
import functools
import json
Expand Down Expand Up @@ -40,7 +41,10 @@

class IndexInfoDict(TypedDict):
"""Information for each file, to render the index file."""
nums: Numbers
# For in-memory use, we have Numbers. For serialization, we write a list
# of ints. Two fields keeps the type-checker happier.
nums: Numbers | None
numlist: list[int]
html_filename: str
relative_filename: str

Expand Down Expand Up @@ -460,6 +464,7 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
# Save this file's information for the index file.
index_info: IndexInfoDict = {
"nums": ftr.analysis.numbers,
"numlist": [],
"html_filename": ftr.html_filename,
"relative_filename": ftr.fr.relative_filename(),
}
Expand Down Expand Up @@ -565,7 +570,7 @@ def read(self) -> None:
if usable:
self.files = {}
for filename, fileinfo in status["files"].items():
fileinfo["index"]["nums"] = Numbers(*fileinfo["index"]["nums"])
fileinfo["index"]["nums"] = Numbers(*fileinfo["index"]["numlist"])
self.files[filename] = fileinfo
self.globals = status["globals"]
else:
Expand All @@ -577,7 +582,9 @@ def write(self) -> None:
files = {}
for filename, fileinfo in self.files.items():
index = fileinfo["index"]
index["nums"] = index["nums"].init_args() # type: ignore[typeddict-item]
assert index["nums"] is not None
index["numlist"] = list(dataclasses.astuple(index["nums"]))
index["nums"] = None
files[filename] = fileinfo

status = {
Expand Down
4 changes: 0 additions & 4 deletions coverage/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ class Numbers:
n_partial_branches: int = 0
n_missing_branches: int = 0

def init_args(self) -> list[int]:
"""Return a list for __init__(*args) to recreate this object."""
return list(dataclasses.astuple(self))

@property
def n_executed(self) -> int:
"""Returns the number of executed statements."""
Expand Down

0 comments on commit c02c967

Please sign in to comment.