Skip to content

Commit 92d7c88

Browse files
committed
[Style] make ruff/flake happy
1 parent 830af61 commit 92d7c88

11 files changed

+68
-55
lines changed

spm/__wrapper__/array.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import numpy as np
2+
13
from .core import (
2-
WrappedArray,
4+
WrappedArray,
35
_ListishMixin
46
)
57
from .utils import _copy_if_needed
68

7-
import numpy as np
8-
99

1010
class Array(_ListishMixin, WrappedArray):
1111
"""
@@ -178,7 +178,7 @@ def from_cell(cls, other, **kwargs) -> "Array":
178178
array : Array
179179
Converted array.
180180
"""
181-
from .cell import Cell # FIXME: avoid circular import
181+
from .cell import Cell # FIXME: avoid circular import
182182

183183
if not isinstance(other, Cell):
184184
raise TypeError(f"Expected a {Cell} but got a {type(other)}")
@@ -200,4 +200,4 @@ def __repr__(self):
200200
# Scalar -> display as python scalar
201201
return np.array2string(self, separator=", ")
202202
else:
203-
return super().__repr__()
203+
return super().__repr__()

spm/__wrapper__/cell.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
from .core import (
23
MatlabType,
34
WrappedArray,
@@ -6,15 +7,13 @@
67
_ListMixin,
78
)
89
from .utils import (
9-
_empty_array,
10-
_copy_if_needed,
11-
_import_matlab,
10+
_empty_array,
11+
_copy_if_needed,
12+
_import_matlab,
1213
_matlab_array_types
1314
)
1415
global matlab
1516

16-
import numpy as np
17-
1817

1918
class Cell(_ListMixin, WrappedArray):
2019
"""

spm/__wrapper__/core/base_types.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from functools import partial
2+
import numpy as np
13
from ..utils import (
24
_matlab_array_types,
35
_import_matlab,
4-
)
5-
from functools import partial
6-
import numpy as np
6+
)
7+
78

89
class MatlabType(object):
910
"""Generic type for objects that have an exact matlab equivalent."""
@@ -16,10 +17,10 @@ def from_any(cls, other, **kwargs):
1617
1718
!!! warning "Conversion is performed in-place when possible."
1819
"""
19-
# FIXME: Circular import
20+
# FIXME: Circular import
2021
from .delayed_types import AnyDelayedArray
2122

22-
# FIXME: Circular import
23+
# FIXME: Circular import
2324
from ..cell import Cell
2425
from ..array import Array
2526
from ..matlab_function import MatlabFunction
@@ -45,11 +46,20 @@ def from_any(cls, other, **kwargs):
4546
if "type__" in other:
4647
type__ = other["type__"]
4748

48-
if type__ == "structarray":
49+
if type__ == "none":
50+
# MPython returns this when run with nargout=1 but
51+
# should have been nargout=0
52+
return None
53+
54+
elif type__ == "structarray":
4955
# MPython returns a list of dictionaries in data__
5056
# and the array shape in size__.
5157
return Struct._from_runtime(other)
5258

59+
elif type__ == "emptystruct":
60+
# 0x0 struct
61+
return Struct.from_shape([0])
62+
5363
elif type__ == "cell":
5464
# MPython returns a list of dictionaries in data__
5565
# and the array shape in size__.
@@ -81,8 +91,7 @@ def from_any(cls, other, **kwargs):
8191

8292
else:
8393
other = type(other)(
84-
zip(other.keys(),
85-
map(_from_any, other.values()))
94+
zip(other.keys(), map(_from_any, other.values()))
8695
)
8796
return Struct.from_any(other)
8897

