Skip to content

Commit e4b297a

Browse files
authored
Migrate to using Ruff for formatting and linting (#679)
* Migrated to using Ruff for linting and formatting * Applied Ruff formatting changes * Deleted 'transfer' CLI file
1 parent 4f78a6f commit e4b297a

37 files changed

+173
-334
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,53 +45,15 @@ repos:
4545
name: Formatting the pyproject.toml file
4646
additional_dependencies: ["tox>=4.9"]
4747

48-
# Automatic source code formatting
49-
- repo: https://github.com/psf/black
50-
rev: 24.3.0 # Released 2024-03-15
48+
# Format and lint using Ruff
49+
- repo: https://github.com/astral-sh/ruff-pre-commit
50+
rev: v0.14.0
5151
hooks:
52-
- id: black
53-
name: Formatting Python scripts according to Black
54-
args: [--safe, --quiet]
55-
56-
# Automatically sort imports
57-
- repo: https://github.com/PyCQA/isort
58-
rev: 5.13.2 # Released 2023-12-13
59-
hooks:
60-
- id: isort
61-
name: Tidying Python imports via Isort
62-
args: [
63-
#'-a', 'from __future__ import annotations', # 3.7-3.11
64-
"--rm",
65-
"from __future__ import absolute_import", # -3.0
66-
"--rm",
67-
"from __future__ import division", # -3.0
68-
"--rm",
69-
"from __future__ import generator_stop", # -3.7
70-
"--rm",
71-
"from __future__ import generators", # -2.3
72-
"--rm",
73-
"from __future__ import nested_scopes", # -2.2
74-
"--rm",
75-
"from __future__ import print_function", # -3.0
76-
"--rm",
77-
"from __future__ import unicode_literals", # -3.0
78-
"--rm",
79-
"from __future__ import with_statement", # -2.6
80-
]
81-
82-
# Linting
83-
- repo: https://github.com/PyCQA/flake8
84-
# Release history: https://flake8.pycqa.org/en/latest/release-notes/index.html
85-
rev: 7.0.0 # Released 2024-01-05
86-
hooks:
87-
- id: flake8
88-
name: Running Flake8 linter on Python files
89-
additional_dependencies: [
90-
# Added Flake8-pyproject plugin for .toml compatibility
91-
"Flake8-pyproject==1.2.3", # Released 2023-03-21
92-
# flake8-comprehensions URL: https://github.com/adamchainz/flake8-comprehensions
93-
"flake8-comprehensions==3.14.0", # Released 2023-07-10
94-
]
52+
- id: ruff
53+
name: Running Ruff linter
54+
args: ["--fix"]
55+
- id: ruff-format
56+
name: Running Ruff formatter
9557

9658
# Type checking
9759
- repo: https://github.com/pre-commit/mirrors-mypy

pyproject.toml

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -121,53 +121,26 @@ zip-safe = false
121121
[tool.setuptools.packages.find]
122122
where = ["src", "tests"]
123123

124-
[tool.isort]
125-
profile = "black"
126-
127-
[tool.flake8]
128-
# Flake8-pyproject allows TOML file settings to be read into Flake8
129-
# URL: https://pypi.org/project/Flake8-pyproject/
130-
select = [
124+
[tool.ruff]
125+
line-length = 88
126+
lint.extend-ignore = ["E203", "E266", "E501", "E731", "E741"]
127+
lint.select = [
131128
"C4",
132-
"E401",
133-
"E711",
134-
"E712",
135-
"E713",
136-
"E714",
137-
"E721",
138-
"E722",
139-
"E901",
140-
"F401",
141-
"F402",
142-
"F403",
143-
"F405",
144-
"F541",
145-
"F631",
146-
"F632",
147-
"F633",
148-
"F811",
149-
"F812",
150-
"F821",
151-
"F822",
152-
"F841",
153-
"F901",
154-
"W191",
155-
"W291",
156-
"W292",
157-
"W293",
158-
"W602",
159-
"W603",
160-
"W604",
161-
"W605",
162-
"W606",
163-
]
164-
ignore = [
165-
"E203",
166-
"E266",
167-
"E501",
168-
"W503",
129+
"E4", "E7", "E9",
130+
"F",
131+
"I",
132+
"W6",
169133
]
170-
max-line-length = "88"
134+
fix = true
135+
136+
[tool.ruff.lint.isort]
137+
known-first-party = ["murfey"]
138+
combine-as-imports = true
139+
140+
[tool.ruff.format]
141+
quote-style="double"
142+
indent-style="space"
143+
line-ending="auto"
171144

172145
[tool.pyproject-fmt]
173146
inputs = "pyproject.toml"

src/murfey/cli/generate_route_manifest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def is_implicitly_resolved(value: str) -> bool:
6868

6969

7070
def find_routers(name: str) -> dict[str, APIRouter]:
71-
7271
def _extract_routers_from_module(module: ModuleType):
7372
routers = {}
7473
for name, obj in inspect.getmembers(module):
@@ -115,7 +114,6 @@ def _extract_routers_from_module(module: ModuleType):
115114

116115

117116
def get_route_manifest(routers: dict[str, APIRouter]):
118-
119117
manifest = {}
120118

121119
for router_name, router in routers.items():

src/murfey/cli/inject_spa_processing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def run():
173173
/ str(ppath.stem + "_motion_corrected.mrc")
174174
)
175175
if proc_params:
176-
177176
detached_ids = [c.id for c in collected_ids]
178177

179178
if not mrc_out.parent.exists():

src/murfey/cli/spa_ispyb_messages.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
DataCollectionGroup,
1515
ProcessingJob,
1616
)
17-
from sqlmodel import Session as MurfeySession
18-
from sqlmodel import create_engine, select
17+
from sqlmodel import Session as MurfeySession, create_engine, select
1918

