Repository: mne-tools/mne-bids

Issue:
[BUG] BIDSpath.update changes suffix option even if suffix isn't supported
#### Describe the bug
When using the BIDSPath option, if you try to update the suffix with a non-supported suffix (e.g. `_ave`), you will receive an error that the suffix is not supported (intended behavior) but the BIDSPath option is updated regardless (not intended behavior). I believe this is because the `_check` method is called after setting the suffix instead of being called first.

#### Steps to reproduce
```python
import mne_bids


bids_path = mne_bids.BIDSPath(subject='test')
bids_path.update(suffix='ave')
bids_path
```

#### Expected results
BIDSPath object should not update with the unsupported extension or there should be an ignore option so that the error doesn't occur.

#### Actual results
```python
In [6]: bids_path.update(suffix='ave')  
ValueError: Suffix ave is not allowed. Use one of these suffixes ['meg', 'markers', 'eeg', 'ieeg', 'T1w', 'FLASH', 'participants', 'scans', 'electrodes', 'channels', 'coordsystem', 'events', 'headshape', 'digitizer', 'beh', 'physio', 'stim'].

In [7]: bids_path                                                               
Out[7]: 
BIDSPath(
root: None
datatype: None
basename: sub-test_ave)
```



Discussion:

Resolving patch:
diff --git a/mne_bids/path.py b/mne_bids/path.py
--- a/mne_bids/path.py
+++ b/mne_bids/path.py
@@ -714,6 +714,7 @@ def update(self, *, check=None, **kwargs):
                 kwargs['extension'] = f'.{extension}'
 
         # error check entities
+        old_kwargs = dict()
         for key, val in kwargs.items():
 
             # check if there are any characters not allowed
@@ -728,6 +729,8 @@ def update(self, *, check=None, **kwargs):
             # set entity value, ensuring `root` is a Path
             if val is not None and key == 'root':
                 val = Path(val).expanduser()
+            old_kwargs[key] = \
+                getattr(self, f'{key}') if hasattr(self, f'_{key}') else None
             setattr(self, f'_{key}', val)
 
         # infer datatype if suffix is uniquely the datatype
@@ -735,8 +738,15 @@ def update(self, *, check=None, **kwargs):
                 self.suffix in SUFFIX_TO_DATATYPE:
             self._datatype = SUFFIX_TO_DATATYPE[self.suffix]
 
-        # Perform a check of the entities.
-        self._check()
+        # Perform a check of the entities and revert changes if check fails
+        try:
+            self._check()
+        except Exception as e:
+            old_check = self.check
+            self.check = False
+            self.update(**old_kwargs)
+            self.check = old_check
+            raise e
         return self
 
     def match(self, check=False):


Test patch:
diff --git a/mne_bids/tests/test_path.py b/mne_bids/tests/test_path.py
--- a/mne_bids/tests/test_path.py
+++ b/mne_bids/tests/test_path.py
@@ -1134,3 +1134,12 @@ def test_datasetdescription_with_bidspath(return_bids_test_dir):
     bids_path.update(suffix='dataset_description', check=False)
     assert bids_path.fpath.as_posix() == \
         Path(f'{return_bids_test_dir}/dataset_description.json').as_posix()
+
+
+def test_update_fail_check_no_change():
+    bids_path = BIDSPath(subject='test')
+    try:
+        bids_path.update(suffix='ave')
+    except Exception:
+        pass
+    assert bids_path.suffix is None