Skip to content

Commit 05180e8

Browse files
committed
improve CollisionError string representations
1 parent 2714e32 commit 05180e8

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

lambda_calculus/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .terms import Variable, Abstraction, Application
66

7-
__version__ = "2.0.0"
7+
__version__ = "2.0.1"
88
__author__ = "Eric Niklas Wolf"
99
__email__ = "[email protected]"
1010
__all__ = (

lambda_calculus/errors.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ def __init__(self, message: str, collisions: Collection[V]) -> None:
2828
self.collisions = collisions
2929

3030
def __repr__(self) -> str:
31-
return f"{self.__class__.__name__}({self.message!r}, {self.collisions!r})"
31+
return (
32+
f"{self.__class__.__module__}.{self.__class__.__qualname__}"
33+
f"({self.message!r}, {self.collisions!r})"
34+
)
3235

3336
def __str__(self) -> str:
3437
collisions = ", ".join(map(str, self.collisions))
35-
return f"[collisions: {collisions}] {self.message}"
38+
return f"[collisions: {collisions or 'none'}] {self.message}"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "lambda_calculus"
3-
version = "2.0.0"
3+
version = "2.0.1"
44
description = "Implementation of the Lambda calculus"
55
requires-python = ">=3.10"
66
keywords = []

tests/test_errors.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
3+
"""Tests for custom errors"""
4+
5+
from unittest import TestCase
6+
from lambda_calculus import errors
7+
8+
9+
class CollisionErrorTest(TestCase):
10+
"""Tests for Exception thrown when a variable already exists"""
11+
12+
def test_exception(self) -> None:
13+
"""test exception subclass"""
14+
exception: Exception = errors.CollisionError("test", [])
15+
self.assertIsInstance(exception, ValueError)
16+
self.assertEqual(exception.args, ("test", []))
17+
18+
def test_repr(self) -> None:
19+
"""test string representation"""
20+
exception: Exception = errors.CollisionError("test", [])
21+
self.assertEqual(
22+
repr(exception),
23+
f"lambda_calculus.errors.CollisionError({'test'!r}, {[]!r})"
24+
)
25+
26+
def test_str(self) -> None:
27+
"""test exception message"""
28+
self.assertEqual(
29+
str(errors.CollisionError("test", [])),
30+
"[collisions: none] test"
31+
)
32+
self.assertEqual(
33+
str(errors.CollisionError("test", range(3))),
34+
"[collisions: 0, 1, 2] test"
35+
)

0 commit comments

Comments
 (0)