@@ -135,7 +144,7 @@ def _to_runtime(cls, obj):
135144
Convert object to representation that the matlab runtime understands.
136145
"""
137146
to_runtime = cls._to_runtime
138-
from ..utils import sparse # FIXME: Circular import
147+
from ..utils import sparse # FIXME: Circular import
139148

140149
if isinstance(obj, MatlabType):
141150
# class / structarray / cell
@@ -159,7 +168,7 @@ def _to_runtime(cls, obj):
159168
return obj
160169

161170
elif sparse and isinstance(obj, sparse.sparray):
162-
from .SparseArray import SparseArray
171+
from ..sparse_array import SparseArray
163172
return SparseArray.from_any(obj)._as_runtime()
164173

165174
else:
@@ -182,6 +191,7 @@ def _as_matlab_object(self):
182191
# FIXME: Or just keep `_as_matlab_object` and remove `_as_runtime`?
183192
return self._as_runtime()
184193

194+
185195
class AnyMatlabArray(MatlabType):
186196
"""Base class for all matlab-like arrays (numeric, cell, struct)."""
187197

@@ -203,4 +213,4 @@ def as_struct(self):
203213
f"Cannot interpret a {type(self).__name__} as a struct"
204214
)
205215

206-
# TODO: `as_obj` for object arrays?
216+
# TODO: `as_obj` for object arrays?

spm/__wrapper__/core/delayed_types.py

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def __setattr__(self, key, value):
256256
self.as_struct[key] = value
257257
return self._finalize() # Setter -> we can trigger finalize
258258

259+
259260
class WrappedDelayedArray(AnyDelayedArray):
260261
"""
261262
Base class for future objects with known type.

spm/__wrapper__/core/mixin_types.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
from collections.abc import (
2+
MutableSequence,
3+
MutableMapping,
4+
KeysView,
5+
ValuesView,
6+
ItemsView
7+
)
8+
import numpy as np
9+
110
from .base_types import MatlabType
211
from .wrapped_types import WrappedArray
312
from .delayed_types import AnyDelayedArray
413
from ..utils import _matlab_array_types, _empty_array
514

6-
import numpy as np
7-
from collections.abc import (
8-
MutableSequence,
9-
MutableMapping,
10-
KeysView,
11-
ValuesView,
12-
ItemsView
13-
)
1415

1516
class _ListishMixin:
1617
"""These methods are implemented in Cell and Array, but not Struct."""
@@ -81,7 +82,7 @@ def _from_runtime(cls, dictobj: dict):
8182
indices = np.asarray(dictobj['indices__'], dtype=np.long) - 1
8283
values = np.asarray(dictobj['values__'], dtype=dtype).ravel()
8384
return cls.from_coo(values, indices.T, size)
84-
85+
8586

8687
class _ListMixin(_ListishMixin, MutableSequence):
8788
"""These methods are implemented in Cell, but not in Array or Struct."""
@@ -303,7 +304,6 @@ def sort(self, *, key=None, reverse=False, kind="stable", axis=0):
303304
self.reverse()
304305

305306

306-
307307
class _DictMixin(MutableMapping):
308308

309309
# NOTE:
@@ -446,7 +446,7 @@ def __getitem__(self, key):
446446

447447
parent = getattr(self, "_delayed_wrapper", self)
448448

