mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.71k stars 1.32k forks source link

BUG: warn magic isn't magical enough for contextlib.contextmanager #12913

Open larsoner opened 1 week ago

larsoner commented 1 week ago

... I was wondering why the log contains next(self.gen):

>>> raw = mne.io.read_raw("/Users/clemens/Downloads/S01_MI/motorimagination_subject1_run1.gdf")
Extracting EDF parameters from /Users/clemens/Downloads/S01_MI/motorimagination_subject1_run1.gdf...
GDF file detected
Setting channel info structure...
Could not determine channel type of the following channels, they will be set as EEG:
F3, F1, Fz, F2, F4, FFC5h, FFC3h, FFC1h, FFC2h, FFC4h, FFC6h, FC5, FC3, FC1, FCz, FC2, FC4, FC6, FTT7h, FCC5h, FCC3h, FCC1h, FCC2h, FCC4h, FCC6h, FTT8h, C5, C3, C1, Cz, C2, C4, C6, TTP7h, CCP5h, CCP3h, CCP1h, CCP2h, CCP4h, CCP6h, TTP8h, CP5, CP3, CP1, CPz, CP2, CP4, CP6, CPP5h, CPP3h, CPP1h, CPP2h, CPP4h, CPP6h, P3, P1, Pz, P2, P4, PPO1h, PPO2h, eog-l, eog-m, eog-r, thumb_near, thumb_far, thumb_index, index_near, index_far, index_middle, middle_near, middle_far, middle_ring, ring_near, ring_far, ring_little, litte_near, litte_far, thumb_palm, wrist_bend, roll, pitch, gesture, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy, armeodummy
Creating raw.info structure...
/Users/clemens/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/contextlib.py:144: RuntimeWarning: Channel names are not unique, found duplicates for: {'armeodummy'}. Applying running numbers for duplicates.
  next(self.gen)

Originally posted by @cbrnr in https://github.com/mne-tools/mne-python/pull/12909#pullrequestreview-2383989923

larsoner commented 1 week ago

Probably not just your setup -- our warn does some magic to set the stacklevel such that it's out of the mne namespace. My guess is that some code is using a @contextlib.contextmanager and while traversing the stack, contextlib is outside the mne namespace so our warn stops there and emits a warning. Instead it should know contextlib is special in this way and keep going.