Skip to content

Commit 394683d

Browse files
Copilotjgbradley1
andcommitted
Fix linting issues in workflow callback handlers
Co-authored-by: jgbradley1 <654554+jgbradley1@users.noreply.github.com>
1 parent e427bda commit 394683d

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

graphrag/callbacks/blob_workflow_callbacks.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
try:
1313
from azure.identity import DefaultAzureCredential
1414
from azure.storage.blob import BlobServiceClient
15+
1516
_AZURE_AVAILABLE = True
1617
except ImportError:
1718
_AZURE_AVAILABLE = False
19+
1820
# Create dummy classes for type hints when azure is not available
1921
class DefaultAzureCredential: # type: ignore
20-
pass
22+
"""Dummy class when Azure is not available."""
23+
2124
class BlobServiceClient: # type: ignore
22-
pass
25+
"""Dummy class when Azure is not available."""
26+
2327

2428
from graphrag.callbacks.workflow_handler_base import WorkflowHandlerBase
2529

@@ -42,21 +46,21 @@ def __init__(
4246
):
4347
"""Create a new instance of the BlobWorkflowCallbacks class."""
4448
super().__init__(level)
45-
49+
4650
if not _AZURE_AVAILABLE:
4751
msg = "Azure dependencies are not installed. Install graphrag with azure extras."
4852
raise ImportError(msg)
49-
53+
5054
if container_name is None:
5155
msg = "No container name provided for blob storage."
5256
raise ValueError(msg)
5357
if connection_string is None and storage_account_blob_url is None:
5458
msg = "No storage account blob url provided for blob storage."
5559
raise ValueError(msg)
56-
60+
5761
self._connection_string = connection_string
5862
self._storage_account_blob_url = storage_account_blob_url
59-
63+
6064
if self._connection_string:
6165
self._blob_service_client = BlobServiceClient.from_connection_string(
6266
self._connection_string
@@ -92,27 +96,26 @@ def emit(self, record):
9296
"type": self._get_log_type(record.levelno),
9397
"data": record.getMessage(),
9498
}
95-
99+
96100
# Add additional fields if they exist
97-
if hasattr(record, 'details') and record.details:
101+
if hasattr(record, "details") and record.details:
98102
log_data["details"] = record.details
99103
if record.exc_info and record.exc_info[1]:
100104
log_data["cause"] = str(record.exc_info[1])
101-
if hasattr(record, 'stack') and record.stack:
105+
if hasattr(record, "stack") and record.stack:
102106
log_data["stack"] = record.stack
103-
107+
104108
self._write_log(log_data)
105-
except Exception:
109+
except (OSError, ValueError):
106110
self.handleError(record)
107111

108112
def _get_log_type(self, level: int) -> str:
109113
"""Get log type string based on log level."""
110114
if level >= logging.ERROR:
111115
return "error"
112-
elif level >= logging.WARNING:
116+
if level >= logging.WARNING:
113117
return "warning"
114-
else:
115-
return "log"
118+
return "log"
116119

117120
def _write_log(self, log: dict[str, Any]):
118121
"""Write log data to blob storage."""

graphrag/callbacks/console_workflow_callbacks.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@ def __init__(self, level: int = logging.NOTSET):
1717
super().__init__(level)
1818
# Use a StreamHandler for actual console output
1919
self._stream_handler = logging.StreamHandler(sys.stdout)
20-
20+
2121
# Set up a formatter for console output
2222
formatter = logging.Formatter(
23-
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
23+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
2424
)
2525
self._stream_handler.setFormatter(formatter)
2626

2727
def emit(self, record):
2828
"""Emit a log record using the underlying StreamHandler."""
2929
# For warning records, add color formatting if the message is just a warning
30-
if record.levelno == logging.WARNING and hasattr(record, 'details'):
30+
if record.levelno == logging.WARNING and hasattr(record, "details"):
3131
# Apply warning color formatting similar to original
3232
formatted_msg = f"\033[93m{record.getMessage()}\033[00m"
3333
# Create a new record with the colored message
3434
colored_record = logging.LogRecord(
35-
record.name, record.levelno, record.pathname, record.lineno,
36-
formatted_msg, (), record.exc_info
35+
record.name,
36+
record.levelno,
37+
record.pathname,
38+
record.lineno,
39+
formatted_msg,
40+
(),
41+
record.exc_info,
3742
)
3843
self._stream_handler.emit(colored_record)
3944
else:

