Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff for our python lints #1378

Merged
merged 9 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ jobs:
run: |
just py-lint

- name: Check requirements
run: |
just py-requirements

# ---------------------------------------------------------------------------

matrix-setup:
Expand Down
3 changes: 1 addition & 2 deletions examples/python/api_demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
import math

import numpy as np
import rerun as rr
from rerun.log.annotation import AnnotationInfo
from rerun.log.rects import RectFormat
from rerun.log.text import LoggingHandler, LogLevel
from scipy.spatial.transform import Rotation

import rerun as rr


def run_segmentation() -> None:
rr.set_time_seconds("sim_time", 1)
Expand Down
1 change: 0 additions & 1 deletion examples/python/car/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import cv2
import numpy as np
import numpy.typing as npt

import rerun as rr


Expand Down
1 change: 0 additions & 1 deletion examples/python/clock/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import Final, Tuple

import numpy as np

import rerun as rr

LENGTH_S: Final = 20.0
Expand Down
4 changes: 1 addition & 3 deletions examples/python/colmap/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import numpy as np
import numpy.typing as npt
import requests
import rerun as rr
from read_write_model import Camera, read_model
from tqdm import tqdm

import rerun as rr

DATASET_DIR: Final = Path(os.path.dirname(__file__)) / "dataset"
DATASET_URL_BASE: Final = "https://storage.googleapis.com/rerun-example-datasets/colmap"
# When dataset filtering is turned on, drop views with less than this many valid points.
Expand Down Expand Up @@ -55,7 +54,6 @@ def intrinsics_for_camera(camera: Camera) -> npt.NDArray[Any]:


def get_downloaded_dataset_path(dataset_name: str) -> Path:

dataset_url = f"{DATASET_URL_BASE}/{dataset_name}.zip"

recording_dir = DATASET_DIR / dataset_name
Expand Down
3 changes: 1 addition & 2 deletions examples/python/deep_sdf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@
import mesh_to_sdf
import numpy as np
import numpy.typing as npt
import rerun as rr
import trimesh
from download_dataset import AVAILABLE_MESHES, ensure_mesh_downloaded
from rerun.log.file import MeshFormat
from rerun.log.text import LogLevel
from trimesh import Trimesh

import rerun as rr

CACHE_DIR = Path(os.path.dirname(__file__)) / "cache"


Expand Down
1 change: 0 additions & 1 deletion examples/python/dicom/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import numpy.typing as npt
import pydicom as dicom
import requests

import rerun as rr

DATASET_DIR: Final = Path(os.path.dirname(__file__)) / "dataset"
Expand Down
3 changes: 1 addition & 2 deletions examples/python/dna/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
from math import tau

import numpy as np
import rerun as rr
from rerun_demo.data import build_color_spiral
from rerun_demo.util import bounce_lerp, interleave
from scipy.spatial.transform import Rotation

import rerun as rr

rr.init("DNA Abacus")

