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

perf: only clear assertion _extension when overridden #172

Merged
merged 2 commits into from
Apr 3, 2020
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
16 changes: 11 additions & 5 deletions src/syrupy/assertion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from gettext import gettext
from typing import (
TYPE_CHECKING,
Callable,
Dict,
List,
Optional,
Expand Down Expand Up @@ -44,10 +45,9 @@ class SnapshotAssertion:
_test_location: "TestLocation" = attr.ib(kw_only=True)
_update_snapshots: bool = attr.ib(kw_only=True)
_extension: Optional["AbstractSyrupyExtension"] = attr.ib(init=False, default=None)
_executions: int = attr.ib(init=False, default=0, kw_only=True)
_execution_results: Dict[int, "AssertionResult"] = attr.ib(
init=False, factory=dict, kw_only=True
)
_executions: int = attr.ib(init=False, default=0)
_execution_results: Dict[int, "AssertionResult"] = attr.ib(init=False, factory=dict)
_post_assert_actions: List[Callable[..., None]] = attr.ib(init=False, factory=list)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could use a deque but that seems like premature optimisations


def __attrs_post_init__(self) -> None:
self._session.register_request(self)
Expand Down Expand Up @@ -108,6 +108,11 @@ def __call__(
"""
if extension_class:
self._extension = self.__init_extension(extension_class)

def clear_extension() -> None:
self._extension = None

self._post_assert_actions.append(clear_extension)
return self

def __repr__(self) -> str:
Expand Down Expand Up @@ -155,7 +160,8 @@ def _post_assert(self) -> None:
"""
Restores assertion instance options
"""
self._extension = None
while self._post_assert_actions:
self._post_assert_actions.pop()()

def _recall_data(self, index: int) -> Optional["SerializableData"]:
try:
Expand Down
1 change: 1 addition & 0 deletions tests/test_extension_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ def test_multiple_snapshot_extensions(snapshot):
"""
assert actual_svg == snapshot(extension_class=SVGImageSnapshotExtension)
assert actual_svg == snapshot # uses initial extension class
assert snapshot._extension is not None
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is testing internals. Is there a user facing test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's no user effect only that slight performance drawback of generating the extension instance on each assertion

Copy link
Collaborator

Choose a reason for hiding this comment

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

Bit concerned about coupling the internal implementation with the test cases, but am okay with this as a temporary measure until we have perf benchmarking

assert actual_png == snapshot(extension_class=PNGImageSnapshotExtension)
assert actual_svg == snapshot(extension_class=SVGImageSnapshotExtension)