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

Adjust tests: don't assert on GetAttrKey, catch and check warnings. #48

Merged
merged 1 commit into from
Nov 21, 2024
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
7 changes: 6 additions & 1 deletion tests/fixtures/treescope_examples_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def __call__(self, value: int) -> int:
some_callable_block = SomethingCallable()


@dataclasses.dataclass(frozen=True)
class FrozenDataclassKey:
name: str


@jax.tree_util.register_pytree_with_keys_class
class UnknownPytreeNode:
"""A Pytree node treescope doesn't know."""
Expand All @@ -145,7 +150,7 @@ def __repr__(self):

def tree_flatten_with_keys(self):
return (
((jax.tree_util.GetAttrKey("x"), self.x), ("custom_key", self.y)),
((FrozenDataclassKey("x"), self.x), ("string_key", self.y)),
"example_pytree_aux_data",
)

Expand Down
50 changes: 37 additions & 13 deletions tests/renderer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import collections
import dataclasses
import functools
import re
import textwrap
import types
from typing import Any, Callable
Expand Down Expand Up @@ -553,11 +554,16 @@ def test_object_rendering(

renderer = treescope.active_renderer.get()
# Render it to IR.
rendering = rendering_parts.build_full_line_with_annotations(
renderer.to_foldable_representation(
target, ignore_exceptions=ignore_exceptions
)
)
with warnings.catch_warnings():
if ignore_exceptions:
# Also ignore warnings due to ignoring exceptions to avoid cluttering
# logs.
warnings.simplefilter("ignore")
rendering = rendering_parts.build_full_line_with_annotations(
renderer.to_foldable_representation(
target, ignore_exceptions=ignore_exceptions
)
)

# Collapse all foldables.
layout_algorithms.expand_to_depth(rendering, 0)
Expand Down Expand Up @@ -676,8 +682,8 @@ def test_fallback_repr_pytree_node(self):
<custom repr for UnknownPytreeNode: x=1234, y=5678>
#╭┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄╮
# PyTree children:
GetAttrKey(name='x'): 1234,
'custom_key': 5678,
FrozenDataclassKey(name='x'): 1234,
'string_key': 5678,
#╰┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄╯
, # {object.__repr__(target[0])}
]"""),
Expand Down Expand Up @@ -712,9 +718,19 @@ def test_fallback_repr_after_error(self):
):
renderer.to_foldable_representation(target)

rendering = rendering_parts.build_full_line_with_annotations(
renderer.to_foldable_representation(target, ignore_exceptions=True)
)
with self.assertWarnsRegex(
UserWarning,
"(.|\n)*".join([
re.escape("Ignoring error while formatting value of type"),
re.escape("ObjectWithCustomHandlerThatThrows"),
re.escape(
'raise RuntimeError("Simulated treescope_repr failure!")'
),
]),
):
rendering = rendering_parts.build_full_line_with_annotations(
renderer.to_foldable_representation(target, ignore_exceptions=True)
)

layout_algorithms.expand_to_depth(rendering, 0)
self.assertEqual(
Expand Down Expand Up @@ -842,9 +858,17 @@ def test_failsafe_for_throw_in_repr(self):
):
renderer.to_foldable_representation(target)

rendering = rendering_parts.build_full_line_with_annotations(
renderer.to_foldable_representation(target, ignore_exceptions=True)
)
with self.assertWarnsRegex(
UserWarning,
"(.|\n)*".join([
re.escape("Ignoring error while formatting value of type"),
re.escape("ObjectWithReprThatThrows"),
re.escape('raise RuntimeError("Simulated repr failure!")'),
]),
):
rendering = rendering_parts.build_full_line_with_annotations(
renderer.to_foldable_representation(target, ignore_exceptions=True)
)

layout_algorithms.expand_to_depth(rendering, 0)
self.assertEqual(
Expand Down
Loading