rr.spawn()
Expand Down
1 change: 0 additions & 1 deletion examples/python/minimal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""Demonstrates the most barebone usage of the Rerun SDK."""

import numpy as np

import rerun as rr

rr.spawn()
Expand Down
4 changes: 1 addition & 3 deletions examples/python/mp_pose/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import numpy as np
import numpy.typing as npt
import requests
from rerun.log.annotation import AnnotationInfo, ClassDescription

import rerun as rr
from rerun.log.annotation import AnnotationInfo, ClassDescription

EXAMPLE_DIR: Final = Path(os.path.dirname(__file__))
DATASET_DIR: Final = EXAMPLE_DIR / "dataset" / "pose_movement"
Expand All @@ -41,7 +40,6 @@ def track_pose(video_path: str, segment: bool) -> None:

with closing(VideoSource(video_path)) as video_source, mp_pose.Pose(enable_segmentation=segment) as pose:
for bgr_frame in video_source.stream_bgr():

rgb = cv.cvtColor(bgr_frame.data, cv.COLOR_BGR2RGB)
rr.set_time_seconds("time", bgr_frame.time)
rr.set_time_sequence("frame_idx", bgr_frame.idx)
Expand Down
3 changes: 1 addition & 2 deletions examples/python/multithreading/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import numpy as np
import numpy.typing as npt
from rerun.log.rects import RectFormat

import rerun as rr
from rerun.log.rects import RectFormat


def rect_logger(path: str, color: npt.NDArray[np.float32]) -> None:
Expand Down
3 changes: 1 addition & 2 deletions examples/python/nyud/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import numpy as np
import numpy.typing as npt
import requests
from tqdm import tqdm

import rerun as rr
from tqdm import tqdm

DEPTH_IMAGE_SCALING: Final = 1e4
DATASET_DIR: Final = Path(os.path.dirname(__file__)) / "dataset"
Expand Down
2 changes: 1 addition & 1 deletion examples/python/objectron/download_dataset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
from pathlib import Path
from typing import Final, List, Optional
from typing import Final, Optional

import cv2
import requests
Expand Down
5 changes: 2 additions & 3 deletions examples/python/objectron/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import numpy as np
import numpy.typing as npt
import rerun as rr
from download_dataset import (
ANNOTATIONS_FILENAME,
AVAILABLE_RECORDINGS,
Expand All @@ -38,8 +39,6 @@
from rerun.log.file import ImageFormat
from scipy.spatial.transform import Rotation as R

import rerun as rr


@dataclass
class SampleARFrame:
Expand Down Expand Up @@ -213,7 +212,7 @@ def log_frame_annotations(frame_times: List[float], frame_annotations: List[Fram
if len(keypoint_pos2s) == 9:
log_projected_bbox(f"world/camera/video/estimates/box-{obj_ann.object_id}", keypoint_pos2s)
else:
for (id, pos2) in zip(keypoint_ids, keypoint_pos2s):
for id, pos2 in zip(keypoint_ids, keypoint_pos2s):
rr.log_point(
f"world/camera/video/estimates/box-{obj_ann.object_id}/{id}",
pos2,
Expand Down
1 change: 0 additions & 1 deletion examples/python/plots/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from math import cos, sin, tau

import numpy as np

import rerun as rr


Expand Down
3 changes: 1 addition & 2 deletions examples/python/raw_mesh/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
from typing import Optional, cast

import numpy as np
import rerun as rr
import trimesh
from download_dataset import AVAILABLE_MESHES, ensure_mesh_downloaded

import rerun as rr


def load_scene(path: Path) -> trimesh.Scene:
print(f"loading scene {path}…")
Expand Down
1 change: 1 addition & 0 deletions examples/python/stable_diffusion/huggingface_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
logger.addHandler(rr.log.text.LoggingHandler("logs"))


# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.preprocess
def preprocess(image):
if isinstance(image, torch.Tensor):
Expand Down
3 changes: 1 addition & 2 deletions examples/python/stable_diffusion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"

import requests
import rerun as rr
import torch
from huggingface_pipeline import StableDiffusionDepth2ImgPipeline
from PIL import Image

import rerun as rr

EXAMPLE_DIR: Final = Path(os.path.dirname(__file__))
DATASET_DIR: Final = EXAMPLE_DIR / "dataset"
CACHE_DIR: Final = EXAMPLE_DIR / "cache"
Expand Down
3 changes: 1 addition & 2 deletions examples/python/tracking_hf_opencv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import numpy as np
import numpy.typing as npt
import requests
from PIL import Image

import rerun as rr
from PIL import Image

EXAMPLE_DIR: Final = Path(os.path.dirname(__file__))
DATASET_DIR: Final = EXAMPLE_DIR / "dataset" / "tracking_sequences"
Expand Down
8 changes: 3 additions & 5 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,18 @@ py-build:
py-format:
black --config rerun_py/pyproject.toml {{py_folders}}
blackdoc {{py_folders}}
isort {{py_folders}}
pyupgrade --py37-plus `find rerun_py/rerun/ -name "*.py" -type f`
ruff --fix --config rerun_py/pyproject.toml {{py_folders}}

# Check that all the requirements.txt files for all the examples are correct
py-requirements:
find examples/python/ -name main.py | xargs -I _ sh -c 'cd $(dirname _) && echo $(pwd) && pip-missing-reqs . || exit 255'

# Run linting
py-lint: py-requirements
py-lint:
jleibs marked this conversation as resolved.
Show resolved Hide resolved
black --check --config rerun_py/pyproject.toml --diff {{py_folders}}
blackdoc --check {{py_folders}}
isort --check {{py_folders}}
mypy --no-warn-unused-ignore
jleibs marked this conversation as resolved.
Show resolved Hide resolved
flake8 {{py_folders}}
ruff check --config rerun_py/pyproject.toml {{py_folders}}

# Run fast unittests
py-test:
Expand Down
2 changes: 0 additions & 2 deletions rerun_py/docs/gen_common_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def make_slug(s: str) -> str:


with mkdocs_gen_files.open(index_path, "w") as index_file:

# TODO(#1161): add links to our high-level docs!

# Hide the TOC for the index since it's identical to the left nav-bar
Expand All @@ -175,7 +174,6 @@ def make_slug(s: str) -> str:
)

for section in SECTION_TABLE:

# Turn the heading into a slug and add it to the nav
md_name = make_slug(section.title)
md_file = md_name + ".md"
Expand Down
32 changes: 32 additions & 0 deletions rerun_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ rerun = "rerun.__main__:main"
line-length = 120
target-version = ["py38"]

[tool.ruff]
extend-exclude = [
"venv/",
"target/",
"examples/python/objectron/proto/objectron/proto.py",
"examples/python/stable_diffusion/huggingface_pipeline.py",
"examples/python/colmap/read_write_model.py",
jleibs marked this conversation as resolved.
Show resolved Hide resolved
]
ignore = [
"D1",
"D107",
"D202",
"D203",
"D212",
"D401",
"D402",
"D415",
"D416",
jleibs marked this conversation as resolved.
Show resolved Hide resolved
]
line-length = 120
select = [
"D",
"E",
"F",
"I",
"TID",
"W",
jleibs marked this conversation as resolved.
Show resolved Hide resolved
]

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "all"

[tool.maturin]
# We use a python package from inside the rerun_sdk folder to avoid conflicting
# with the other `rerun` pypi package. The rerun_sdk.pth adds this to the pythonpath
Expand Down
11 changes: 2 additions & 9 deletions rerun_py/requirements-lint.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
black==22.8.0
black==23.1.0
blackdoc==0.3.6
flake8-bugbear==22.8.23
flake8-comprehensions==3.10.0
flake8-docstrings==1.6.0
flake8-simplify==0.19.3
flake8-tidy-imports==4.8.0
flake8==5.0.4
isort==5.10.1
mypy==0.971
numpy==1.23 # For mypy plugin
pip-check-reqs==2.4.3 # Checks for missing deps in requirements.txt files
pyupgrade==2.37.3
ruff=0.0.251
types-Deprecated==1.2.9
types-requests==2.28.10
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional

import rerun_bindings as bindings # type: ignore[attr-defined]

from rerun.log import log_cleared
from rerun.log.annotation import log_annotation_context
from rerun.log.arrow import log_arrow
Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import numpy.typing as npt
import pyarrow as pa

from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import numpy.typing as npt
import pyarrow as pa

from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory
from rerun.components.vec import Vec3DArray

Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import numpy.typing as npt
import pyarrow as pa

from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import numpy.typing as npt
import pyarrow as pa

from rerun.color_conversion import u8_array_to_rgba
from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory

Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import numpy.typing as npt
import pyarrow as pa

from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Sequence

import pyarrow as pa

from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions rerun_py/rerun_sdk/rerun/components/linestrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import numpy.typing as npt
import pyarrow as pa

from rerun.components import REGISTERED_COMPONENT_NAMES, ComponentTypeFactory

__all__ = [
Expand Down
Loading