Skip to content

Commit 4edcd1f

Browse files
committed
Deprecate passing floats to RendererAgg.draw_text_image
These were silently truncated to int anyway, so we should make the types explicit.
1 parent 0832ad6 commit 4edcd1f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Passing floating-point values to ``RendererAgg.draw_text_image``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Any floating-point values passed to the *x* and *y* parameters were truncated to integers
5+
silently. This behaviour is now deprecated, and only `int` values should be used.

src/_backend_agg_wrapper.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,37 @@ PyRendererAgg_draw_path(RendererAgg *self,
6060
static void
6161
PyRendererAgg_draw_text_image(RendererAgg *self,
6262
py::array_t<agg::int8u, py::array::c_style | py::array::forcecast> image_obj,
63-
double x,
64-
double y,
63+
std::variant<double, int> vx,
64+
std::variant<double, int> vy,
6565
double angle,
6666
GCAgg &gc)
6767
{
68+
int x, y;
69+
70+
if (auto value = std::get_if<double>(&vx)) {
71+
auto api = py::module_::import("matplotlib._api");
72+
auto warn = api.attr("warn_deprecated");
73+
warn("since"_a="3.10", "name"_a="x", "obj_type"_a="parameter as float",
74+
"alternative"_a="int(x)");
75+
x = static_cast<int>(*value);
76+
} else if (auto value = std::get_if<int>(&vx)) {
77+
x = *value;
78+
} else {
79+
throw std::runtime_error("Should not happen");
80+
}
81+
82+
if (auto value = std::get_if<double>(&vy)) {
83+
auto api = py::module_::import("matplotlib._api");
84+
auto warn = api.attr("warn_deprecated");
85+
warn("since"_a="3.10", "name"_a="y", "obj_type"_a="parameter as float",
86+
"alternative"_a="int(y)");
87+
y = static_cast<int>(*value);
88+
} else if (auto value = std::get_if<int>(&vy)) {
89+
y = *value;
90+
} else {
91+
throw std::runtime_error("Should not happen");
92+
}
93+
6894
// TODO: This really shouldn't be mutable, but Agg's renderer buffers aren't const.
6995
auto image = image_obj.mutable_unchecked<2>();
7096

0 commit comments

Comments
 (0)