Skip to content

Commit

Permalink
fix(CVSS2, CVSS3, CVSS4): implement and correct __eq__ methods (#62)
Browse files Browse the repository at this point in the history
* fix(CVSS4): implement missing __eq__ method

* fix(CVSS2, CVSS3): correct __eq__ methods for consistency

* fix(CVSS2, CVSS3, CVSS4): 🩹 remove type hints from magic methods

Removed type hints from the __eq__ and __hash__ magic methods in CVSS2, CVSS3 and CVSS4 classes for better compatibility with < Python3.5 versions
  • Loading branch information
fqlenos authored Sep 16, 2024
1 parent 4c0367a commit 5e83877
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
16 changes: 8 additions & 8 deletions cvss/cvss2.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,6 @@ def rh_vector(self):
"""
return str(self.scores()[0]) + "/" + self.clean_vector()

def __eq__(self, o):
if isinstance(o, CVSS2):
return self.clean_vector().__eq__(o.clean_vector())
return NotImplemented

def __hash__(self):
return hash(self.clean_vector())

def as_json(self, sort=False, minimal=False):
"""
Returns a dictionary formatted with attribute names and values defined by the official
Expand Down Expand Up @@ -381,3 +373,11 @@ def add_metric_to_data(metric):
data = OrderedDict(sorted(data.items()))

return data

def __hash__(self):
return hash(self.clean_vector())

def __eq__(self, o):
if isinstance(o, CVSS2):
return self.clean_vector() == o.clean_vector()
return False
16 changes: 8 additions & 8 deletions cvss/cvss3.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,6 @@ def rh_vector(self):
"""
return str(self.scores()[0]) + "/" + self.clean_vector()

def __eq__(self, o):
if isinstance(o, CVSS3):
return self.clean_vector().__eq__(o.clean_vector())
return NotImplemented

def __hash__(self):
return hash(self.clean_vector())

def as_json(self, sort=False, minimal=False):
"""
Returns a dictionary formatted with attribute names and values defined by the official
Expand Down Expand Up @@ -508,3 +500,11 @@ def add_metric_to_data(metric):
if sort:
data = OrderedDict(sorted(data.items()))
return data

def __hash__(self):
return hash(self.clean_vector())

def __eq__(self, o):
if isinstance(o, CVSS3):
return self.clean_vector() == o.clean_vector()
return False
11 changes: 8 additions & 3 deletions cvss/cvss4.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,6 @@ def compute_base_score(self):

self.base_score = round_away_from_zero(value)

def __hash__(self):
return hash(self.clean_vector())

def clean_vector(self, output_prefix=True):
"""
Returns vector without optional metrics marked as X and in preferred order.
Expand Down Expand Up @@ -670,3 +667,11 @@ def add_metric_to_data(metric):
if sort:
data = OrderedDict(sorted(data.items()))
return data

def __hash__(self):
return hash(self.clean_vector())

def __eq__(self, o):
if isinstance(o, CVSS4):
return self.clean_vector() == o.clean_vector()
return False

0 comments on commit 5e83877

Please sign in to comment.