Skip to content

Commit

Permalink
Fix custom excepcions as kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
quiqueporta committed May 25, 2024
1 parent 476c5a0 commit efe594c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## 3.1.1 (2024-05-10)
## 3.2.0 (2024-05-25)

- Fix custom exceptions to allow to pass them as no kwargs

## 3.1.1 (2024-05-24)

- Add customs exceptions for invariants

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Value Object

![Version number](https://img.shields.io/badge/version-3.1.1-blue.svg) ![License MIT](https://img.shields.io/github/license/quiqueporta/simple-value-object) ![Python Version](https://img.shields.io/badge/python-3.7,_3.8,_3.9,_3.10,3.11,3.12-blue.svg)
![Version number](https://img.shields.io/badge/version-3.2.0-blue.svg) ![License MIT](https://img.shields.io/github/license/quiqueporta/simple-value-object) ![Python Version](https://img.shields.io/badge/python-3.7,_3.8,_3.9,_3.10,3.11,3.12-blue.svg)

Based on Ruby Gem by [NoFlopSquad](https://github.com/noflopsquad/value-object)

Expand Down Expand Up @@ -160,6 +160,10 @@ class Point(ValueObject):
def inside_first_quadrant(self):
return self.x > 0 and self.y > 0, "You must be inside the first quadrant"

@invariant(MyException)
def x_lower_than_y(self):
return self.x < self.y, "X must be lower than Y"

Point(-5, 3)
#MyException: "You must be inside the first quadrant"
```
Expand Down
2 changes: 1 addition & 1 deletion simple_value_object/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .value_object import ValueObject
from .decorators import invariant

VERSION = (3, 1, 1, "final")
VERSION = (3, 2, 0, "final")
__version__ = VERSION


Expand Down
5 changes: 5 additions & 0 deletions simple_value_object/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@


def invariant(func=None, *, exception_type=None):
# Check if func is actually the exception type (when called as @invariant(MyException))
if isinstance(func, type) and issubclass(func, Exception):
exception_type = func
func = None

# Decorator called with parentheses and arguments
if func is None:
return lambda f: invariant(f, exception_type=exception_type)
Expand Down
20 changes: 19 additions & 1 deletion specs/value_object_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def bar(self):

expect(lambda: Foo("buzz")).to(raise_error(MyCustomException))

with it("can return a custome message for the exception"):
with it("can return a custom message for the exception"):

class MyCustomException(Exception):
pass
Expand All @@ -284,3 +284,21 @@ def bar(self):
expect(lambda: Foo("buzz")).to(
raise_error(MyCustomException, "This is a custom message")
)

with it(
"can return a custom message for the exception when passed as no kwargs"
):

class MyCustomException(Exception):
pass

class Foo(ValueObject):
any: str

@invariant(MyCustomException)
def bar(self):
return self.any == "foo", "This is a custom message"

expect(lambda: Foo("buzz")).to(
raise_error(MyCustomException, "This is a custom message")
)

0 comments on commit efe594c

Please sign in to comment.