Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Dec 31, 2019
1 parent c4b3d16 commit 5e8c580
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
63 changes: 36 additions & 27 deletions openfisca_core/taxscales.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import traceback
from bisect import bisect_left, bisect_right
from sys import getrecursionlimit as recursion_limit
from typing import Any, List, NoReturn, Optional, Union

from numpy import (
Expand Down Expand Up @@ -32,30 +31,43 @@
log = logging.getLogger(__name__)


class EmptyTaxScaleError(IndexError):
class TaxScaleError(Exception):
"""
Exception raised when an operation is run on an empty tax scale (without brackets).
Exception raised for various types of tax scale related issues.
"""

message: str

def __init__(self, message: List[str]) -> None:
stacktrace = os.linesep.join(traceback.format_stack(None, recursion_limit()))
stacktrace = os.linesep.join(traceback.format_stack())
self.message = os.linesep.join([f" {line}" for line in message])
self.message = os.linesep.join([stacktrace, self.message])
super(IndexError, self).__init__(self.message)
Exception.__init__(self, self.message)


class EmptyTaxBaseError(IndexError):
"""Exception raised when an operation is is run with an empty tax base."""
class EmptyTaxScaleError(TaxScaleError):
"""Exception raised when an operation is run on an empty tax scale."""

message: str
def __init__(self, class_name: str, method_name: str, thresholds: ndarray) -> None:
message = [
f"Method '{method_name}' can't be run on an empty "
f"{class_name}:\n",
">>> self.thresholds",
f"{thresholds}",
]
TaxScaleError.__init__(self, message)

def __init__(self, message: List[str]) -> None:
stacktrace = os.linesep.join(traceback.format_stack(None, recursion_limit()))
self.message = os.linesep.join([f" {line}" for line in message])
self.message = os.linesep.join([stacktrace, self.message])
super(IndexError, self).__init__(message)

class EmptyArgumentError(TaxScaleError):
"""Exception raised when an operation is is run with an empty argument."""

def __init__(self, method_name: str, arg_name: str, arg_value: ndarray) -> None:
message = [
f"Method '{method_name}' can't be run with an empty '{arg_name}':\n",
f">>> {arg_name}",
f"{arg_value}",
]
TaxScaleError.__init__(self, message)


class AbstractTaxScale:
Expand Down Expand Up @@ -144,7 +156,8 @@ class AbstractRateTaxScale(AbstractTaxScale):
rates: List

def __init__(self, name: Optional[str] = None, option = None, unit = None) -> None:
super(AbstractRateTaxScale, self).__init__(
AbstractTaxScale.__init__(
self,
name = name,
option = option,
unit = unit,
Expand Down Expand Up @@ -261,21 +274,16 @@ def bracket_indices(

if not size(array(self.thresholds)):
raise EmptyTaxScaleError(
[
"Method 'bracket_indices' can't be run on an empty "
f"{self.__class__.__name__}:\n",
">>> self.thresholds",
f"{self.thresholds}",
],
self.__class__.__name__,
"bracket_indices",
self.thresholds,
)

if not size(array(tax_base)):
raise EmptyTaxBaseError(
[
"Method 'bracket_indices' can't be run with an empty tax base:\n",
">>> tax_base",
f"{tax_base}",
],
raise EmptyArgumentError(
"bracket_indices",
"tax_base",
tax_base,
)

base1 = tile(tax_base, (len(self.thresholds), 1)).T
Expand Down Expand Up @@ -305,7 +313,8 @@ class SingleAmountTaxScale(AbstractTaxScale):
amounts: List

def __init__(self, name: Optional[str] = None, option = None, unit = None) -> None:
super(SingleAmountTaxScale, self).__init__(
AbstractTaxScale.__init__(
self,
name = name,
option = option,
unit = unit,
Expand Down
4 changes: 2 additions & 2 deletions tests/core/tax_scales/test_abstract_rate_tax_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from openfisca_core.taxscales import (
AbstractRateTaxScale,
EmptyTaxBaseError,
EmptyArgumentError,
EmptyTaxScaleError,
)
from openfisca_core.tools import assert_near
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_bracket_indices_without_tax_base():
tax_scale.add_bracket(2, 0)
tax_scale.add_bracket(4, 0)

with pytest.raises(EmptyTaxBaseError):
with pytest.raises(EmptyArgumentError):
tax_scale.bracket_indices(tax_base)


Expand Down

0 comments on commit 5e8c580

Please sign in to comment.