mne-tools / mne-bids

MNE-BIDS is a Python package that allows you to read and write BIDS-compatible datasets with the help of MNE-Python.
https://mne.tools/mne-bids/
BSD 3-Clause "New" or "Revised" License
126 stars 84 forks source link

Fix string format `"blabla" "bla"` #1243

Closed mscheltienne closed 3 months ago

mscheltienne commented 3 months ago

PR Description

I propose to change the stacklevel of a couple of warnings I encountered, because

<ipython-input-34-b78341704568>:1: RuntimeWarning: Converting data files to BrainVision format

is really not that informative and I would appreciate to quickly jump to the function issuing the warning.

While I was on it, I fixed probably all the occurrences of "blabla" "bla" strings. I don't believe this minor maintenance PR needs a changelog entry.

Merge checklist

Maintainer, please confirm the following before merging. If applicable:

larsoner commented 3 months ago

To me if you did mne_bids.something(...) in the interpreter the correct line to get in the warning is the current input one. Typically it's most informative and standard to emit the caller line rather than the something

mscheltienne commented 3 months ago

OK, I did not notice this was using mne.utils.warn and not directly warnings.warn, so indeed the caller line is returned which can be informative.

In an ideal world, we would get both the caller line and the line where the warning was emitted (stacklevel=1, to quickly jump to the warning line and figure out why it was emitted in the first place). I use this second option very often.

Anyway, for now I removed that erroneous stacklevel=2 part.

codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 92.30769% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 97.60%. Comparing base (87eea28) to head (81ae67d). Report is 18 commits behind head on main.

Files Patch % Lines
mne_bids/commands/mne_bids_crosstalk_to_bids.py 50.00% 1 Missing :warning:
mne_bids/commands/mne_bids_mark_channels.py 50.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1243 +/- ## ========================================== - Coverage 97.61% 97.60% -0.02% ========================================== Files 40 40 Lines 8685 8682 -3 ========================================== - Hits 8478 8474 -4 - Misses 207 208 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

larsoner commented 3 months ago

In an ideal world, we would get both the caller line and the line where the warning was emitted (stacklevel=1, to quickly jump to the warning line and figure out why it was emitted in the first place). I use this second option very often.

In theory it should be a few lines to add an add_caller=False to mne.utils.warn that uses the adjacent stack frame calling function somehow like:

<ipython-input-blah>:1: RuntimeWarning: In call to mne_bids.something: Converting data files to BrainVision format

then mne-bids could make use of it.

Maybe we'd even want add_caller=True by default...?

mscheltienne commented 3 months ago

@larsoner That's a nice idea, and would give the caller line, but only as text right? VSCode terminal does some nice magic to enable a Ctrl + click on the emitted warning to jump to the line.

Screenshot from 2024-04-02 15-41-23

With that proposition, the caller line would not benefit from the same 'link' to jump to the definition; and thus without this I don't see the added benefit compared to quickly searching for the error string within the codebase.

Anyway, I will give it a try and see how it renders ;)

mscheltienne commented 3 months ago

Let's keep this PR as a simple string format fix then, mark for merge if happy :)