Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __slots__ to ObserverExpression and its subclasses #1515

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions traits/observation/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ObserverExpression:
functions provided in this module, e.g. ``trait``.
"""

__slots__ = ()

def __eq__(self, other):
""" Return true if the other value is an ObserverExpression with
equivalent content.
Expand Down Expand Up @@ -278,6 +280,8 @@ class SingleObserverExpression(ObserverExpression):
""" Container of ObserverExpression for wrapping a single observer.
"""

__slots__ = ("observer",)

def __init__(self, observer):
self.observer = observer

Expand All @@ -298,6 +302,8 @@ class SeriesObserverExpression(ObserverExpression):
Right expression to be joined in series.
"""

__slots__ = ("_first", "_second")

def __init__(self, first, second):
self._first = first
self._second = second
Expand All @@ -318,6 +324,8 @@ class ParallelObserverExpression(ObserverExpression):
Right expression to be joined in parallel.
"""

__slots__ = ("_left", "_right")

def __init__(self, left, right):
self._left = left
self._right = right
Expand Down
16 changes: 16 additions & 0 deletions traits/observation/tests/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,22 @@ def test_equality_different_type(self):
self.assertNotEqual(expr, "1")


class TestObserverExpressionSlots(unittest.TestCase):
""" Check that expressions use __slots__. """

def test_single_expression(self):
expr = create_expression(1)
self.assertFalse(hasattr(expr, "__dict__"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicky question - why not use the same tests as earlier where you check for an exception instead of a different test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. That was this morning; this afternoon, I like this way better. (But not enough better to go back and update all the other tests.)


def test_series_expression(self):
expr = create_expression(1).then(create_expression(2))
self.assertFalse(hasattr(expr, "__dict__"))

def test_parallel_expression(self):
expr = create_expression(1) | create_expression(2)
self.assertFalse(hasattr(expr, "__dict__"))


class TestCompileFromExpr(unittest.TestCase):
""" Tests for compile_expr. """

Expand Down