From d3db2ee004cb377145a3dcd48d54f96f38186303 Mon Sep 17 00:00:00 2001 From: Daniel Johnson Date: Thu, 21 Nov 2024 11:21:00 -0500 Subject: [PATCH] Enable pyink on tests, fix VSCode pyink config, fix lint. Turns on checking pyink formatting for test files. Also disables a bad Python version for pyink: Black and Pyink do not support python 3.12.5, but uvx automatically selects this version occasionally, which breaks autoformatting in VSCode. Also fixes remaining lint issues. --- .github/workflows/unittests.yml | 2 +- .vscode/settings.json | 2 +- tests/fixtures/treescope_examples_fixture.py | 1 + tests/representation_test.py | 4 +++- treescope/_internal/api/arrayviz.py | 6 +++--- .../_internal/handlers/shared_value_postprocessor.py | 8 ++++++-- treescope/_internal/html_encapsulation.py | 6 +++--- treescope/_internal/parts/basic_parts.py | 3 ++- treescope/external/jax_support.py | 2 ++ 9 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index 1c3d6f6..fe59d1e 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -42,7 +42,7 @@ jobs: # Check formatting - name: Check pyink formatting - run: uv run pyink treescope --check + run: uv run pyink treescope tests --check - name: Run pylint run: uv run pylint treescope diff --git a/.vscode/settings.json b/.vscode/settings.json index 3307852..5036144 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,6 +24,6 @@ "editor.detectIndentation": false, "editor.defaultFormatter": "ms-python.black-formatter", }, - "black-formatter.path": ["uvx", "pyink"], + "black-formatter.path": ["uvx", "--python=>=3.9,!=3.12.5", "pyink"], "pylint.enabled": true, } diff --git a/tests/fixtures/treescope_examples_fixture.py b/tests/fixtures/treescope_examples_fixture.py index 70627a7..8000080 100644 --- a/tests/fixtures/treescope_examples_fixture.py +++ b/tests/fixtures/treescope_examples_fixture.py @@ -206,6 +206,7 @@ class ObjectWithCustomHandlerThatThrowsDeferred: def __treescope_repr__(self, path, subtree_renderer): del path, subtree_renderer + def _internal_main_thunk(layout_decision): del layout_decision raise RuntimeError("Simulated deferred treescope_repr failure!") diff --git a/tests/representation_test.py b/tests/representation_test.py index 8ddfc60..c22cc6a 100644 --- a/tests/representation_test.py +++ b/tests/representation_test.py @@ -156,7 +156,9 @@ class RepresentationPartsTest(parameterized.TestCase): expected_text_roundtrip_collapsed='some text with characters < > &', expected_text_roundtrip_expanded='some text with characters < > &', expected_html='some text with characters < > &', - expected_html_at_beginning='some text with characters < > &', + expected_html_at_beginning=( + 'some text with characters < > &' + ), ), dict( testcase_name='siblings', diff --git a/treescope/_internal/api/arrayviz.py b/treescope/_internal/api/arrayviz.py index 08e13c0..4afcce2 100644 --- a/treescope/_internal/api/arrayviz.py +++ b/treescope/_internal/api/arrayviz.py @@ -223,9 +223,9 @@ def render_array( something like ``palettable.matplotlib.Inferno_20.colors``. truncate: Whether or not to truncate the array to a smaller size before rendering. - maximum_size: Maximum number of elements of an array to show. Arrays larger - than this will be truncated along one or more axes. Ignored unless - ``truncate`` is True. + maximum_size: Maximum number of elements of an array to show. Arrays + larger than this will be truncated along one or more axes. Ignored + unless ``truncate`` is True. cutoff_size_per_axis: Maximum number of elements of each individual axis to show without truncation. Any axis longer than this will be truncated, with their visual size increasing logarithmically with the true axis diff --git a/treescope/_internal/handlers/shared_value_postprocessor.py b/treescope/_internal/handlers/shared_value_postprocessor.py index 6c8ba3e..067a819 100644 --- a/treescope/_internal/handlers/shared_value_postprocessor.py +++ b/treescope/_internal/handlers/shared_value_postprocessor.py @@ -215,9 +215,13 @@ def html_setup_parts( {{ padding-right: 1ch; margin-right: -0.5ch; - background: linear-gradient(135deg, orange 0 0.6ch, transparent 0.6ch ); + background: linear-gradient( + 135deg, orange 0 0.6ch, transparent 0.6ch + ); }} - .shared_warning_pip.is_first_on_line:not({setup_context.collapsed_selector} *) + .shared_warning_pip.is_first_on_line:not( + {setup_context.collapsed_selector} * + ) {{ margin-left: -0.5ch; }} diff --git a/treescope/_internal/html_encapsulation.py b/treescope/_internal/html_encapsulation.py index 9d03dc7..49e3b71 100644 --- a/treescope/_internal/html_encapsulation.py +++ b/treescope/_internal/html_encapsulation.py @@ -356,9 +356,9 @@ def encapsulate_streaming_html( stream = io.StringIO() if stealable: - stealer_content = _prep_html_js_and_strip_comments(STEALER_TEMPLATE).replace( - "{__REPLACE_ME_WITH_CONTAINER_ID_CLASS__}", unique_id_class - ) + stealer_content = _prep_html_js_and_strip_comments( + STEALER_TEMPLATE + ).replace("{__REPLACE_ME_WITH_CONTAINER_ID_CLASS__}", unique_id_class) yield HTMLOutputSegment( html_src=stealer_content, segment_type=SegmentType.FINAL_OUTPUT_STEALER, diff --git a/treescope/_internal/parts/basic_parts.py b/treescope/_internal/parts/basic_parts.py index e4c371e..e563740 100644 --- a/treescope/_internal/parts/basic_parts.py +++ b/treescope/_internal/parts/basic_parts.py @@ -17,6 +17,7 @@ from __future__ import annotations import abc +from collections.abc import Hashable import dataclasses import functools import html @@ -24,7 +25,7 @@ import itertools import operator import typing -from typing import Any, Hashable, Sequence +from typing import Any, Sequence from treescope._internal import html_escaping from treescope._internal.parts import part_interface diff --git a/treescope/external/jax_support.py b/treescope/external/jax_support.py index b1c7b1c..0447fb0 100644 --- a/treescope/external/jax_support.py +++ b/treescope/external/jax_support.py @@ -409,12 +409,14 @@ def _summarize_array_data_unconditionally(array: jax.Array) -> list[str]: stat = compute_summary(array, is_floating, is_integer, is_bool) # Get values in parallel. stat = jax.device_get(stat) + # pylint: disable=inconsistent-quotes if is_floating and stat["any_finite"]: output_parts.append(f" ≈{stat['mean']:.2} ±{stat['std']:.2}") output_parts.append(f" [≥{stat['nanmin']:.2}, ≤{stat['nanmax']:.2}]") if is_integer: output_parts.append(f" [≥{stat['min']:_d}, ≤{stat['max']:_d}]") + # pylint: enable=inconsistent-quotes def append_if_present(output_parts, *names): for name in names: