@@ -369,9 +369,6 @@ def _get_format_function(data, **options):
369
369
find the right formatting function for the dtype_
370
370
"""
371
371
dtype_ = data .dtype
372
- if dtype_ .fields is not None :
373
- return StructureFormat .from_data (data , ** options )
374
-
375
372
dtypeobj = dtype_ .type
376
373
formatdict = _get_formatdict (data , ** options )
377
374
if issubclass (dtypeobj , _nt .bool_ ):
@@ -398,7 +395,10 @@ def _get_format_function(data, **options):
398
395
elif issubclass (dtypeobj , _nt .object_ ):
399
396
return formatdict ['object' ]()
400
397
elif issubclass (dtypeobj , _nt .void ):
401
- return formatdict ['void' ]()
398
+ if dtype_ .names is not None :
399
+ return StructuredVoidFormat .from_data (data , ** options )
400
+ else :
401
+ return formatdict ['void' ]()
402
402
else :
403
403
return formatdict ['numpystr' ]()
404
404
@@ -1200,14 +1200,21 @@ def __call__(self, arr):
1200
1200
return "[" + ", " .join (self .__call__ (a ) for a in arr ) + "]"
1201
1201
1202
1202
1203
- class StructureFormat (object ):
1203
+ class StructuredVoidFormat (object ):
1204
+ """
1205
+ Formatter for structured np.void objects.
1206
+
1207
+ This does not work on structured alias types like np.dtype(('i4', 'i2,i2')),
1208
+ as alias scalars lose their field information, and the implementation
1209
+ relies upon np.void.__getitem__.
1210
+ """
1204
1211
def __init__ (self , format_functions ):
1205
1212
self .format_functions = format_functions
1206
1213
1207
1214
@classmethod
1208
1215
def from_data (cls , data , ** options ):
1209
1216
"""
1210
- This is a second way to initialize StructureFormat , using the raw data
1217
+ This is a second way to initialize StructuredVoidFormat , using the raw data
1211
1218
as input. Added to avoid changing the signature of __init__.
1212
1219
"""
1213
1220
format_functions = []
@@ -1228,13 +1235,24 @@ def __call__(self, x):
1228
1235
else :
1229
1236
return "({})" .format (", " .join (str_fields ))
1230
1237
1238
+
1239
+ # for backwards compatibility
1240
+ class StructureFormat (StructuredVoidFormat ):
1241
+ def __init__ (self , * args , ** kwargs ):
1242
+ # NumPy 1.14, 2018-02-14
1243
+ warnings .warn (
1244
+ "StructureFormat has been replaced by StructuredVoidFormat" ,
1245
+ DeprecationWarning , stacklevel = 2 )
1246
+ super (StructureFormat , self ).__init__ (* args , ** kwargs )
1247
+
1248
+
1231
1249
def _void_scalar_repr (x ):
1232
1250
"""
1233
1251
Implements the repr for structured-void scalars. It is called from the
1234
1252
scalartypes.c.src code, and is placed here because it uses the elementwise
1235
1253
formatters defined above.
1236
1254
"""
1237
- return StructureFormat .from_data (array (x ), ** _format_options )(x )
1255
+ return StructuredVoidFormat .from_data (array (x ), ** _format_options )(x )
1238
1256
1239
1257
1240
1258
_typelessdata = [int_ , float_ , complex_ , bool_ ]
@@ -1272,6 +1290,11 @@ def dtype_is_implied(dtype):
1272
1290
dtype = np .dtype (dtype )
1273
1291
if _format_options ['legacy' ] == '1.13' and dtype .type == bool_ :
1274
1292
return False
1293
+
1294
+ # not just void types can be structured, and names are not part of the repr
1295
+ if dtype .names is not None :
1296
+ return False
1297
+
1275
1298
return dtype .type in _typelessdata
1276
1299
1277
1300
@@ -1284,12 +1307,12 @@ def dtype_short_repr(dtype):
1284
1307
>>> from numpy import *
1285
1308
>>> assert eval(dtype_short_repr(dt)) == dt
1286
1309
"""
1287
- # handle these separately so they don't give garbage like str256
1288
- if issubclass ( dtype . type , flexible ):
1289
- if dtype . names is not None :
1290
- return "%s" % str (dtype )
1291
- else :
1292
- return "'%s'" % str (dtype )
1310
+ if dtype . names is not None :
1311
+ # structured dtypes give a list or tuple repr
1312
+ return str ( dtype )
1313
+ elif issubclass (dtype . type , flexible ):
1314
+ # handle these separately so they don't give garbage like str256
1315
+ return "'%s'" % str (dtype )
1293
1316
1294
1317
typename = dtype .name
1295
1318
# quote typenames which can't be represented as python variable names
0 commit comments