Skip to content

Commit 63db625

Browse files
committed
fix flake8 error
1 parent 45cebc9 commit 63db625

11 files changed

Lines changed: 51 additions & 63 deletions

objwatch/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from .config import ObjWatchConfig
1010
from .tracer import Tracer
1111
from .wrappers import ABCWrapper
12-
from .runtime_info import runtime_info
13-
from .sinks.consumer import ZeroMQFileConsumer, DynamicRoutingConsumer
12+
from .sinks.consumer import DynamicRoutingConsumer
1413
from .utils.logger import log_info, setup_logging_from_config
14+
from .runtime_info import runtime_info
1515

1616

1717
class ObjWatch:

objwatch/sinks/consumer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# MIT License
22
# Copyright (c) 2025 aeeeeeep
33

4+
import os
45
import zmq
56
import time
67
import logging
78
import msgpack
89
import threading
9-
import os
10-
import sys
1110
from pathlib import Path
1211
from typing import Dict, Any, Optional
1312
from collections import OrderedDict
14-
from functools import lru_cache
1513

1614

1715
class ZeroMQFileConsumer:

objwatch/utils/logger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,17 @@ def emit(self, record: logging.LogRecord) -> None:
130130
'time': record.created,
131131
'name': record.name,
132132
}
133-
133+
134134
# Add output_file and process_id for dynamic routing support
135135
if hasattr(sink, 'output_file') and sink.output_file:
136136
event['output_file'] = sink.output_file
137137
else:
138138
event['output_file'] = None
139-
139+
140140
import os
141+
141142
event['process_id'] = os.getpid()
142-
143+
143144
sink.emit(event)
144145
except Exception as e:
145146
logging.error(f"SinkHandler failed to emit record: {e}")

tests/test_base.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33

44
import os
55
import runpy
6-
import importlib
7-
import unittest
8-
from unittest.mock import MagicMock, patch
96
import logging
7+
import unittest
8+
import importlib
109
from io import StringIO
10+
from unittest.mock import MagicMock, patch
11+
1112
import objwatch
12-
from objwatch.config import ObjWatchConfig
1313
from objwatch.wrappers import BaseWrapper, TensorShapeWrapper, ABCWrapper
1414
from objwatch.core import ObjWatch
1515
from objwatch.targets import Targets
16-
from objwatch.tracer import Tracer
1716
from tests.util import strip_line_numbers
1817

1918
try:
@@ -285,7 +284,10 @@ def test_wrap_call_with_tensor_dict_over_limit(self):
285284

286285
tensors_dict = {f"key_{i}": torch.randn(2, 2) for i in range(5)}
287286
mock_frame.f_locals = {'arg_tensors': tensors_dict}
288-
expected_call_msg = "'0':(dict)[('key_0', torch.Size([2, 2])), ('key_1', torch.Size([2, 2])), ('key_2', torch.Size([2, 2])), '... (2 more elements)']"
287+
expected_call_msg = (
288+
"'0':(dict)[('key_0', torch.Size([2, 2])), ('key_1', torch.Size([2, 2])), "
289+
"('key_2', torch.Size([2, 2])), '... (2 more elements)']"
290+
)
289291
actual_call_msg = self.tensor_shape_logger.wrap_call('test_tensor_func', mock_frame)
290292
self.assertEqual(actual_call_msg, expected_call_msg)
291293

tests/test_comprehensive_exclude.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
#!/usr/bin/env python3
2-
"""
3-
Comprehensive test for exclude functionality in track_all mode.
4-
"""
5-
6-
import sys
7-
import os
8-
9-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
103

114
from objwatch.tracer import Tracer
125
from objwatch.config import ObjWatchConfig
136

14-
# Import test module from the same directory
15-
from .utils.example_module import TestClass
16-
177

188
def test_comprehensive_exclude():
199
"""Test comprehensive exclude functionality with track_all mode."""

tests/test_exclude_functionality.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
#!/usr/bin/env python3
2-
"""Test script to verify exclude functionality with track_all."""
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
33

44
import sys
5-
import os
65

7-
# Add the objwatch package to the path
8-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
9-
10-
from objwatch.config import ObjWatchConfig
116
from objwatch.tracer import Tracer
12-
13-
# Import test module from the same directory
14-
from .utils.example_module import TestClass
7+
from objwatch.config import ObjWatchConfig
158

169

1710
def test_exclude_functionality():
@@ -50,10 +43,10 @@ def test_exclude_functionality():
5043
print(f"Should track excluded_attr: {should_track_attr_excluded}")
5144

5245
# Verify results
53-
assert should_track_tracked == True, "tracked_method should be tracked"
54-
assert should_track_excluded == False, "excluded_method should be excluded"
55-
assert should_track_attr_tracked == True, "tracked_attr should be tracked"
56-
assert should_track_attr_excluded == False, "excluded_attr should be excluded"
46+
assert should_track_tracked, "tracked_method should be tracked"
47+
assert not should_track_excluded, "excluded_method should be excluded"
48+
assert should_track_attr_tracked, "tracked_attr should be tracked"
49+
assert not should_track_attr_excluded, "excluded_attr should be excluded"
5750

