Skip to content

MNT: test that table doesn't try to convert unitized data #26953

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

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_table.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import datetime
from unittest.mock import Mock

import numpy as np
import pytest

Expand All @@ -7,6 +10,7 @@
from matplotlib.table import CustomCell, Table
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.transforms import Bbox
import matplotlib.units as munits


def test_non_square():
Expand Down Expand Up @@ -229,3 +233,34 @@ def test_table_bbox(fig_test, fig_ref):
loc='center',
bbox=Bbox.from_extents(0.1, 0.2, 0.9, 0.8)
)


@check_figures_equal(extensions=['png'])
def test_table_unit(fig_test, fig_ref):
# test that table doesn't participate in unit machinery, instead uses repr/str

class FakeUnit:
def __init__(self, thing):
pass
def __repr__(self):
return "Hello"

fake_convertor = munits.ConversionInterface()
# v, u, a = value, unit, axis
fake_convertor.convert = Mock(side_effect=lambda v, u, a: 0)
# not used, here for completeness
fake_convertor.default_units = Mock(side_effect=lambda v, a: None)
fake_convertor.axisinfo = Mock(side_effect=lambda u, a: munits.AxisInfo())

munits.registry[FakeUnit] = fake_convertor

data = [[FakeUnit("yellow"), FakeUnit(42)],
[FakeUnit(datetime.datetime(1968, 8, 1)), FakeUnit(True)]]

fig_test.subplots().table(data)
fig_ref.subplots().table([["Hello", "Hello"], ["Hello", "Hello"]])
fig_test.canvas.draw()
fake_convertor.convert.assert_not_called()

munits.registry.pop(FakeUnit)
assert not munits.registry.get_converter(FakeUnit)