mne-tools / mne-tools/mne-python

When using Nuitka to package a project, an error occurred when reading the file: "KeyError: 'self'"

Open
#12,923 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

ENH
Dominant language
Python
Stars
3.5k
Forks
1.6k
Avg merge
1d 6h
Merged PRs (30d)
100

Description

Describe the new feature or enhancement

I'm experiencing an issue when using Nuitka to package a project that relies on MNE-Python. The problem appears to be related to the _get_argvalues function in MNE, which attempts to access frame information but fails due to Nuitka's handling of frame dictionaries.

The related issue on Nuitka's GitHub is documented here: Nuitka Issue #2995.

Describe your proposed implementation

As mentioned:
One possible solution would be to modify _get_argvalues to ensure that the frame dictionary is populated correctly. Currently, the function relies on frame inspection through inspect.currentframe() and stack traversal. However, Nuitka does not populate the frame dictionary until an exception occurs, which makes this approach unreliable.

Here are two alternative methods:

  1. Annotate functions: Annotate specific functions so that their frame dictionaries are always populated, even if no exception occurs. This would require a custom decorator or configuration within MNE to forcefully populate the frame.
  2. Use locals() directly: Modify _get_argvalues to use {locals()[var_name] for var_name in inspect.getargvalues(inspect.currentframe())[0]} to access argument values directly from locals() instead of relying on stack inspection. This approach avoids the need to traverse the frame hierarchy and should work more reliably with Nuitka.
Describe possible alternatives

Another possible alternative is to inject __import__("inspect").currentframe().f_locals.update(locals()) before calling the base class. This workaround could populate f_locals to ensure that derived classes have access to necessary parameters. However, this would require changes in all derived classes and may not be feasible in the long term due to maintenance concerns.

Among the approaches, using locals() directly appears to be the most efficient and portable solution.

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the _get_argvalues function and reproducing the MNE packaging failure under Nuitka, using Nuitka Issue #2995 for context. Compare frame inspection with the proposed locals() approach and determine a maintainable solution; done means packaged projects no longer fail with KeyError: 'self' when reading argument values.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.