Skip to content

Commit 809e92f

Browse files
committed
fix mypy issues
1 parent 738d7dc commit 809e92f

File tree

15 files changed

+258
-217
lines changed

15 files changed

+258
-217
lines changed

logprep/abc/component.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@ def __attrs_post_init__(self):
5454
attribute.labels = self._labels
5555
attribute.init_tracker()
5656

57-
# __dict__ is added to support functools.cached_property
5857
__slots__ = [
5958
"name",
6059
"_config",
6160
"pipeline_index",
6261
"_job_tag_for_cleanup",
6362
"_is_shut_down",
64-
"__dict__",
63+
"__dict__", # __dict__ is added to support functools.cached_property
6564
]
6665

6766
# instance attributes

logprep/abc/processor.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import logging
44
import os
5+
import typing
56
from abc import abstractmethod
6-
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Type, cast
7+
from typing import TYPE_CHECKING, Any, ClassVar, Iterable, Sequence, Type, cast
78

89
from attrs import define, field, validators
910

@@ -41,13 +42,13 @@ class ProcessorResult:
4142
4243
processor_name : str
4344
The name of the processor
44-
event: Optional[dict]
45+
event: dict | None
4546
A reference to the event that was processed
46-
data : Optional[list]
47+
data : list | None
4748
The generated extra data
48-
errors : Optional[list]
49+
errors : list | None
4950
The errors that occurred during processing
50-
warnings : Optional[list]
51+
warnings : list | None
5152
The warnings that occurred during processing
5253
"""
5354

@@ -82,11 +83,11 @@ class Processor(Component):
8283
class Config(Component.Config):
8384
"""Common Configurations"""
8485

85-
rules: List[str] = field(
86-
validator=[
87-
validators.instance_of(list),
88-
validators.deep_iterable(member_validator=validators.instance_of((str, dict))),
89-
]
86+
rules: list[str] = field(
87+
validator=validators.deep_iterable(
88+
member_validator=validators.instance_of((str, dict)),
89+
iterable_validator=validators.instance_of(list),
90+
),
9091
)
9192
"""List of rule locations to load rules from.
9293
In addition to paths to file directories it is possible to retrieve rules from a URI.
@@ -118,14 +119,19 @@ class Config(Component.Config):
118119

119120
def __init__(self, name: str, configuration: "Processor.Config"):
120121
super().__init__(name, configuration)
121-
self._rule_tree = RuleTree(config=self._config.tree_config)
122-
self.load_rules(rules_targets=self._config.rules)
122+
self._rule_tree = RuleTree(config=self.config.tree_config)
123+
self.load_rules(rules_targets=self.config.rules)
123124
self._result = None
124125
self._bypass_rule_tree = False
125126
if os.environ.get("LOGPREP_BYPASS_RULE_TREE"):
126127
self._bypass_rule_tree = True
127128
logger.debug("Bypassing rule tree for processor %s", self.name)
128129

130+
@property
131+
def config(self) -> Config:
132+
"""Provides the properly typed rule configuration object"""
133+
return typing.cast("Processor.Config", self._config)
134+
129135
@property
130136
def result(self) -> ProcessorResult:
131137
"""Returns the current result object which is guaranteed to be non-None
@@ -158,7 +164,7 @@ def metric_labels(self) -> dict:
158164
return {
159165
"component": "processor",
160166
"description": self.describe(),
161-
"type": self._config.type,
167+
"type": self.config.type,
162168
"name": self.name,
163169
}
164170

@@ -208,7 +214,7 @@ def _process_rule(rule, event):
208214
return event
209215

210216
def _process_rule_tree_multiple_times(tree: RuleTree, event: dict):
211-
matching_rules = tree.get_matching_rules(event)
217+
matching_rules: Iterable[Rule] = tree.get_matching_rules(event)
212218
while matching_rules:
213219
for rule in matching_rules:
214220
_process_rule(rule, event)
@@ -219,7 +225,7 @@ def _process_rule_tree_once(tree: RuleTree, event: dict):
219225
for rule in matching_rules:
220226
_process_rule(rule, event)
221227

222-
if self._config.apply_multiple_times:
228+
if self.config.apply_multiple_times:
223229
_process_rule_tree_multiple_times(tree, event)
224230
else:
225231
_process_rule_tree_once(tree, event)
@@ -257,7 +263,7 @@ def test_rules(self) -> dict | None:
257263
258264
"""
259265

