Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add type hints in util.py #1572

Merged
merged 12 commits into from
Feb 22, 2022
31 changes: 17 additions & 14 deletions cve_bin_tool/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
# SPDX-License-Identifier: GPL-3.0-or-later

""" Utility classes for the CVE Binary Tool """
from __future__ import annotations

import fnmatch
import os
import sys
from collections import defaultdict
from enum import Enum
from typing import NamedTuple
from typing import DefaultDict, Iterator, NamedTuple, Pattern


class OrderedEnum(Enum):
def __ge__(self, other):
def __ge__(self, other: OrderedEnum) -> bool:
if self.__class__ is other.__class__:
return self.value >= other.value
return NotImplemented

def __gt__(self, other):
def __gt__(self, other: OrderedEnum) -> bool:
if self.__class__ is other.__class__:
return self.value > other.value
return NotImplemented

def __le__(self, other):
def __le__(self, other: OrderedEnum) -> bool:
if self.__class__ is other.__class__:
return self.value <= other.value
return NotImplemented

def __lt__(self, other):
def __lt__(self, other: OrderedEnum) -> bool:
if self.__class__ is other.__class__:
return self.value < other.value
return NotImplemented
Expand All @@ -39,7 +40,7 @@ class Remarks(OrderedEnum):
Mitigated = 4, "4", "Mitigated", "m", "M"
Ignored = 5, "5", "Ignored", "i", "I"

def __new__(cls, value, *aliases):
def __new__(cls, value: int, *aliases: str) -> Remarks:
obj = object.__new__(cls)
obj._value_ = value
for alias in aliases:
Expand Down Expand Up @@ -71,18 +72,20 @@ class VersionInfo(NamedTuple):
end_excluding: str


class CVEData(defaultdict):
def __missing__(self, key):
class CVEData(DefaultDict[str, list[CVE] | set[str]]):
rhythmrx9 marked this conversation as resolved.
Show resolved Hide resolved
def __missing__(self, key: str) -> list[CVE] | set[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this one have a return type? I'm not sure if those are needed for __ functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do already use type hints for __ functions, I think we should have them, to be consistent.

if key == "cves":
self[key] = []
new_list: list[CVE] = []
self[key] = new_list
elif key == "paths":
self[key] = set()
new_set: set[str] = set()
self[key] = new_set
else:
return NotImplemented
return self[key]
rhythmrx9 marked this conversation as resolved.
Show resolved Hide resolved


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

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


def inpath(binary) -> bool:
def inpath(binary: str) -> bool:
"""Check to see if something is available in the path.
Used to check if dependencies are installed before use."""
if sys.platform == "win32":
Expand Down Expand Up @@ -147,7 +150,7 @@ def __init__(
self.yield_files = yield_files
self.yield_folders = yield_folders

def walk(self, roots=None):
def walk(self, roots: list[str] | None = None) -> Iterator[str]:
"""Walk the directory looking for files matching the pattern"""
if roots is None:
roots = []
Expand Down