449-
from ..struct import Struct # FIXME: circular imports
449+
from ..struct import Struct # FIXME: circular imports
450450
delayed = Struct(self.shape)
451451
opt = dict(
452452
flags=['refs_ok', 'zerosize_ok', 'multi_index'],
@@ -527,7 +527,7 @@ def setdefault(self, key, value=None):
527527
item.setdefault(key, value)
528528

529529
def update(self, other):
530-
from ..struct import Struct # FIXME: circular imports
530+
from ..struct import Struct # FIXME: circular imports
531531
other = Struct.from_any(other)
532532
other = np.ndarray.view(other, np.ndarray)
533533
other = np.broadcast_to(other, self.shape)
@@ -542,7 +542,7 @@ def update(self, other):
542542
item.update(other_elem)
543543

544544
# --- helper ------------------------------------------------------
545-
class deal: # FIXME: Removed dependency to Cell
545+
class deal: # FIXME: Removed dependency to Cell
546546
"""
547547
Helper class to assign values into a specific field of a Struct array.
548548
@@ -573,6 +573,5 @@ def broadcast_to_struct(self, struct):
573573
return np.broadcast_to(self, shape)
574574

575575
def to_cell(self):
576-
from ..cell import Cell # FIXME: circular imports
576+
from ..cell import Cell # FIXME: circular imports
577577
return np.ndarray.view(self, Cell)
578-

spm/__wrapper__/core/wrapped_types.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .base_types import (
2-
MatlabType,
3-
AnyMatlabArray,
2+
MatlabType,
3+
AnyMatlabArray,
44
)
55
from .delayed_types import (
66
AnyDelayedArray,
@@ -9,6 +9,7 @@
99
)
1010
import numpy as np
1111

12+
1213
# ----------------------------------------------------------------------
1314
# WrappedArray
1415
# ----------------------------------------------------------------------x
@@ -294,7 +295,7 @@ def _resize_for_index(self, index, set_default=True):
294295

295296
def _return_delayed(self, index):
296297
from ..cell import Cell
297-
from ..struct import Struct # FIXME: avoid circular import
298+
from ..struct import Struct # FIXME: avoid circular import
298299

299300
if not isinstance(index, tuple):
300301
index = (index,)
@@ -336,4 +337,4 @@ def _return_delayed(self, index):
336337
# In numeric arrays, only seeting OOB items is allowed.
337338
# Getting OOB items should raise an error, which this
338339
# call to the ndarray accessor will do.
339-
return super().__getitem__(index)
340+
return super().__getitem__(index)

spm/__wrapper__/matlab_class.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import warnings
2+
import numpy as np
3+
14
from .core import MatlabType
25

3-
import numpy as np
4-
import warnings
56

67
class MatlabClass(MatlabType):
78
_subclasses = dict()
@@ -96,7 +97,7 @@ def _process_index(self, ind, k=1, n=1):
9697
)
9798
except TypeError:
9899
pass
99-
100+
100101
from .runtime import Runtime
101102

102103
if not hasattr(self, '__endfn'):

spm/__wrapper__/matlab_function.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .core import MatlabType
22
from .utils import _import_matlab
33

4+
45
class MatlabFunction(MatlabType):
56
"""
67
Wrapper for matlab function handles.
@@ -19,7 +20,7 @@ def __init__(self, matlab_object):
1920
matlab = _import_matlab()
2021
if not isinstance(matlab_object, matlab.object):
2122
raise TypeError("Expected a matlab.object")
22-
23+
2324
self._matlab_object = matlab_object
2425

2526
def _as_runtime(self):
@@ -37,4 +38,4 @@ def from_any(cls, other):
3738

3839
def __call__(self, *args, **kwargs):
3940
from .runtime import Runtime
40-
return Runtime.call(self._matlab_object, *args, **kwargs)
41+
return Runtime.call(self._matlab_object, *args, **kwargs)

spm/__wrapper__/sparse_array.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import warnings
2+
import numpy as np
3+
14
from .core import (
2-
AnyWrappedArray,
5+
AnyWrappedArray,
36
_SparseMixin
47
)
58
from .utils import (
@@ -8,8 +11,6 @@
811
)
912
from .array import Array
1013

11-
import numpy as np
12-
import warnings
1314

1415
if sparse:
1516
class WrappedSparseArray(sparse.sparray, AnyWrappedArray):
@@ -205,4 +206,3 @@ def from_coo(cls, values, indices, shape=None, **kw) -> "SparseArray":
205206
obj = cls.from_shape(shape, dtype=dtype)
206207
obj[tuple(indices)] = values
207208
return obj
208-

spm/__wrapper__/struct.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import numpy as np
2+
13
from .core import (
24
WrappedArray,
35
_DictMixin,
46
MatlabType,
57
DelayedStruct
68
)
79
from .utils import (
8-
_copy_if_needed,
10+
_copy_if_needed,
911
_empty_array
1012
)
1113

12-
import numpy as np
13-
1414

1515
class Struct(_DictMixin, WrappedArray):
1616
"""
@@ -358,7 +358,7 @@ def as_dict(self, keys=None) -> dict:
358358

359359
raise ValueError(keys)
360360
return asdict
361-
361+
362362
def _allkeys(self):
363363
# Return all keys present across all elements.
364364
# Keys are ordered by (1) element (2) within-element order
@@ -478,4 +478,3 @@ def __delattr__(self, key):
478478
return _DictMixin.__delitem__(self, key)
479479
except KeyError as e:
480480
raise AttributeError(str(e))
481-

spm/__wrapper__/utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
except (ImportError, ModuleNotFoundError):
88
sparse = None
99

10+
1011
# ----------------------------------------------------------------------
1112
# Helpers
1213
# ----------------------------------------------------------------------
1314

15+
1416
# We'll complain later if the runtime is not instantiated
1517
def _import_matlab():
1618
try:
1719
import matlab
1820
except (ImportError, ModuleNotFoundError):
1921
matlab = None
20-
return matlab
22+
return matlab
2123

2224

2325
def _copy_if_needed(out, inp, copy=None) -> np.ndarray:

0 commit comments

Comments
 (0)