Skip to content

Commit ca88963

Browse files
author
John Hughes
authored
Fix python linting and formatting (#166)
* Added new workflow for enabling Python linting in CI * Adjust config so that linting and format works from root * Moved mypy config into root so it's picked up globally. * Moved .flake8 config to root * Make mypy green * Fix typos in dicom example * Fixed mypy linting issues in rerun_sdk and color_conversion * Added numpy to lint requirements for typing plugin Co-authored-by: Emil Ernerfeldt <emil@rerun.io>
1 parent dcad68d commit ca88963

23 files changed

+417
-315
lines changed

rerun_py/.flake8 .flake8

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
2-
max-line-length = 88
2+
max-line-length = 120
33
ban-relative-imports = true
44
docstring-convention = all
55
extend-ignore =
@@ -17,3 +17,6 @@ extend-exclude =
1717
# Automatically generated test artifacts
1818
venv/,
1919
target/,
20+
21+
# Disabled for now TODO(john)
22+
examples/,

.github/workflows/lint-python.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Lint Python
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "rerun_py/**"
7+
- "examples/**"
8+
- ".github/workflows/lint-python.yml"
9+
push:
10+
branches:
11+
- "main"
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
main:
19+
name: Lint Python
20+
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- uses: actions/checkout@v3
25+
26+
- uses: extractions/setup-just@v1
27+
with:
28+
just-version: 1.5
29+
30+
- name: Set up Python
31+
uses: actions/setup-python@v4
32+
with:
33+
python-version: '3.8'
34+
cache: 'pip'
35+
cache-dependency-path: 'rerun_py/requirements-lint.txt'
36+
37+
- name: Install Python dependencies
38+
run: |
39+
pip install --upgrade pip
40+
pip install -r rerun_py/requirements-lint.txt
41+
42+
- name: Lint Python
43+
run: |
44+
just py-lint

.isort.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[settings]
2+
profile=black

.mypy.ini

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[mypy]
2+
files = rerun_py/rerun_sdk, rerun_py/tests
3+
namespace_packages = True
4+
show_error_codes = True
5+
strict = True
6+
enable_error_code = redundant-expr, truthy-bool, ignore-without-code
7+
plugins = numpy.typing.mypy_plugin

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
"target/**": true
1313
},
1414
"files.autoGuessEncoding": true,
15-
"python.formatting.provider": "black"
15+
"python.formatting.provider": "black",
16+
"python.linting.mypyEnabled": true,
17+
"python.linting.enabled": true
1618
}

examples/car/main.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env python3
22
"""Shows how to use the Rerun SDK."""
33

4-
from dataclasses import dataclass
4+
import argparse
55
import os
6+
from dataclasses import dataclass
67
from pathlib import Path
78
from time import sleep
89
from typing import Final, Iterator, Tuple
910

10-
import argparse
1111
import cv2 # type: ignore
1212
import numpy as np
13-
1413
import rerun_sdk as rerun
1514

