Skip to content

Commit f0723e8

Browse files
fix: address lint, type check, and coverage restart safety
- Add None guard for _cov_instance before ._analyze() to fix type checker error - Wrap stop/erase/start cycle in try/finally so coverage always restarts on error - Run ruff format on coverage_server.py and drift_sdk.py - Update uv.lock to match pyproject.toml dependency changes
1 parent a9eeb5e commit f0723e8

3 files changed

Lines changed: 47 additions & 41 deletions

File tree

drift/core/coverage_server.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def start_coverage_collection() -> bool:
4444
import coverage as coverage_module
4545
except ImportError:
4646
logger.warning(
47-
"Coverage requested but 'coverage' package is not installed. "
48-
"Install it with: pip install coverage"
47+
"Coverage requested but 'coverage' package is not installed. Install it with: pip install coverage"
4948
)
5049
return False
5150

@@ -108,38 +107,39 @@ def take_coverage_snapshot(baseline: bool = False) -> dict:
108107
_cov_instance.stop()
109108
coverage = {}
110109

111-
if baseline:
112-
data = _cov_instance.get_data()
113-
for filename in data.measured_files():
114-
if not _is_user_file(filename):
115-
continue
116-
try:
117-
_, statements, _, missing, _ = _cov_instance.analysis2(filename)
118-
missing_set = set(missing)
119-
lines_map = {}
120-
for line in statements:
121-
lines_map[str(line)] = 0 if line in missing_set else 1
122-
branch_data = _get_branch_data(data, filename)
123-
if lines_map:
124-
coverage[filename] = {"lines": lines_map, **branch_data}
125-
except Exception as e:
126-
logger.debug(f"Failed to analyze {filename}: {e}")
127-
continue
128-
else:
129-
data = _cov_instance.get_data()
130-
for filename in data.measured_files():
131-
if not _is_user_file(filename):
132-
continue
133-
lines = data.lines(filename)
134-
if lines:
135-
branch_data = _get_branch_data(data, filename)
136-
coverage[filename] = {
137-
"lines": {str(line): 1 for line in lines},
138-
**branch_data,
139-
}
140-
141-
_cov_instance.erase()
142-
_cov_instance.start()
110+
try:
111+
if baseline:
112+
data = _cov_instance.get_data()
113+
for filename in data.measured_files():
114+
if not _is_user_file(filename):
115+
continue
116+
try:
117+
_, statements, _, missing, _ = _cov_instance.analysis2(filename)
118+
missing_set = set(missing)
119+
lines_map = {}
120+
for line in statements:
121+
lines_map[str(line)] = 0 if line in missing_set else 1
122+
branch_data = _get_branch_data(data, filename)
123+
if lines_map:
124+
coverage[filename] = {"lines": lines_map, **branch_data}
125+
except Exception as e:
126+
logger.debug(f"Failed to analyze {filename}: {e}")
127+
continue
128+
else:
129+
data = _cov_instance.get_data()
130+
for filename in data.measured_files():
131+
if not _is_user_file(filename):
132+
continue
133+
lines = data.lines(filename)
134+
if lines:
135+
branch_data = _get_branch_data(data, filename)
136+
coverage[filename] = {
137+
"lines": {str(line): 1 for line in lines},
138+
**branch_data,
139+
}
140+
finally:
141+
_cov_instance.erase()
142+
_cov_instance.start()
143143

144144
return coverage
145145

@@ -166,6 +166,9 @@ def _get_branch_data(data, filename: str) -> dict:
166166
if not data.has_arcs():
167167
return {"totalBranches": 0, "coveredBranches": 0, "branches": {}}
168168

169+
if _cov_instance is None:
170+
return {"totalBranches": 0, "coveredBranches": 0, "branches": {}}
171+
169172
analysis = _cov_instance._analyze(filename)
170173
numbers = analysis.numbers
171174

drift/core/drift_sdk.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def initialize(
163163
# Start coverage collection early (before any SDK mode checks that might return early).
164164
# Coverage data is accessed via protobuf channel (communicator handles requests).
165165
from .coverage_server import start_coverage_collection
166+
166167
start_coverage_collection()
167168

168169
instance._init_params = {

uv.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)