Skip to content

Commit

Permalink
Enable ruff UP rules (#146)
Browse files Browse the repository at this point in the history
* enable ruff up rules

* fix

* exclude pyi globally
  • Loading branch information
mscheltienne authored Jan 15, 2024
1 parent f4cb169 commit d1bda8d
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pycrostates/cluster/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ def _reject_edge_segments(segmentation: NDArray[int]) -> NDArray[int]:
segmentation[:n] = -1

# set last segment to unlabeled
n = np.flip((segmentation != segmentation[-1])).argmax()
n = np.flip(segmentation != segmentation[-1]).argmax()
segmentation[-n:] = -1

return segmentation
Expand Down
2 changes: 1 addition & 1 deletion pycrostates/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def matplotlib_config():

class CallbackRegistryReraise(orig):
def __init__(self, exception_handler=None, signals=None):
super(CallbackRegistryReraise, self).__init__(exception_handler)
super().__init__(exception_handler)

cbook.CallbackRegistry = CallbackRegistryReraise

Expand Down
2 changes: 1 addition & 1 deletion pycrostates/preprocessing/tests/test_spatial_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_spatial_filter_custom_adjacency():
adjacency_matrix, ch_names = find_ch_adjacency(raw_all.info, "eeg")
apply_spatial_filter(raw_all.copy(), "eeg", adjacency=adjacency_matrix)
with pytest.raises(ValueError, match="Adjacency must have exactly 2 dimensions"):
apply_spatial_filter(raw_all.copy(), "eeg", adjacency=np.ones((len(ch_names))))
apply_spatial_filter(raw_all.copy(), "eeg", adjacency=np.ones(len(ch_names)))
with pytest.raises(ValueError, match="Adjacency must be of shape"):
apply_spatial_filter(
raw_all.copy(), "eeg", adjacency=adjacency_matrix[:-2, :-2]
Expand Down
2 changes: 1 addition & 1 deletion pycrostates/segmentation/tests/test_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_plot_segmentation(ModK, inst):
)
def test_invalid_segmentation(Segmentation, inst, bad_inst, caplog):
"""Test that we can not create an invalid segmentation."""
labels = np.zeros((inst.times.size))
labels = np.zeros(inst.times.size)
cluster_centers = np.zeros((4, len(inst.ch_names) - len(inst.info["bads"])))
cluster_names = ["a", "b", "c", "d"]

Expand Down
11 changes: 6 additions & 5 deletions pycrostates/utils/_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _ensure_int(item, item_name=None):
item = int(operator.index(item))
except TypeError:
item_name = "Item" if item_name is None else "'%s'" % item_name
raise TypeError("%s must be an int, got %s instead." % (item_name, type(item)))
raise TypeError(f"{item_name} must be an int, got {type(item)} instead.")

return item

Expand Down Expand Up @@ -161,9 +161,9 @@ def _check_value(item, allowed_values, item_name=None, extra=None):
if len(allowed_values) == 1:
options = "The only allowed value is %s" % repr(allowed_values[0])
elif len(allowed_values) == 2:
options = "Allowed values are %s and %s" % (
repr(allowed_values[0]),
repr(allowed_values[1]),
options = (
f"Allowed values are {repr(allowed_values[0])} "
f"and {repr(allowed_values[1])}"
)
else:
options = "Allowed values are "
Expand Down Expand Up @@ -286,7 +286,8 @@ def _check_picks_uniqueness(info, picks):
ch_types = info.get_channel_types(unique=False)
ch_types, counts = np.unique(ch_types, return_counts=True)
channels_msg = ", ".join(
"%s '%s' channel(s)" % t for t in zip(counts, ch_types)
"%s '%s' channel(s)" % t # noqa: UP031
for t in zip(counts, ch_types)
)
raise ValueError(
f"Only one datatype can be selected, but 'picks' results in {channels_msg}."
Expand Down
2 changes: 1 addition & 1 deletion pycrostates/utils/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_config():
if not os.path.isfile(config_path):
# create default config
_save_config(default_config)
with open(config_path, "r", encoding="utf-8") as f:
with open(config_path, encoding="utf-8") as f:
config = json.load(f)
return config

Expand Down
2 changes: 1 addition & 1 deletion pycrostates/utils/_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


# https://github.com/sphinx-gallery/sphinx-gallery/issues/1112
class _WrapStdOut(object):
class _WrapStdOut:
"""Dynamically wrap to sys.stdout.
This makes packages that monkey-patch sys.stdout (e.g.doctest, sphinx-gallery) work
Expand Down
2 changes: 1 addition & 1 deletion pycrostates/utils/tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_docdict_order():
del docdict_[key]
docs_path = Path(__file__).parents[1] / "_docs.py"
assert docs_path.is_file()
with open(docs_path, "r", encoding="UTF-8") as fid:
with open(docs_path, encoding="UTF-8") as fid:
docs = fid.read()
entries = re.findall(r'docdict\[(?:\n )?["\'](.+)["\']\n?\] = ', docs)
# test length, uniqueness and order
Expand Down
2 changes: 1 addition & 1 deletion pycrostates/utils/tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_file_handler(tmp_path):

logger.handlers[-1].close()

with open(fname, mode="r") as file:
with open(fname) as file:
lines = file.readlines()

assert len(lines) == 2
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,21 @@ minversion = '6.0'

[tool.ruff]
extend-exclude = [
'*.pyi',
'doc',
'setup.py',
]
ignore = [
"UP007", # 'Use `X | Y` for type annotations', requires python 3.10
]
line-length = 88
select = ["E", "F", "W"]
select = ["E", "F", "UP", "W"]
target-version = 'py39'

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.per-file-ignores]
'*.pyi' = ['E501']
'__init__.py' = ['F401']

[tool.setuptools]
Expand Down

0 comments on commit d1bda8d

Please sign in to comment.