Skip to content

Commit

Permalink
Merge pull request #167 from janluke/fix/constraint-cannot-be-copied
Browse files Browse the repository at this point in the history
Fix: `Constraint.__getattr__` was returning `None` instead of raising `AttributeError`
  • Loading branch information
janluke committed Aug 23, 2023
2 parents 3ba825d + 3d05061 commit c4a9130
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cloup/constraints/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ def must_check_consistency(ctx: click.Context) -> bool:
def __getattr__(self, attr: str) -> Any:
removed_attrs = ('toggle_consistency_checks', 'consistency_checks_toggled')
if attr in removed_attrs:
raise Exception(
raise AttributeError(
f'attribute `{attr}` was removed in v0.9. You can now enable/disable '
f'consistency checks using the `click.Context` parameter '
f'`check_constraints_consistency`. '
f'Pass it as part of your `context_settings`.'
)
else:
raise AttributeError(attr)

@abc.abstractmethod
def help(self, ctx: click.Context) -> str:
Expand Down
14 changes: 11 additions & 3 deletions tests/constraints/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from cloup.constraints.exceptions import ConstraintViolated, UnsatisfiableConstraint
from tests.util import (
make_context, make_fake_context, make_options, parametrize, should_raise
make_context, make_fake_context, make_options, parametrize, should_raise,
)


Expand All @@ -31,8 +31,10 @@ class FakeConstraint(Constraint):
to create a test double with characteristics of both a mock and a fake."""

def __init__(self, satisfied=True, consistent=True, help='help',
error='error', inconsistency_reason='consistency_error'):
def __init__(
self, satisfied=True, consistent=True, help='help',
error='error', inconsistency_reason='consistency_error'
):
self.satisfied = satisfied
self.consistent = consistent
self._help = help
Expand Down Expand Up @@ -349,3 +351,9 @@ def test_check_consistency_doesnt_raise_if_wrapped_constraint_doesnt_raise(self)
params = make_options('abc')
with pytest.raises(UnsatisfiableConstraint):
rephraser.check_consistency(params)


def test_a_constraint_can_be_copied_and_deep_copied():
import copy
copy.copy(RequireAtLeast(1))
copy.deepcopy(RequireAtLeast(1))

0 comments on commit c4a9130

Please sign in to comment.