Skip to content

Commit d6cee36

Browse files
chore: Setup ruff (#217)
* chore: Replace black and flake8 with Ruff - Setup ruff, ran ruff scans and format - Fixed all present issues - Updated workflow to use ruff instead * chore: Setup pre-commit with ruff hooks * chore: Fix failing tests and moved conftest * chore: modified conftest to aquire token if it is not present
1 parent a1d49be commit d6cee36

14 files changed

Lines changed: 149 additions & 115 deletions

.flake8

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

.github/workflows/linting.yml

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
1-
name: linting
1+
name: Check formatting and linting
22

33
on:
44
pull_request:
55
push: { branches: [main] }
66

77
jobs:
8-
black:
8+
ruff-check:
9+
name: Run ruff lint and format checks
910
runs-on: ubuntu-latest
10-
strategy:
11-
matrix:
12-
python-version: ["3.9"]
13-
steps:
14-
- uses: actions/checkout@v4
15-
- name: Set up Python ${{ matrix.python-version }}
16-
uses: actions/setup-python@v5
17-
with:
18-
python-version: ${{ matrix.python-version }}
19-
- uses: psf/black@stable
20-
with:
21-
options: "--check --verbose --line-length 79"
22-
src: "./src/sumo/wrapper"
23-
flake8:
24-
runs-on: ubuntu-latest
25-
strategy:
26-
matrix:
27-
python-version: ["3.9"]
11+
2812
steps:
2913
- uses: actions/checkout@v4
30-
- name: Set up Python ${{ matrix.python-version }}
31-
uses: actions/setup-python@v5
14+
15+
- uses: actions/setup-python@v5
3216
with:
33-
python-version: ${{ matrix.python-version }}
34-
- name: Install dependencies
35-
run: |
36-
python -m pip install --upgrade pip
37-
pip install flake8
38-
- name: Analysing the code with flake8
39-
run: |
40-
flake8 src/sumo/wrapper --config .flake8
17+
python-version: '3.11'
18+
cache: 'pip'
19+
20+
- name: Installing dependencies
21+
run: pip install ruff
22+
23+
- name: Run ruff lint
24+
run: ruff check .
25+
26+
- name: Run ruff format
27+
run: ruff format . --check
28+

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ build
1212
/src/testing.py
1313
/docs/_build
1414
/src/sumo/wrapper/version.py
15-
*_version.py
15+
*_version.py
16+
.vscode/

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: lint
5+
name: Ruff Lint
6+
description: Linting using ruff
7+
entry: bash -c 'ruff check .'
8+
language: system
9+
stages: ["pre-commit", "pre-push"]
10+
11+
- id: format
12+
name: Ruff Format
13+
description: Formatting using ruff
14+
entry: bash -c 'ruff format . --check'
15+
language: system
16+
stages: ["pre-commit", "pre-push"]

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,3 @@
5555
# relative to this directory. They are copied after the builtin static files,
5656
# so a file named "default.css" will overwrite the builtin "default.css".
5757
html_static_path = ["_static"]
58-

pyproject.toml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ build-backend = "setuptools.build_meta"
55
[tool.setuptools_scm]
66
version_file = "src/sumo/wrapper/_version.py"
77

8-
9-
[tool.black]
10-
line-length = 79
11-
128
[project]
139
name = "sumo-wrapper-python"
1410
description = "Python wrapper for the Sumo API"
@@ -39,6 +35,7 @@ docs = [
3935
"sphinx-autodoc-typehints",
4036
"sphinxcontrib-apidoc",
4137
]
38+
dev = ["ruff", "pre-commit"]
4239

4340
[project.urls]
4441
Repository = "https://github.com/equinor/sumo-wrapper-python"
@@ -48,3 +45,32 @@ sumo_login = "sumo.wrapper.login:main"
4845

4946
[tool.setuptools.packages.find]
5047
where = ["src"]
48+
49+
[tool.ruff]
50+
exclude = [
51+
".env",
52+
".git",
53+
".github",
54+
".venv",
55+
"venv",
56+
]
57+
58+
line-length = 79
59+
60+
[tool.ruff.lint]
61+
ignore = [
62+
"E501",
63+
]
64+
65+
extend-select = [
66+
"C4", # Flake8-comprehensions
67+
"I", # isort
68+
"SIM", # Flake8-simplify
69+
"TC", # Flake8-type-checking
70+
"TID", # Flake8-tidy-imports
71+
"N", # pep8-naming
72+
]
73+
74+
[tool.ruff.lint.per-file-ignores]
75+
"__init__.py" = ["F401"]
76+

src/sumo/wrapper/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .sumo_client import SumoClient
21
from ._retry_strategy import RetryStrategy
2+
from .sumo_client import SumoClient
33

44
try:
55
from ._version import version

src/sumo/wrapper/_auth_provider.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import msal
1+
import errno
2+
import json
23
import os
3-
from datetime import datetime, timedelta
44
import stat
55
import sys
6-
import json
7-
import jwt
86
import time
9-
from azure.identity import ManagedIdentityCredential
10-
import tenacity as tn
11-
from ._retry_strategy import _log_retry_info, _return_last_value
7+
from datetime import datetime, timedelta
128

9+
import jwt
10+
import msal
11+
import tenacity as tn
12+
from azure.identity import ManagedIdentityCredential
1313
from msal_extensions.persistence import FilePersistence
1414
from msal_extensions.token_cache import PersistedTokenCache
15-
import errno
1615

16+
from ._retry_strategy import _log_retry_info, _return_last_value
1717

1818
if not sys.platform.startswith("linux"):
1919
from msal_extensions import build_encrypted_persistence
@@ -422,14 +422,12 @@ def get_auth_provider(
422422
return AuthProviderDeviceCode(client_id, authority, resource_id)
423423
# ELSE
424424
if all(
425-
[
426-
os.getenv(x)
427-
for x in [
428-
"AZURE_FEDERATED_TOKEN_FILE",
429-
"AZURE_TENANT_ID",
430-
"AZURE_CLIENT_ID",
431-
"AZURE_AUTHORITY_HOST",
432-
]
425+
os.getenv(x)
426+
for x in [
427+
"AZURE_FEDERATED_TOKEN_FILE",
428+
"AZURE_TENANT_ID",
429+
"AZURE_CLIENT_ID",
430+
"AZURE_AUTHORITY_HOST",
433431
]
434432
):
435433
return AuthProviderManaged(resource_id)

src/sumo/wrapper/_logging.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44

55
class LogHandlerSumo(logging.Handler):
6-
def __init__(self, sumoClient):
6+
def __init__(self, sumo_client):
77
logging.Handler.__init__(self)
8-
self._sumoClient = sumoClient
8+
self._sumoClient = sumo_client
99
return
1010

1111
def emit(self, record):
1212
try:
13-
dt = datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
13+
dt = (
14+
datetime.now(datetime.timezone.utc)
15+
.replace(microsecond=0)
16+
.isoformat()
17+
+ "Z"
18+
)
1419
json = {
1520
"severity": record.levelname,
1621
"message": record.getMessage(),
@@ -20,7 +25,7 @@ def emit(self, record):
2025
"funcname": record.funcName,
2126
"linenumber": record.lineno,
2227
}
23-
if "objectUuid" in record.__dict__.keys():
28+
if "objectUuid" in record.__dict__:
2429
json["objectUuid"] = record.__dict__.get("objectUuid")
2530

2631
self._sumoClient.post("/message-log/new", json=json)

src/sumo/wrapper/_retry_strategy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import tenacity as tn
21
import httpx
2+
import tenacity as tn
33

44

55
def _log_retry_info(retry_state):

0 commit comments

Comments
 (0)