66import json
77import pathlib
88from collections .abc import Sequence
9+ from typing import Any
910
1011from coverage_comment import log , subprocess
1112
@@ -21,20 +22,19 @@ class CoverageMetadata:
2122 show_contexts : bool
2223
2324
24- class OutputMixin :
25- def as_output (self , prefix : str ) -> dict :
26- data = dataclasses .asdict (self )
27- output = {}
28- for key , value in data .items ():
29- if value is not None and not isinstance (value , dict ):
30- output [f"{ prefix } _{ key } " ] = (
31- float (value ) if isinstance (value , decimal .Decimal ) else value
32- )
33- return output
25+ def as_output (obj : Any , prefix : str ) -> dict [str , Any ]:
26+ data = dataclasses .asdict (obj )
27+ output : dict [str , Any ] = {}
28+ for key , value in data .items ():
29+ if value is not None and not isinstance (value , dict ):
30+ output [f"{ prefix } _{ key } " ] = (
31+ float (value ) if isinstance (value , decimal .Decimal ) else value
32+ )
33+ return output
3434
3535
3636@dataclasses .dataclass (kw_only = True )
37- class CoverageInfo ( OutputMixin ) :
37+ class CoverageInfo :
3838 covered_lines : int
3939 num_statements : int
4040 percent_covered : decimal .Decimal
@@ -88,7 +88,7 @@ def violation_lines(self) -> list[int]:
8888
8989
9090@dataclasses .dataclass (kw_only = True )
91- class DiffCoverage ( OutputMixin ) :
91+ class DiffCoverage :
9292 total_num_lines : int
9393 total_num_violations : int
9494 total_percent_covered : decimal .Decimal
@@ -112,7 +112,7 @@ def compute_coverage(
112112
113113def get_coverage_info (
114114 merge : bool , coverage_path : pathlib .Path
115- ) -> tuple [dict , Coverage ]:
115+ ) -> tuple [dict [ str , Any ] , Coverage ]:
116116 try :
117117 if merge :
118118 subprocess .run ("coverage" , "combine" , path = coverage_path )
@@ -160,7 +160,7 @@ def generate_coverage_markdown(coverage_path: pathlib.Path) -> str:
160160 )
161161
162162
163- def _make_coverage_info (data : dict ) -> CoverageInfo :
163+ def _make_coverage_info (data : dict [ str , Any ] ) -> CoverageInfo :
164164 """Build a CoverageInfo object from a "summary" or "totals" key."""
165165 return CoverageInfo (
166166 covered_lines = data ["covered_lines" ],
@@ -180,7 +180,7 @@ def _make_coverage_info(data: dict) -> CoverageInfo:
180180 )
181181
182182
183- def extract_info (data : dict , coverage_path : pathlib .Path ) -> Coverage :
183+ def extract_info (data : dict [ str , Any ] , coverage_path : pathlib .Path ) -> Coverage :
184184 """
185185 {
186186 "meta": {
@@ -246,7 +246,7 @@ def extract_info(data: dict, coverage_path: pathlib.Path) -> Coverage:
246246def get_diff_coverage_info (
247247 added_lines : dict [pathlib .Path , list [int ]], coverage : Coverage
248248) -> DiffCoverage :
249- files = {}
249+ files : dict [ pathlib . Path , FileDiffCoverage ] = {}
250250 total_num_lines = 0
251251 total_num_violations = 0
252252 num_changed_lines = 0
0 commit comments