2019
from murfey.client.contexts.spa import _get_xml_list_index
2120
from murfey.server.feedback import _murfey_id, _register

src/murfey/cli/transfer.py

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/murfey/client/contexts/clem.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def _get_image_elements(root: ET.Element) -> List[ET.Element]:
6565
def _find_elements_recursively(
6666
node: ET.Element,
6767
) -> Generator[ET.Element, None, None]:
68-
6968
# Find items labelled "Element" under current node
7069
elem_list = node.findall("./Children/Element")
7170
if len(elem_list) < 1: # Try alternative path for top-level of XML tree
@@ -103,7 +102,6 @@ def post_transfer(
103102
environment: Optional[MurfeyInstanceEnvironment] = None,
104103
**kwargs,
105104
) -> bool:
106-
107105
super().post_transfer(transferred_file, environment=environment, **kwargs)
108106

109107
# Process files generated by "auto-save" acquisition mode
@@ -188,7 +186,6 @@ def post_transfer(
188186

189187
# Process XLIF files
190188
if transferred_file.suffix == ".xlif":
191-
192189
# Skip processing of "_histo" histogram XLIF files
193190
if transferred_file.stem.endswith("_histo"):
194191
logger.debug(

src/murfey/client/tui/progress.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def _get_pulse_segments(
7171
def __rich_console__(
7272
self, console: Console, options: ConsoleOptions
7373
) -> RenderResult:
74-
7574
width = min(self.width or options.max_width, options.max_width)
7675
ascii = options.ascii_only
7776
if self.pulse:

src/murfey/client/tui/screens.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def __init__(
193193
self._context = SPAModularContext
194194

195195
def compose(self):
196-
197196
machine_data = capture_get(
198197
base_url=str(self.app._environment.url.geturl()),
199198
router_name="session_control.router",
@@ -1013,7 +1012,7 @@ def compose(self):
10131012
name_root += st
10141013
if dest_num:
10151014
dest = str(
1016-
dest_path.parent / f"{name_root}{dest_num+1}"
1015+
dest_path.parent / f"{name_root}{dest_num + 1}"
10171016
)
10181017
else:
10191018
dest = str(dest_path.parent / f"{name_root}2")

src/murfey/server/api/auth.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from murfey.server.murfey_db import murfey_db, url
2424
from murfey.util.api import url_path_for
2525
from murfey.util.config import get_security_config
26-
from murfey.util.db import MurfeyUser as User
27-
from murfey.util.db import Session as MurfeySession
26+
from murfey.util.db import MurfeyUser as User, Session as MurfeySession
2827

2928
# Set up logger
3029
logger = getLogger("murfey.server.api.auth")
@@ -147,7 +146,7 @@ def validate_session_against_visit(session_id: int, visit: str):
147146

148147

149148
async def validate_instrument_token(
150-
token: Annotated[str, Depends(instrument_oauth2_scheme)]
149+
token: Annotated[str, Depends(instrument_oauth2_scheme)],
151150
):
152151
"""
153152
Used by the backend routers to check the incoming instrument server token.
@@ -334,7 +333,6 @@ def validate_user(username: str, password: str) -> bool:
334333

335334

336335
def create_access_token(data: dict, token: str = "") -> str:
337-
338336
# If authenticating with password, auth URL needs a 'mint_session_token' endpoint
339337
if security_config.auth_type == "password":
340338
if auth_url and data.get("session"):
@@ -420,6 +418,6 @@ async def mint_session_token(session_id: MurfeySessionIDFrontend, db=murfey_db):
420418

421419
@router.get("/validate_token")
422420
async def simple_token_validation(
423-
token: Annotated[str, Depends(validate_instrument_token)]
421+
token: Annotated[str, Depends(validate_instrument_token)],
424422
):
425423
return {"valid": True}

0 commit comments

Comments
 (0)