Skip to content

Commit 59bea79

Browse files
committed
fix: resolve all remaining lint errors
- Use importlib.util.find_spec for module availability checks - Use contextlib.suppress instead of try-except-pass - Use ternary operator for simple conditionals - Remove unused method arguments
1 parent 7b142b0 commit 59bea79

4 files changed

Lines changed: 18 additions & 29 deletions

File tree

src/trueentropy/harvesters/network.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ def collect(self) -> HarvestResult:
119119
Returns:
120120
HarvestResult containing network timing entropy
121121
"""
122-
# Attempt import of requests library
123-
try:
124-
import requests
125-
except ImportError:
122+
# Check if requests library is available
123+
import importlib.util
124+
125+
if importlib.util.find_spec("requests") is None:
126126
return HarvestResult(
127127
data=b"",
128128
entropy_bits=0,
@@ -132,7 +132,7 @@ def collect(self) -> HarvestResult:
132132
)
133133

134134
# Collect latency measurements
135-
measurements = self._measure_latencies(requests)
135+
measurements = self._measure_latencies()
136136

137137
if not measurements:
138138
return HarvestResult(
@@ -156,17 +156,14 @@ def collect(self) -> HarvestResult:
156156
# Private Methods
157157
# -------------------------------------------------------------------------
158158

159-
def _measure_latencies(self, requests_module: object) -> list[tuple[str, int]]:
159+
def _measure_latencies(self) -> list[tuple[str, int]]:
160160
"""
161161
Measure latency to each target server.
162162
163-
Args:
164-
requests_module: The imported requests module
165-
166163
Returns:
167164
List of (target, latency_ns) tuples for successful requests
168165
"""
169-
import requests # Type ignore for the module passed in
166+
import requests
170167

171168
measurements: list[tuple[str, int]] = []
172169

src/trueentropy/harvesters/system.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def collect(self) -> HarvestResult:
8484
Returns:
8585
HarvestResult containing system state entropy
8686
"""
87-
# Attempt import of psutil
88-
try:
89-
import psutil
90-
except ImportError:
87+
# Check if psutil library is available
88+
import importlib.util
89+
90+
if importlib.util.find_spec("psutil") is None:
9191
return HarvestResult(
9292
data=b"",
9393
entropy_bits=0,
@@ -97,7 +97,7 @@ def collect(self) -> HarvestResult:
9797
)
9898

9999
# Collect system metrics
100-
metrics = self._collect_metrics(psutil)
100+
metrics = self._collect_metrics()
101101

102102
# Convert to bytes
103103
data = self._metrics_to_bytes(metrics)
@@ -112,17 +112,14 @@ def collect(self) -> HarvestResult:
112112
# Private Methods
113113
# -------------------------------------------------------------------------
114114

115-
def _collect_metrics(self, psutil: object) -> list[tuple[str, int | float]]:
115+
def _collect_metrics(self) -> list[tuple[str, int | float]]:
116116
"""
117117
Collect various system metrics.
118118
119-
Args:
120-
psutil: The imported psutil module
121-
122119
Returns:
123120
List of (metric_name, value) tuples
124121
"""
125-
import psutil as ps # For type hints
122+
import psutil as ps
126123

127124
metrics: list[tuple[str, int | float]] = []
128125

src/trueentropy/persistence.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ def save_pool(pool: EntropyPool, path: PathLike, include_checksum: bool = True)
8989
_write_pool_state(f, state_data, include_checksum)
9090

9191
# Set restrictive permissions on Unix systems
92-
try:
92+
import contextlib
93+
94+
with contextlib.suppress(OSError, AttributeError):
9395
os.chmod(path, 0o600)
94-
except (OSError, AttributeError):
95-
pass # Windows or permission denied
9696

9797

9898
def load_pool(path: PathLike, verify_checksum: bool = True) -> EntropyPool:

src/trueentropy/pool.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ def __init__(self, seed: bytes | None = None) -> None:
8989
# Initialize the pool with random data
9090
# We use os.urandom() as the initial seed because it provides
9191
# cryptographically secure random bytes from the OS
92-
if seed is not None:
93-
# Use provided seed (useful for testing with deterministic output)
94-
initial = seed
95-
else:
96-
# Use OS-provided entropy for production use
97-
initial = os.urandom(self.POOL_SIZE)
92+
initial = seed if seed is not None else os.urandom(self.POOL_SIZE)
9893

9994
# Expand seed to full pool size if needed
10095
self._pool: bytes = self._expand_to_pool_size(initial)

0 commit comments

Comments
 (0)