260-
def load_rules(self, rules_targets: List[str | Dict]) -> None:
266+
def load_rules(self, rules_targets: Sequence[str | dict]) -> None:
261267
"""method to add rules from directories or urls"""
262268
try:
263269
rules = RuleLoader(rules_targets, self.name).rules

logprep/filter/expression/filter_expression.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def does_match(self, document: dict) -> bool:
240240
value = self._get_value(self.key, document)
241241

242242
if isinstance(value, list):
243-
return any(filter(self._matcher.match, (str(val) for val in value)))
243+
return any(self._matcher.match(str(v)) for v in value)
244244

245245
match_result = self._matcher.match(str(value))
246246

@@ -339,6 +339,7 @@ def _normalize_regex(regex: str) -> str:
339339
else:
340340
end_token = "$"
341341
match = RegExFilterExpression.match_parts_pattern.match(regex)
342+
assert match, "regex is designed to always match"
342343
flag, _, pattern = match.groups()
343344
flag = "" if flag is None else flag
344345
pattern = "" if pattern is None else pattern
@@ -348,7 +349,7 @@ def does_match(self, document: dict) -> bool:
348349
value = self._get_value(self.key, document)
349350

350351
if isinstance(value, list):
351-
return any(filter(self._matcher.match, value))
352+
return any(self._matcher.match(str(v)) for v in value)
352353
return self._matcher.match(str(value)) is not None
353354

354355

logprep/filter/lucene_filter.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@
9696
import re
9797
from itertools import chain, zip_longest
9898

99-
# pylint: enable=anomalous-backslash-in-string
100-
from typing import List, Optional, Union
101-
10299
import luqum
103100
from luqum.parser import IllegalCharacterError, ParseSyntaxError, parser
104101
from luqum.tree import (
@@ -128,6 +125,9 @@
128125
StringFilterExpression,
129126
)
130127

128+
# pylint: enable=anomalous-backslash-in-string
129+
130+
131131
logger = logging.getLogger("LuceneFilter")
132132

133133

@@ -143,7 +143,7 @@ class LuceneFilter:
143143
end_escaping_pattern = re.compile(r'((?:\\)+"[\s\)]+(?:AND|OR|NOT|$))')
144144

145145
@staticmethod
146-
def create(query_string: str, special_fields: dict = None) -> FilterExpression:
146+
def create(query_string: str, special_fields: dict | None = None) -> FilterExpression:
147147
"""Create a FilterExpression from a lucene query string.
148148
149149
Parameters
@@ -232,22 +232,22 @@ def _escape_ends_of_expressions(query_string):
232232
class LuceneTransformer:
233233
"""A transformer that converts a luqum tree into a FilterExpression."""
234234

235-
_special_fields_map = {
235+
_special_fields_map: dict[str, type[RegExFilterExpression] | type[SigmaFilterExpression]] = {
236236
"regex_fields": RegExFilterExpression,
237237
"sigma_fields": SigmaFilterExpression,
238238
}
239239

240240
find_unescaping_quote_pattern = re.compile(r'(?:\\)*"')
241241
find_unescaping_end_pattern = re.compile(r"(?:\\)*$")
242242

243-
def __init__(self, tree: luqum.tree, special_fields: dict = None):
243+
def __init__(self, tree: luqum.tree, special_fields: dict | None = None):
244244
self._tree = tree
245245

246246
self._special_fields = {}
247247

248248
special_fields = special_fields if special_fields else {}
249249
for key in self._special_fields_map:
250-
self._special_fields[key] = special_fields.get(key) if special_fields.get(key) else []
250+
self._special_fields[key] = special_fields.get(key, [])
251251

252252
self._last_search_field = None
253253

@@ -308,6 +308,7 @@ def _create_field_group_expression(self, tree: luqum.tree) -> FilterExpression:
308308
Parsed filter expression.
309309
310310
"""
311+
assert self._last_search_field is not None
311312
key = self._last_search_field.split(".")
312313
value = self._strip_quote_from_string(tree.value)
313314
value = self._remove_lucene_escaping(value)
@@ -317,13 +318,13 @@ def _create_field_group_expression(self, tree: luqum.tree) -> FilterExpression:
317318
else:
318319
return self._get_filter_expression(key, value)
319320

320-
def _collect_children(self, tree: luqum.tree) -> List[FilterExpression]:
321+
def _collect_children(self, tree: luqum.tree) -> list[FilterExpression]:
321322
expressions = []
322323
for child in tree.children:
323324
expressions.append(self._parse_tree(child))
324325
return expressions
325326

