Skip to content

Commit 5086958

Browse files
timopollmeiergreenbonebot
authored andcommitted
Fix mypy type hint errors, rename loop functions
Fixed several errors found by the type hint linter mypy and added underscore prefix for the callback functions _loop_step_end and _loop_end.
1 parent 9d9d856 commit 5086958

File tree

14 files changed

+84
-56
lines changed

14 files changed

+84
-56
lines changed

greenbone/scap/cpe/cli/download.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,14 @@ async def download(console: Console, error_console: Console) -> None:
424424
)
425425

426426
if run_time_file:
427+
if until:
428+
run_time = until
429+
else:
430+
run_time = datetime.now()
427431
# ensure directories exist
428432
run_time_file.parent.mkdir(parents=True, exist_ok=True)
429433
run_time_file.write_text(
430-
f"{until.isoformat()}\n",
434+
f"{run_time.isoformat()}\n",
431435
encoding="utf8", # type: ignore
432436
)
433437
console.log(f"Wrote run time to {run_time_file.absolute()}.")

greenbone/scap/cpe_match/db/manager.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def add_cpe_match_strings(
107107
)
108108
await self._insert_foreign_data(transaction, match_strings)
109109

110-
self._cpes = []
110+
self._cpe_match_strings = []
111111

112112
async def _insert_foreign_data(
113113
self,
@@ -150,8 +150,8 @@ async def find(
150150
index: int | None = None,
151151
last_modification_start_date: datetime | None = None,
152152
last_modification_end_date: datetime | None = None,
153-
published_start_date: datetime | None = None,
154-
published_end_date: datetime | None = None,
153+
created_start_date: datetime | None = None,
154+
created_end_date: datetime | None = None,
155155
) -> AsyncIterator[CPEMatchStringDatabaseModel]:
156156
clauses = []
157157

@@ -171,13 +171,13 @@ async def find(
171171
CPEMatchStringDatabaseModel.last_modified
172172
<= last_modification_end_date
173173
)
174-
if published_start_date:
174+
if created_start_date:
175175
clauses.append(
176-
CPEMatchStringDatabaseModel.published >= published_start_date
176+
CPEMatchStringDatabaseModel.created >= created_start_date
177177
)
178-
if published_end_date:
178+
if created_end_date:
179179
clauses.append(
180-
CPEMatchStringDatabaseModel.published <= published_end_date
180+
CPEMatchStringDatabaseModel.created <= created_end_date
181181
)
182182

183183
statement = (
@@ -221,8 +221,8 @@ async def count(
221221
match_criteria_id: str | None,
222222
last_modification_start_date: datetime | None = None,
223223
last_modification_end_date: datetime | None = None,
224-
published_start_date: datetime | None = None,
225-
published_end_date: datetime | None = None,
224+
created_start_date: datetime | None = None,
225+
created_end_date: datetime | None = None,
226226
) -> int:
227227
clauses = []
228228

@@ -242,13 +242,13 @@ async def count(
242242
CPEMatchStringDatabaseModel.last_modified
243243
<= last_modification_end_date
244244
)
245-
if published_start_date:
245+
if created_start_date:
246246
clauses.append(
247-
CPEMatchStringDatabaseModel.published >= published_start_date
247+
CPEMatchStringDatabaseModel.created >= created_start_date
248248
)
249-
if published_end_date:
249+
if created_end_date:
250250
clauses.append(
251-
CPEMatchStringDatabaseModel.published <= published_end_date
251+
CPEMatchStringDatabaseModel.created <= created_end_date
252252
)
253253

254254
statement = select(

greenbone/scap/cpe_match/json.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from dataclasses import asdict, dataclass
88
from datetime import datetime
99
from pathlib import Path
10+
from typing import Sequence
1011

1112
from pontos.nvd.models.cpe_match_string import CPEMatchString
1213
from rich.console import Console
@@ -65,7 +66,7 @@ def __init__(
6566
storage_path: Path,
6667
*,
6768
compress: bool = False,
68-
schema_path: Path = None,
69+
schema_path: Path | None = None,
6970
raise_error_on_validation=False,
7071
):
7172
super().__init__(
@@ -88,7 +89,9 @@ def add_match_string(self, match_string: CPEMatchString) -> None:
8889
MatchStringItem(match_string=match_string)
8990
)
9091

91-
def add_match_strings(self, match_strings: list[CPEMatchString]) -> None:
92+
def add_match_strings(
93+
self, match_strings: Sequence[CPEMatchString]
94+
) -> None:
9295
for match_string in match_strings:
9396
self.add_match_string(match_string)
9497

greenbone/scap/cpe_match/producer/nvd_api.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from rich.console import Console
1010
from rich.progress import Progress
1111

12+
from ...cli import DEFAULT_RETRIES
13+
from ...errors import ScapError
1214
from ...generic_cli.producer.nvd_api import NvdApiProducer
1315
from ..cli.processor import CPE_MATCH_TYPE_PLURAL
1416

@@ -69,12 +71,12 @@ def __init__(
6971
error_console: Console,
7072
progress: Progress,
7173
*,
74+
retry_attempts: int = DEFAULT_RETRIES,
7275
nvd_api_key: str | None = None,
73-
retry_attempts: int = None,
74-
request_results: int = None,
76+
request_results: int | None = None,
7577
request_filter_opts: dict = {},
7678
start_index: int = 0,
77-
verbose: int = None,
79+
verbose: int | None = None,
7880
):
7981
"""
8082
Constructor for a CPE match string NVD API producer.
@@ -90,19 +92,21 @@ def __init__(
9092
start_index: index/offset of the first item to request
9193
verbose: Verbosity level of log messages.
9294
"""
95+
self._nvd_api: CPEMatchApi
96+
9397
super().__init__(
9498
console,
9599
error_console,
96100
progress,
97-
nvd_api_key=nvd_api_key,
98101
retry_attempts=retry_attempts,
102+
nvd_api_key=nvd_api_key,
99103
request_results=request_results,
100104
request_filter_opts=request_filter_opts,
101105
start_index=start_index,
102106
verbose=verbose,
103107
)
104108

105-
def _create_nvd_api(self, nvd_api_key: str) -> CPEMatchApi:
109+
def _create_nvd_api(self, nvd_api_key: str | None) -> CPEMatchApi:
106110
"""
107111
Callback used by the constructor to create the NVD API object
108112
that can be queried for CPE match strings.
@@ -123,6 +127,9 @@ async def _create_nvd_results(self) -> NVDResults[CPEMatchString]:
123127
124128
Returns: The new `NVDResults` object.
125129
"""
130+
if self._queue is None:
131+
raise ScapError("No queue has been assigned")
132+
126133
return await self._nvd_api.cpe_matches(
127134
last_modified_start_date=self._request_filter_opts.get(
128135
"last_modified_start_date"

greenbone/scap/cpe_match/worker/db.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def __init__(
9292
echo_sql: Whether to print SQL statements.
9393
verbose: Verbosity level of log messages.
9494
"""
95+
self._manager: CPEMatchStringDatabaseManager
96+
9597
super().__init__(
9698
console,
9799
error_console,

greenbone/scap/cpe_match/worker/json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ async def _handle_chunk(self, chunk: Sequence[CPEMatchString]):
111111
"""
112112
self._json_manager.add_match_strings(chunk)
113113

114-
async def loop_end(self) -> None:
114+
async def _loop_end(self) -> None:
115115
"""
116116
Callback handling the exiting the main worker loop.
117117
118118
Makes the JSON manager write the document to the file.
119119
"""
120120
self._json_manager.write()
121-
await super().loop_end()
121+
await super()._loop_end()

greenbone/scap/cve/cli/download.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,14 @@ async def download(console: Console, error_console: Console):
432432
)
433433

434434
if run_time_file:
435+
if until:
436+
run_time = until
437+
else:
438+
run_time = datetime.now()
435439
# ensure directories exist
436440
run_time_file.parent.mkdir(parents=True, exist_ok=True)
437441
run_time_file.write_text(
438-
f"{until.isoformat()}\n",
442+
f"{run_time.isoformat()}\n",
439443
encoding="utf8", # type: ignore
440444
)
441445
console.log(f"Wrote run time to {run_time_file.absolute()}.")

greenbone/scap/data_utils/json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class JsonManager:
123123
def __init__(
124124
self,
125125
error_console: Console,
126-
schema_path: Path = None,
126+
schema_path: Path | None = None,
127127
raise_error_on_validation=False,
128128
):
129129
"""

greenbone/scap/generic_cli/processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __init__(
118118
self._worker.set_queue(self._queue)
119119

120120
self._verbose: int = (
121-
verbose if not None else self._arg_defaults["verbose"],
121+
verbose if verbose is not None else self._arg_defaults["verbose"]
122122
)
123123
"Verbosity level of log messages."
124124

greenbone/scap/generic_cli/producer/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from abc import ABC, abstractmethod
66
from argparse import ArgumentParser
7-
from typing import AsyncContextManager, Generic, TypeVar
7+
from typing import Any, AsyncContextManager, Generic, TypeVar
88

99
from rich.console import Console
1010
from rich.progress import Progress
@@ -30,10 +30,10 @@ class BaseScapProducer(Generic[T], AsyncContextManager, ABC):
3030
e.g. `BaseScapProducer[CPE]` will be a producer handling CPE objects.
3131
"""
3232

33-
_item_type_plural = "SCAP items"
33+
_item_type_plural: str = "SCAP items"
3434
"Plural form of the type of items to use in log messages"
3535

36-
_arg_defaults = {
36+
_arg_defaults: dict[str, Any] = {
3737
"verbose": DEFAULT_VERBOSITY,
3838
}
3939
"Default values for optional arguments."
@@ -79,7 +79,7 @@ def __init__(
7979
self._verbose = verbose if not None else self._arg_defaults["verbose"]
8080
"Verbosity level of log messages."
8181

82-
self._queue: ScapChunkQueue[T] | None = None
82+
self._queue: ScapChunkQueue[T]
8383
"Queue chunks of SCAP items are added to."
8484

8585
@abstractmethod
@@ -110,7 +110,7 @@ async def run_loop(
110110
It should also create a task for the `progress` object and update it
111111
regularly.
112112
"""
113-
self._queue.set_producer_finished()
113+
pass
114114

115115
def set_queue(self, queue: ScapChunkQueue[T]) -> None:
116116
"""

0 commit comments

Comments
 (0)