Skip to content

Conversation

@balbasty
Copy link
Contributor

See #3

This PR removes the explicit import of spm._spm and instead lets the wrapped package (in our case, spm-python) implement its own Runtime class. Something like:

# in: spm/__wrapper__.py
from mpython.runtime import Runtime as RuntimeBase

class Runtime(RuntimeBase):
    @classmethod
    def _import_runtime(cls):
        import spm_runtime
        return spm_runtime

It also needs each of the MatlabClass children it implements to point to the correct runtime. It's a bit tricky if we don't want to mess with the __init_subclass__ business implemented in MatlabClass but I think it can be done in a not too verbose way using a mixin:

# in: spm/__wrapper__.py

class RuntimeMixin:
    @classmethod
    def _runtime(cls):
        return Runtime

# in: spm/nifti.py
from mpython.matlab_class import MatlabClass

class nifti(RuntimeMixin, MatlabClass):
    ...

I am leaving this as draft as it needs

  • spm-runtime to be properly released and published to pypi
  • spm-python and mpython to define the runtime and mixin described above

I've also replaced the local imports (used to break circular imports) with "delayed imports" using a magic class. The (only?) advantage is it allows to have all the imports listed at the top of the file like we're used to. See e.g.

class _imports(DelayedImport):
Array = 'mpython.array.Array'
Cell = 'mpython.cell.Cell'
Struct = 'mpython.struct.Struct'
SparseArray = 'mpython.sparse_array.SparseArray'
MatlabClass = 'mpython.matlab_class.MatlabClass'
MatlabFunction = 'mpython.matlab_function.MatlabFunction'
AnyDelayedArray = 'mpython.core.delayed_types.MatlabFunction'


Happy to discuss! (@johmedr )

@balbasty balbasty marked this pull request as ready for review June 19, 2025 14:43
Copy link
Member

@johmedr johmedr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for updating this. Seems to work nicely, and can be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants