Skip to content

Commit 49f008e

Browse files
Fix: AttributeArray.__str__ (#55)
* more explicit AttributeArray printing * bump version * add commas for array printing * ruff format
1 parent fe2162a commit 49f008e

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

databpy/array.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,14 @@ def __repr__(self):
177177
obj_name = getattr(self._blender_object, "name", "Unknown")
178178
obj_type = getattr(self._blender_object.data, "name", "Unknown")
179179

180-
# Get array representation
181-
array_repr = np.array_repr(np.asarray(self).view(np.ndarray))
180+
# Get array representation with explicit dtype for cross-platform consistency
181+
# np.array_repr() can omit dtype on Windows when it's the platform default
182+
arr = np.asarray(self).view(np.ndarray)
183+
# Use np.array_repr() but then ensure dtype is always appended
184+
array_repr = np.array_repr(arr)
185+
# If dtype isn't already in the repr, add it before the closing parenthesis
186+
if f"dtype={arr.dtype}" not in array_repr:
187+
array_repr = array_repr.rstrip(")") + f", dtype={arr.dtype})"
182188

183189
return (
184190
f"AttributeArray(name='{attr_name}', object='{obj_name}', mesh='{obj_type}', "

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "databpy"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
description = "A data-oriented wrapper library for the Blender Python API"
55
readme = "README.md"
66
dependencies = ["numpy>=1.24.0,<2.0"]

tests/__snapshots__/test_bob.ambr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
[-1., -1., -1.]], dtype=float32)
1111
# ---
1212
# name: test_bob.1
13-
array([[-1.408998 , -0.18262184, 0.88149434],
14-
[-2.4226575 , -0.23832594, 0.53461844],
15-
[-0.81013453, 0.6320567 , -0.12227035],
16-
[ 1.1924012 , -1.5974301 , -0.14498399],
17-
[ 0.7337191 , -1.6897098 , 0.35846812],
18-
[-1.0602487 , -0.74044144, 1.2087474 ],
19-
[-1.6943052 , 0.18745072, -2.2497435 ],
20-
[ 1.0326254 , 0.02491132, -0.78184235]], dtype=float32)
13+
array([[-2.2497435 , 1.0326254 , 0.02491132],
14+
[-0.78184235, 0.72800225, -0.48501468],
15+
[-0.98779815, -0.15931231, 0.4114268 ],
16+
[-0.7085198 , -0.2979195 , 0.41355893],
17+
[-0.6289273 , -0.9450253 , 0.21435478],
18+
[ 0.6729113 , -0.71147156, -1.161723 ],
19+
[-0.03500854, -0.5114108 , 0.31628448],
20+
[-0.90172744, 0.5529607 , 0.80140704]], dtype=float32)
2121
# ---
2222
# name: test_get_position
2323
array([[ 1., 1., 1.],
@@ -40,12 +40,12 @@
4040
[-1., -1., -1.]], dtype=float32)
4141
# ---
4242
# name: test_set_position.1
43-
array([[-1.0290905 , -2.710574 , -0.04768289],
44-
[-0.6774657 , -0.9472087 , -0.2236253 ],
45-
[-1.5119076 , -1.1819721 , -0.8048682 ],
46-
[-1.1509404 , -0.6400138 , 1.2040595 ],
47-
[-0.69383913, -1.5634813 , 0.9112301 ],
48-
[-0.6464026 , -1.1658763 , -0.93160886],
49-
[ 0.99367285, -0.8332808 , 0.61058164],
50-
[ 0.25490105, -0.7228544 , 0.19398543]], dtype=float32)
43+
array([[ 0.61058164, 0.25490105, -0.7228544 ],
44+
[ 0.19398543, -1.408998 , -0.18262184],
45+
[ 0.88149434, -2.4226575 , -0.23832594],
46+
[ 0.53461844, -0.81013453, 0.6320567 ],
47+
[-0.12227035, 1.1924012 , -1.5974301 ],
48+
[-0.14498399, 0.7337191 , -1.6897098 ],
49+
[ 0.35846812, -1.0602487 , -0.74044144],
50+
[ 1.2087474 , -1.6943052 , 0.18745072]], dtype=float32)
5151
# ---

tests/test_array_print_methods.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,39 @@ def test_str_method_with_large_array(self):
115115
# Should contain numpy's truncation indicator for large arrays
116116
assert "..." in result or len(result.split("\n")) > 1
117117

118+
def test_repr_always_shows_dtype(self, blender_object):
119+
"""Test that __repr__ always explicitly shows dtype for cross-platform consistency.
120+
121+
This is important for snapshot testing across platforms. On Windows, np.array_repr()
122+
may omit dtype when it's the platform default (e.g., int32), while on macOS/Linux
123+
it's always shown. We ensure dtype is always explicit in our repr output.
124+
"""
125+
from databpy.attribute import store_named_attribute
126+
127+
# Get the number of points on the blender object
128+
num_points = len(blender_object.data.vertices)
129+
130+
# Test with int32 (the type that causes platform-dependent repr on Windows)
131+
int_data = np.ones(num_points, dtype=np.int32)
132+
store_named_attribute(blender_object, int_data, "test_int32")
133+
int_arr = AttributeArray(blender_object, "test_int32")
134+
135+
int_repr = repr(int_arr)
136+
# Should contain 'dtype=int32' or 'dtype=int32)' somewhere in output
137+
assert "dtype=int32" in int_repr, (
138+
f"int32 dtype not explicitly shown in repr: {int_repr}"
139+
)
140+
141+
# Test with float32 as well
142+
float_data = np.ones(num_points, dtype=np.float32)
143+
store_named_attribute(blender_object, float_data, "test_float32")
144+
float_arr = AttributeArray(blender_object, "test_float32")
145+
146+
float_repr = repr(float_arr)
147+
assert "dtype=float32" in float_repr, (
148+
f"float32 dtype not explicitly shown in repr: {float_repr}"
149+
)
150+
118151

119152
class TestColumnSlicePrintMethods:
120153
"""Test print behavior of column slice views."""

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)