Skip to content

Commit

Permalink
fix bug: set_enactments_despite tried to access wrong attr
Browse files Browse the repository at this point in the history
  • Loading branch information
mscarey committed Mar 22, 2021
1 parent c434d45 commit 538a6ee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
11 changes: 7 additions & 4 deletions authorityspoke/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ def __init__(
absent: bool = False,
generic: bool = False,
anchors: Optional[List[TextQuoteSelector]] = None,
truth: Optional[bool] = None,
):
Statement.__init__(
self, predicate=predicate, terms=terms, absent=absent, generic=generic
self,
predicate=predicate,
terms=terms,
absent=absent,
generic=generic,
truth=truth,
)
self.standard_of_proof = standard_of_proof
self.anchors = anchors or []
Expand All @@ -95,9 +101,6 @@ def __init__(
raise ValueError(
f"standard of proof must be one of {self.standards_of_proof} or None."
)
super().__init__(
predicate=predicate, terms=terms, name=name, absent=absent, generic=generic
)

@property
def wrapped_string(self):
Expand Down
47 changes: 28 additions & 19 deletions authorityspoke/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,35 +233,42 @@ def generic_factors_by_str(self) -> Dict[str, Comparable]:
return {str(self): self}
return self.procedure.generic_factors_by_str()

def add_enactment(self, incoming: Enactment, role: str = "enactments") -> Rule:
def add_enactment(self, incoming: Enactment) -> Rule:
"""
Make new version of ``self`` with an :class:`.Enactment` added.
:param incoming:
the new :class:`.Enactment` to be added to enactments or
enactments_despite
:param role:
specifies whether the new :class:`.Enactment` should be added
to enactments or enactments_despite
the new :class:`.Enactment` to be added to enactments
:returns:
a new version of ``self`` with the specified change
"""
if role not in self.enactment_attr_names:
raise ValueError(f"'role' must be one of {self.enactment_attr_names}")

if not isinstance(incoming, Enactment):
raise TypeError

new_enactments = list(self.__dict__[role]) + [incoming]
new_enactments = list(self.enactments) + [incoming]
new_enactments = consolidate_enactments(new_enactments)
result = deepcopy(self)
if role == "enactments":
result.set_enactments(new_enactments)
elif role == "enactments_despite":
result.set_enactments_despite(new_enactments)
result.set_enactments(new_enactments)
return result

def add_enactment_despite(self, incoming: Enactment) -> Rule:
r"""
Make new version of ``self`` that applies despite the incoming :class:`.Enactment`\.
:param incoming:
the new :class:`.Enactment` to be added to enactments_despite
:returns:
a new version of ``self`` with the specified change
"""
if not isinstance(incoming, Enactment):
raise TypeError

new_enactments = list(self.enactments_despite) + [incoming]
new_enactments = consolidate_enactments(new_enactments)
result = deepcopy(self)
result.set_enactments_despite(new_enactments)
return result

def add_factor(self, incoming: Factor) -> Rule:
Expand Down Expand Up @@ -576,17 +583,19 @@ def set_despite(self, factors: Sequence[Factor]) -> None:
def set_outputs(self, factors: Sequence[Factor]) -> None:
self.procedure.set_outputs(factors)

def set_enactments(self, enactments: Sequence[Enactment]) -> None:
def set_enactments(self, enactments: Union[Enactment, Sequence[Enactment]]) -> None:
if isinstance(enactments, Enactment):
self.enactments = (enactments,)
else:
self.enactments = tuple(enactments)

def set_enactments_despite(self, enactments: Sequence[Enactment]) -> None:
def set_enactments_despite(
self, enactments: Union[Enactment, Sequence[Enactment]]
) -> None:
if isinstance(enactments, Enactment):
self._enactments_despite = (enactments,)
self.enactments_despite = (enactments,)
else:
self._enactments_despite = tuple(enactments)
self.enactments_despite = tuple(enactments)

def __str__(self):
mandatory = "MUST" if self.mandatory else "MAY"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ anchorpoint>=0.4.4
apispec[validation]~=4.3.0
marshmallow>=3.10
legislice>=0.4.1
nettlesome>=0.3.0
git+git://github.com/mscarey/nettlesome.git@85a5c0c225696085a6a7c75572b1b1679f0bfab6#egg=nettlesome
pint>=0.15
python-dotenv
python-slugify
Expand Down
4 changes: 3 additions & 1 deletion tests/test_opinions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ def test_get_factor_from_opinion(self, make_opinion_with_holding):
def test_factors_by_name(self, make_opinion_with_holding):
oracle = make_opinion_with_holding["oracle_majority"]
factors = oracle.factors_by_name()
factor = factors["false the Java API was an original work"]
factor = factors[
"the fact it was false that <the Java API> was an original work"
]
assert factor.terms[0].name == "the Java API"


Expand Down
8 changes: 3 additions & 5 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ def test_holding_len(self, make_holding):
assert len(make_holding["h1"]) == 2
assert len(make_holding["h3"]) == 5

def test_wrong_role_for_added_enactment(self, e_due_process_14, make_holding):
with pytest.raises(ValueError):
make_holding["h1"].rule.add_enactment(
incoming=e_due_process_14, role="inputs"
)
def test_despite_role_for_added_enactment(self, e_due_process_14, make_holding):
rule = make_holding["h1"].rule.add_enactment_despite(e_due_process_14)
assert "life, liberty, or property" in str(rule).split("DESPITE")[1]


class TestSameMeaning:
Expand Down

0 comments on commit 538a6ee

Please sign in to comment.