Skip to content

Commit 2b04fe6

Browse files
rhythmrx9Molkree
andauthored
refactor: add type hints in util.py (#1572)
* related to #1539 * refactor: type hints in util.py * refactor: arguments to new __new__ Co-authored-by: Dmitry Volodin <[email protected]> * refactor: CVEData Co-authored-by: Dmitry Volodin <[email protected]> * refactor: simplifying type hints Co-authored-by: Dmitry Volodin <[email protected]> * refactor: type hints in regex_find Co-authored-by: Dmitry Volodin <[email protected]> * refactor: imports in util.py Co-authored-by: Dmitry Volodin <[email protected]> * refactor: simplifying type hints Co-authored-by: Dmitry Volodin <[email protected]> * refactor: simplifying type hints Co-authored-by: Dmitry Volodin <[email protected]> * refactor: improving type hints in util.py * fix: inline type hints Co-authored-by: Dmitry Volodin <[email protected]> * fix: imports for inline type hints Co-authored-by: Dmitry Volodin <[email protected]>
1 parent ab07fb9 commit 2b04fe6

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

cve_bin_tool/util.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

44
""" Utility classes for the CVE Binary Tool """
5+
from __future__ import annotations
6+
57
import fnmatch
68
import os
79
import sys
8-
from collections import defaultdict
910
from enum import Enum
10-
from typing import NamedTuple
11+
from typing import DefaultDict, Iterator, List, NamedTuple, Pattern, Set, Union
1112

1213

1314
class OrderedEnum(Enum):
14-
def __ge__(self, other):
15+
def __ge__(self, other: OrderedEnum) -> bool:
1516
if self.__class__ is other.__class__:
1617
return self.value >= other.value
1718
return NotImplemented
1819

19-
def __gt__(self, other):
20+
def __gt__(self, other: OrderedEnum) -> bool:
2021
if self.__class__ is other.__class__:
2122
return self.value > other.value
2223
return NotImplemented
2324

24-
def __le__(self, other):
25+
def __le__(self, other: OrderedEnum) -> bool:
2526
if self.__class__ is other.__class__:
2627
return self.value <= other.value
2728
return NotImplemented
2829

29-
def __lt__(self, other):
30+
def __lt__(self, other: OrderedEnum) -> bool:
3031
if self.__class__ is other.__class__:
3132
return self.value < other.value
3233
return NotImplemented
@@ -39,7 +40,7 @@ class Remarks(OrderedEnum):
3940
Mitigated = 4, "4", "Mitigated", "m", "M"
4041
Ignored = 5, "5", "Ignored", "i", "I"
4142

42-
def __new__(cls, value, *aliases):
43+
def __new__(cls, value: int, *aliases: str) -> Remarks:
4344
obj = object.__new__(cls)
4445
obj._value_ = value
4546
for alias in aliases:
@@ -71,18 +72,20 @@ class VersionInfo(NamedTuple):
7172
end_excluding: str
7273

7374

74-
class CVEData(defaultdict):
75-
def __missing__(self, key):
75+
class CVEData(DefaultDict[str, Union[List[CVE], Set[str]]]):
76+
def __missing__(self, key: str) -> list[CVE] | set[str]:
7677
if key == "cves":
77-
self[key] = []
78+
new_list: list[CVE] = []
79+
self[key] = new_list
7880
elif key == "paths":
79-
self[key] = set()
81+
new_set: set[str] = set()
82+
self[key] = new_set
8083
else:
8184
return NotImplemented
8285
return self[key]
8386

8487

85-
def regex_find(lines, version_patterns) -> str:
88+
def regex_find(lines: str, version_patterns: list[Pattern[str]]) -> str:
8689
"""Search a set of lines to find a match for the given regex"""
8790
new_guess = ""
8891

@@ -98,7 +101,7 @@ def regex_find(lines, version_patterns) -> str:
98101
return "UNKNOWN"
99102

100103

101-
def inpath(binary) -> bool:
104+
def inpath(binary: str) -> bool:
102105
"""Check to see if something is available in the path.
103106
Used to check if dependencies are installed before use."""
104107
if sys.platform == "win32":
@@ -147,7 +150,7 @@ def __init__(
147150
self.yield_files = yield_files
148151
self.yield_folders = yield_folders
149152

150-
def walk(self, roots=None):
153+
def walk(self, roots: list[str] | None = None) -> Iterator[str]:
151154
"""Walk the directory looking for files matching the pattern"""
152155
if roots is None:
153156
roots = []

0 commit comments

Comments
 (0)