1615
CAMERA_GLB: Final = Path(os.path.dirname(__file__)).joinpath(

examples/deep_sdf/download_dataset.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import io
44
import os
5-
import requests
6-
from pathlib import Path
75
import zipfile
6+
from pathlib import Path
7+
8+
import requests
89

910

1011
def download_mcguire_sample(package, name):

examples/deep_sdf/main.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@
3434

3535
import argparse
3636
import os
37+
from pathlib import Path
38+
from timeit import default_timer as timer
39+
from typing import Tuple, cast
3740

3841
import mesh_to_sdf
3942
import numpy as np
4043
import rerun_sdk as rerun
4144
import trimesh
42-
43-
from pathlib import Path
44-
from timeit import default_timer as timer
45-
from typing import Tuple, cast
46-
45+
from rerun_sdk import LogLevel, MeshFormat
4746
from scipy.spatial.transform import Rotation as R
4847
from trimesh import Trimesh
49-
from rerun_sdk import MeshFormat, LogLevel
5048

5149

5250
def announcement(body: str):

examples/dicom/download_dataset.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import io
44
import os
5+
import zipfile
56
from pathlib import Path
67
from typing import Final
7-
import zipfile
88

99
import requests
1010

11-
1211
DATASET_DIR: Final = Path(os.path.dirname(__file__)) / "dataset"
1312

1413

examples/dicom/main.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#!/usr/bin/env python3
2-
"""Example using a vicom MRI scan.
2+
"""Example using a dicom MRI scan.
33
44
Setup:
55
``` sh
6-
python3 examples/vicom/download_dataset.py
6+
python3 examples/dicom/download_dataset.py
77
```
88
99
Run:
1010
``` sh
11-
python3 examples/vicom/main.py
11+
python3 examples/dicom/main.py
1212
```
1313
"""
1414

1515
import argparse
16-
from typing import Final, Iterable, Tuple
17-
import pydicom as dicom
18-
import dicom_numpy # type: ignore
19-
import numpy as np
2016
import os
2117
from pathlib import Path
22-
import numpy.typing as npt
18+
from typing import Final, Iterable, Tuple
2319

20+
import dicom_numpy # type: ignore
21+
import numpy as np
22+
import numpy.typing as npt
23+
import pydicom as dicom
2424
import rerun_sdk as rerun
2525

2626
DATASET_DIR: Final = Path(os.path.dirname(__file__)) / "dataset"
@@ -42,7 +42,7 @@ def list_dicom_files(dir: Path) -> Iterable[Path]:
4242
yield Path(path) / f
4343

4444

45-
def read_and_log_vicom_dataset():
45+
def read_and_log_dicom_dataset():
4646
dicom_files = list_dicom_files(DATASET_DIR)
4747
voxels_volume, _ = extract_voxel_data(dicom_files)
4848
voxels_volume = voxels_volume.astype('uint16') # the data is i16, but in range [0, 536].
@@ -69,7 +69,7 @@ def main() -> None:
6969
# which is `127.0.0.1:9876`.
7070
rerun.connect(args.addr)
7171

72-
read_and_log_vicom_dataset()
72+
read_and_log_dicom_dataset()
7373

7474
if args.save is not None:
7575
rerun.save(args.save)

examples/multiprocessing/main.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import rerun_sdk as rerun
88

9+
910
def task(title):
1011
# All processes spawned with `multiprocessing` will automatically
1112
# be assigned the same default recording_id.

examples/nyud/main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
"""
1616

1717
import argparse
18+
import zipfile
1819
from dataclasses import dataclass
20+
from datetime import datetime
1921
from pathlib import Path
2022
from typing import Final, Tuple
21-
import zipfile
22-
from datetime import datetime
23+
2324
import cv2
2425
import numpy as np
25-
2626
import rerun_sdk as rerun
2727

2828
# Logging depth images is slow, so we don't log every frame

examples/objectron/download_dataset.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python3
22

3-
import requests
43
import os
54

5+
import requests
6+
67

78
def download(url, path):
89
if not os.path.exists(path):

examples/objectron/main.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
import math
2323
import os
2424
import sys
25-
26-
import numpy as np
27-
import rerun_sdk as rerun
28-
2925
from dataclasses import dataclass
3026
from pathlib import Path
31-
from typing import List, Final, Iterator, Iterable
27+
from typing import Final, Iterable, Iterator, List
3228

29+
import numpy as np
30+
import rerun_sdk as rerun
31+
from proto.objectron.proto import (
32+
ARCamera,
33+
ARFrame,
34+
ARPointCloud,
35+
FrameAnnotation,
36+
Object,
37+
ObjectType,
38+
Sequence,
39+
)
3340
from rerun_sdk import ImageFormat
3441
from scipy.spatial.transform import Rotation as R
35-
from proto.objectron.proto import \
36-
ARFrame, ARCamera, ARPointCloud, Sequence, Object, ObjectType, FrameAnnotation
37-
3842

3943
IMAGE_RESOLUTION: Final = (1440, 1920)
4044
GEOMETRY_FILENAME: Final = "geometry.pbdata"

justfile

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ py-build:
2222
2323
# Run autoformatting
2424
py-format:
25-
isort .
26-
black .
25+
black --config rerun_py/pyproject.toml .
2726
blackdoc .
27+
isort .
2828
pyupgrade --py37-plus `find rerun_sdk/ tests/ -name "*.py" -type f`
2929
cargo fmt --all
3030

3131
# Run linting
3232
py-lint:
33-
#!/usr/bin/env bash
34-
cd rerun_py
33+
black --check --config rerun_py/pyproject.toml .
34+
blackdoc --check .
35+
isort --check .
3536
mypy
3637
flake8
3738

rerun_py/pyproject.toml

+5-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ keywords = ["computer-vision", "logging", "rerun"]
1717
license = { text = "MIT OR Apache-2.0" }
1818
requires-python = ">=3.7"
1919

20-
dependencies = ["numpy"]
20+
dependencies = ["numpy==1.23"]
2121

2222
[project.optional-dependencies]
2323
tests = ["pytest==7.1.2"]
@@ -30,12 +30,7 @@ repository = "https://github.com/rerun-io/rerun"
3030
[tool.maturin]
3131
locked = true
3232

33-
[tool.isort]
34-
profile = "black"
35-
36-
[tool.mypy]
37-
files = ["rerun_sdk", "tests"]
38-
namespace_packages = true
39-
show_error_codes = true
40-
strict = true
41-
enable_error_code = ["redundant-expr", "truthy-bool", "ignore-without-code"]
33+
[tool.black]
34+
line-length = 120
35+
target-version = ['py38']
36+
extend-exclude = "examples"

rerun_py/requirements-lint.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ isort==5.10.1
1010
mypy==0.971
1111
pyupgrade==2.37.3
1212
types-requests==2.28.10
13+
numpy==1.23 # For mypy plugin

0 commit comments

Comments
 (0)