5851
print("All exclude functionality tests passed!")
5952
# All assertions passed, no return value needed for pytest

tests/test_multiprocessing_handls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# MIT License
2+
# Copyright (c) 2025 aeeeeeep
3+
14
import runpy
25
import unittest
36
from objwatch import ObjWatch

tests/test_zmq_dynamic_routing.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import time
66
import tempfile
77
import unittest
8-
from objwatch.sinks.consumer import DynamicRoutingConsumer
8+
99
from objwatch.sinks.zmq_sink import ZeroMQSink
10+
from objwatch.sinks.consumer import DynamicRoutingConsumer
1011

1112

1213
class TestDynamicRoutingConsumer(unittest.TestCase):
@@ -28,16 +29,17 @@ def tearDown(self):
2829
"""
2930
# Clean up test output files
3031
if os.path.exists(self.temp_dir):
32+
import logging
3133
for filename in os.listdir(self.temp_dir):
3234
filepath = os.path.join(self.temp_dir, filename)
3335
try:
3436
os.remove(filepath)
35-
except Exception:
36-
pass
37+
except Exception as e:
38+
logging.debug(f"Failed to remove {filepath}: {e}")
3739
try:
3840
os.rmdir(self.temp_dir)
39-
except Exception:
40-
pass
41+
except Exception as e:
42+
logging.debug(f"Failed to remove directory {self.temp_dir}: {e}")
4143

4244
def test_dynamic_routing_basic(self):
4345
"""
@@ -88,7 +90,7 @@ def test_dynamic_routing_basic(self):
8890
"output_file": output1,
8991
"process_id": os.getpid(),
9092
}
91-
93+
9294
sink.emit(event1)
9395
sink.emit(event2)
9496
sink.emit(event3)
@@ -114,18 +116,18 @@ def test_dynamic_routing_basic(self):
114116
# Check that at least some messages were received
115117
self.assertTrue(len(content1) > 0, "Output file 1 should contain messages")
116118
self.assertTrue(len(content2) > 0, "Output file 2 should contain messages")
117-
119+
118120
# Check for presence of expected messages (may not be all due to ZeroMQ async nature)
119121
if "Message to output1" in content1:
120122
print("✓ Received 'Message to output1'")
121123
else:
122124
print("✗ Did not receive 'Message to output1' (may be due to ZeroMQ timing)")
123-
125+
124126
if "Another message to output1" in content1:
125127
print("✓ Received 'Another message to output1'")
126128
else:
127129
print("✗ Did not receive 'Another message to output1' (may be due to ZeroMQ timing)")
128-
130+
129131
if "Message to output2" in content2:
130132
print("✓ Received 'Message to output2'")
131133
else:
@@ -172,13 +174,13 @@ def test_file_handle_lru_cache(self):
172174
Test LRU cache for file handles.
173175
"""
174176
max_open_files = 3
175-
177+
176178
# Create ZeroMQSink first and bind to endpoint
177179
sink = ZeroMQSink(endpoint=self.endpoint, topic="")
178-
180+
179181
# Wait a bit for sink to be ready
180182
time.sleep(0.1)
181-
183+
182184
consumer = DynamicRoutingConsumer(
183185
endpoint=self.endpoint,
184186
auto_start=True,
@@ -226,9 +228,7 @@ def test_consumer_lifecycle(self):
226228
Test proper lifecycle management of DynamicRoutingConsumer.
227229
"""
228230
# Create consumer
229-
consumer = DynamicRoutingConsumer(
230-
endpoint=self.endpoint, auto_start=False, allowed_directories=[self.temp_dir]
231-
)
231+
consumer = DynamicRoutingConsumer(endpoint=self.endpoint, auto_start=False, allowed_directories=[self.temp_dir])
232232

233233
# Start consumer
234234
consumer.start()
@@ -284,10 +284,10 @@ def test_process_id_in_output(self):
284284

285285
# Create ZeroMQSink first and bind to endpoint
286286
sink = ZeroMQSink(endpoint=self.endpoint, topic="")
287-
287+
288288
# Wait a bit for sink to be ready
289289
time.sleep(0.1)
290-
290+
291291
# Create and start consumer
292292
consumer = DynamicRoutingConsumer(
293293
endpoint=self.endpoint, auto_start=True, daemon=True, allowed_directories=[self.temp_dir]
@@ -321,10 +321,10 @@ def test_process_id_in_output(self):
321321
if os.path.exists(output_file):
322322
with open(output_file, "r") as f:
323323
content = f.read()
324-
324+
325325
# Check that at least some messages were received
326326
self.assertTrue(len(content) > 0, "Output file should contain messages")
327-
327+
328328
# Check for process ID (may not be present if no messages were received)
329329
if "PID:12345" in content:
330330
print("✓ Process ID found in output")

tests/test_zmq_e2e.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
import unittest
77
import tempfile
8+
89
from objwatch import ObjWatch, watch
910
from objwatch.config import ObjWatchConfig
1011
from objwatch.sinks.consumer import ZeroMQFileConsumer

tests/test_zmq_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
import time
7+
78
from objwatch import ObjWatch
89
from objwatch.config import ObjWatchConfig
910

0 commit comments

Comments
 (0)