Skip to content

Commit 0ffc1a1

Browse files
committed
[fix] Warn user when overwritting numpy attrs (#10)
1 parent 68f866d commit 0ffc1a1

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

mpython/struct.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import warnings
23

34
from .core import DelayedStruct, MatlabType, WrappedArray, _DictMixin
45
from .utils import _copy_if_needed, _empty_array
@@ -461,6 +462,14 @@ def __setattr__(self, key, value):
461462
self._ensure_defaults_are_set()
462463
return
463464
try:
465+
if key in self._NDARRAY_ATTRS:
466+
# SyntaxWarning: overwriting numpy attributes
467+
warnings.warn(
468+
f"Field name '{key}' conflicts with an existing numpy attribute in {type(self).__name__}. "
469+
f"To avoid ambiguity, consider using dictionary-style access: {type(self).__name__}['{key}'] instead of dot notation.",
470+
SyntaxWarning,
471+
stacklevel=2,
472+
)
464473
_DictMixin.__setitem__(self, key, value)
465474
self._ensure_defaults_are_set()
466475
return

0 commit comments

Comments
 (0)