graphrag/callbacks/file_workflow_callbacks.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class WorkflowJSONFileHandler(logging.FileHandler):
1414
"""A FileHandler that formats log records as JSON for workflow callbacks."""
15-
15+
1616
def emit(self, record):
1717
"""Emit a log record as JSON."""
1818
try:
@@ -21,33 +21,32 @@ def emit(self, record):
2121
"type": self._get_log_type(record.levelno),
2222
"data": record.getMessage(),
2323
}
24-
24+
2525
# Add additional fields if they exist
26-
if hasattr(record, 'details') and record.details:
26+
if hasattr(record, "details") and record.details:
2727
log_data["details"] = record.details
2828
if record.exc_info and record.exc_info[1]:
2929
log_data["source"] = str(record.exc_info[1])
30-
if hasattr(record, 'stack') and record.stack:
30+
if hasattr(record, "stack") and record.stack:
3131
log_data["stack"] = record.stack
32-
32+
3333
# Write JSON to file
3434
json_str = json.dumps(log_data, indent=4, ensure_ascii=False) + "\n"
35-
35+
3636
if self.stream is None:
3737
self.stream = self._open()
3838
self.stream.write(json_str)
3939
self.flush()
40-
except Exception:
40+
except (OSError, ValueError):
4141
self.handleError(record)
42-
42+
4343
def _get_log_type(self, level: int) -> str:
4444
"""Get log type string based on log level."""
4545
if level >= logging.ERROR:
4646
return "error"
47-
elif level >= logging.WARNING:
47+
if level >= logging.WARNING:
4848
return "warning"
49-
else:
50-
return "log"
49+
return "log"
5150

5251

5352
class FileWorkflowCallbacks(WorkflowHandlerBase):
@@ -56,23 +55,23 @@ class FileWorkflowCallbacks(WorkflowHandlerBase):
5655
def __init__(self, directory: str, level: int = logging.NOTSET):
5756
"""Create a new file-based workflow handler."""
5857
super().__init__(level)
59-
58+
6059
# Ensure directory exists
6160
Path(directory).mkdir(parents=True, exist_ok=True)
62-
61+
6362
# Create the JSON file handler
6463
log_file_path = Path(directory) / "logs.json"
65-
self._file_handler = WorkflowJSONFileHandler(str(log_file_path), mode='a')
66-
64+
self._file_handler = WorkflowJSONFileHandler(str(log_file_path), mode="a")
65+
6766
# Also create a regular logger for backwards compatibility
6867
self._logger = logging.getLogger(__name__)
6968

7069
def emit(self, record):
7170
"""Emit a log record using the underlying FileHandler."""
7271
# Emit to the JSON file
7372
self._file_handler.emit(record)
74-
75-
# Also emit to regular logger for backwards compatibility
73+
74+
# Also emit to regular logger for backwards compatibility
7675
if record.levelno >= logging.WARNING:
7776
self._logger.log(record.levelno, record.getMessage())
7877

graphrag/callbacks/workflow_handler_base.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""Base class for workflow callbacks that inherit from logging.Handler."""
55

66
import logging
7-
from typing import Any
87

98
from graphrag.callbacks.workflow_callbacks import WorkflowCallbacks
109
from graphrag.index.typing.pipeline_run_result import PipelineRunResult
@@ -17,7 +16,7 @@ class WorkflowHandlerBase(logging.Handler, WorkflowCallbacks):
1716
def __init__(self, level: int = logging.NOTSET):
1817
"""Initialize the handler."""
1918
super().__init__(level)
20-
19+
2120
def pipeline_start(self, names: list[str]) -> None:
2221
"""Execute this callback to signal when the entire pipeline starts."""
2322
record = logging.LogRecord(
@@ -95,7 +94,7 @@ def error(
9594
full_message = message
9695
if details:
9796
full_message = f"{message} details={details}"
98-
97+
9998
record = logging.LogRecord(
10099
name="graphrag.error",
101100
level=logging.ERROR,
@@ -105,21 +104,21 @@ def error(
105104
args=(),
106105
exc_info=(type(cause), cause, None) if cause else None,
107106
)
108-
107+
109108
# Add custom attributes for stack and details
110109
if stack:
111110
record.stack = stack # type: ignore
112111
if details:
113112
record.details = details # type: ignore
114-
113+
115114
self.emit(record)
116115

117116
def warning(self, message: str, details: dict | None = None) -> None:
118117
"""Handle when a warning occurs."""
119118
full_message = message
120119
if details:
121120
full_message = f"{message} details={details}"
122-
121+
123122
record = logging.LogRecord(
124123
name="graphrag.warning",
125124
level=logging.WARNING,
@@ -129,18 +128,18 @@ def warning(self, message: str, details: dict | None = None) -> None:
129128
args=(),
130129
exc_info=None,
131130
)
132-
131+
133132
if details:
134133
record.details = details # type: ignore
135-
134+
136135
self.emit(record)
137136

138137
def log(self, message: str, details: dict | None = None) -> None:
139138
"""Handle when a log message occurs."""
140139
full_message = message
141140
if details:
142141
full_message = f"{message} details={details}"
143-
142+
144143
record = logging.LogRecord(
145144
name="graphrag.log",
146145
level=logging.INFO,
@@ -150,8 +149,8 @@ def log(self, message: str, details: dict | None = None) -> None:
150149
args=(),
151150
exc_info=None,
152151
)
153-
152+
154153
if details:
155154
record.details = details # type: ignore
156-
157-
self.emit(record)
155+
156+
self.emit(record)

0 commit comments

Comments
 (0)