326-
def _create_field(self, tree: luqum.tree) -> Optional[FilterExpression]:
327+
def _create_field(self, tree: luqum.tree) -> FilterExpression:
327328
if isinstance(tree.expr, (Phrase, Word)):
328329
key = tree.name.replace("\\", "")
329330
key = key.split(".")
@@ -333,7 +334,7 @@ def _create_field(self, tree: luqum.tree) -> Optional[FilterExpression]:
333334
value = self._strip_quote_from_string(tree.expr.value)
334335
value = self._remove_lucene_escaping(value)
335336
return self._get_filter_expression(key, value)
336-
elif isinstance(tree.expr, Regex):
337+
if isinstance(tree.expr, Regex):
337338
key = tree.name.replace("\\", "")
338339
key = key.split(".")
339340
if tree.expr.value == "null":
@@ -342,7 +343,7 @@ def _create_field(self, tree: luqum.tree) -> Optional[FilterExpression]:
342343
value = self._strip_quote_from_string(tree.expr.value)
343344
value = self._remove_lucene_escaping(value)
344345
return self._get_filter_expression_regex(key, value)
345-
return None
346+
raise LuceneFilterError(f'The expression "{str(tree)}" is invalid!')
346347

347348
@staticmethod
348349
def _check_key_and_modifier(key, value):
@@ -353,8 +354,8 @@ def _check_key_and_modifier(key, value):
353354
return None
354355

355356
def _get_filter_expression(
356-
self, key: List[str], value
357-
) -> Union[RegExFilterExpression, StringFilterExpression]:
357+
self, key: list[str], value
358+
) -> RegExFilterExpression | StringFilterExpression | SigmaFilterExpression:
358359

359360
key_and_modifier_check = LuceneTransformer._check_key_and_modifier(key, value)
360361
if key_and_modifier_check is not None:
@@ -376,8 +377,8 @@ def _get_filter_expression(
376377
return StringFilterExpression(key, value)
377378

378379
def _get_filter_expression_regex(
379-
self, key: List[str], value
380-
) -> Union[RegExFilterExpression, StringFilterExpression]:
380+
self, key: list[str], value
381+
) -> RegExFilterExpression | StringFilterExpression:
381382

382383
key_and_modifier_check = LuceneTransformer._check_key_and_modifier(key, value)
383384
if key_and_modifier_check is not None:
@@ -387,7 +388,7 @@ def _get_filter_expression_regex(
387388
return RegExFilterExpression(key, value)
388389

389390
@staticmethod
390-
def _create_value_expression(word: luqum.tree) -> Union[Exists, Always]:
391+
def _create_value_expression(word: luqum.tree) -> Exists | Always:
391392
value = word.value.replace("\\", "")
392393
value = value.split(".")
393394
if value == ["*"]:

logprep/framework/pipeline.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# type: ignore
2-
1+
# mypy: ignore-errors
32
"""This module contains all Pipeline functionality.
43
54
Pipelines contain a list of processors that can be executed in order to process input log data.

logprep/framework/pipeline_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def __init__(self, configuration: Configuration):
166166
self.restart_count: int = 0
167167
self.restart_timeout_ms: int = random.randint(100, 1000)
168168
self.metrics = self.Metrics(labels={"component": "manager"})
169-
self.loghandler: LogprepMPQueueListener = None
169+
self.loghandler: LogprepMPQueueListener | None = None
170170
self.error_queue: multiprocessing.Queue | None = None
171171
self._error_listener: OutputQueueListener | None = None
172172
self._configuration: Configuration = configuration
@@ -246,7 +246,7 @@ def _increase_to_count(self, count: int):
246246
def _decrease_to_count(self, count: int):
247247
while len(self._pipelines) > count:
248248
pipeline_process = self._pipelines.pop()
249-
pipeline_process.stop()
249+
pipeline_process.stop() # type: ignore
250250
pipeline_process.join()
251251
self.metrics.number_of_pipeline_stops += 1
252252

@@ -328,7 +328,7 @@ def _create_pipeline(self, index) -> multiprocessing.Process:
328328
process = multiprocessing.Process(
329329
target=pipeline.run, daemon=True, name=f"Pipeline-{index}"
330330
)
331-
process.stop = pipeline.stop
331+
process.stop = pipeline.stop # type: ignore
332332
process.start()
333333
logger.info("Created new pipeline")
334334
return process

0 commit comments

Comments
 (0)