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

Update to reduce type checker noise #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mitreattack/navlayers/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,18 @@ def typeChecker(caller, testee, desired_type, field):
:param caller: the entity that called this function (used for error
messages)
:param testee: the element to test
:param desired_type: the type the element should be
:param desired_type: the type the element should be or a list of
allowed types
:param field: what the element is to be used as (used for error
messages)
:raises BadType: error denoting the testee element is not of the
correct type
"""
if not isinstance(testee, desired_type):
if isinstance(desired_type, list):
if not any(isinstance(testee, t) for t in desired_type):
handler(caller, f"{testee} [{field}] is not one of {str(desired_type)}")
raise BadType
elif not isinstance(testee, desired_type):
handler(caller, f"{testee} [{field}] is not a {str(desired_type)}")
raise BadType

Expand Down
8 changes: 2 additions & 6 deletions mitreattack/navlayers/core/technique.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ def score(self):
@score.setter
def score(self, score):
"""Setter for score."""
try:
typeChecker(type(self).__name__, score, int, "score")
self.__score = score
except BadType:
typeChecker(type(self).__name__, score, float, "score")
self.__score = int(score)
typeChecker(type(self).__name__, score, [int, float], "score")
self.__score = score

@property
def color(self):
Expand Down