Skip to content

Commit

Permalink
Enforce from __future__ import annotations in Python files (#2377)
Browse files Browse the repository at this point in the history
### What

Follow-up to #2361:

- Add a Ruff rule to check/add `from __future__ import annotations`
everywhere.
- Normalise a bunch of python files (mainly scripts) that the previous
PR missed.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [ ] I've included a screenshot or gif (if applicable)

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2377

<!-- pr-link-docs:start -->
Docs preview: https://rerun.io/preview/2c443d9/docs
Examples preview: https://rerun.io/preview/2c443d9/examples
<!-- pr-link-docs:end -->
  • Loading branch information
abey79 authored and emilk committed Jun 15, 2023
1 parent b75c4e4 commit 7419dfe
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 44 deletions.
9 changes: 5 additions & 4 deletions rerun_py/docs/gen_common_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
* [Helpers](helpers.md)
```
"""
from __future__ import annotations

import re
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Final, List, Optional
from typing import Final

import griffe
import mkdocs_gen_files
Expand All @@ -38,13 +39,13 @@
@dataclass
class Section:
title: str
module_summary: Optional[str]
func_list: List[str]
module_summary: str | None
func_list: list[str]


# This is the list of sections and functions that will be included in the index
# for each of them.
SECTION_TABLE: Final[List[Section]] = [
SECTION_TABLE: Final[list[Section]] = [
Section(
title="Initialization",
module_summary=None,
Expand Down
1 change: 1 addition & 0 deletions rerun_py/docs/gen_package_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
```
"""
from __future__ import annotations

from pathlib import Path

Expand Down
3 changes: 3 additions & 0 deletions rerun_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ select = [
[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "all"

[tool.ruff.isort]
required-imports = ["from __future__ import annotations"]

[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
2 changes: 2 additions & 0 deletions rerun_py/rerun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
real rerun module by adding it to the path and then, and then
replacing our own module content with it.
"""
from __future__ import annotations

import pathlib
import sys

Expand Down
2 changes: 2 additions & 0 deletions rerun_py/rerun_demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
real rerun module by adding it to the path and then, and then
replacing our own module content with it.
"""
from __future__ import annotations

import pathlib
import sys

Expand Down
2 changes: 2 additions & 0 deletions rerun_py/tests/unit/test_color_conversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test for color_conversion module."""
from __future__ import annotations

import numpy as np
from rerun.color_conversion import linear_to_gamma_u8_pixel, linear_to_gamma_u8_value

Expand Down
14 changes: 7 additions & 7 deletions scripts/build_demo_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

"""Build `demo.rerun.io`."""
from __future__ import annotations

import argparse
import http.server
Expand All @@ -10,7 +11,6 @@
import subprocess
import threading
from functools import partial
from typing import List

from jinja2 import Template

Expand All @@ -22,7 +22,7 @@ def __init__(
title: str,
description: str,
commit: str,
build_args: List[str],
build_args: list[str],
):
self.path = os.path.join("examples/python", name, "main.py")
self.name = name
Expand Down Expand Up @@ -56,7 +56,7 @@ def supports_save(self) -> bool:
return "script_add_args" in f.read()


def copy_static_assets(examples: List[Example]) -> None:
def copy_static_assets(examples: list[Example]) -> None:
# copy root
src = os.path.join(SCRIPT_PATH, "demo_assets/static")
dst = BASE_PATH
Expand All @@ -80,7 +80,7 @@ def build_wasm() -> None:
subprocess.run(["cargo", "r", "-p", "re_build_web_viewer", "--", "--release"])


def copy_wasm(examples: List[Example]) -> None:
def copy_wasm(examples: list[Example]) -> None:
files = ["re_viewer_bg.wasm", "re_viewer.js"]
for example in examples:
for file in files:
Expand All @@ -90,7 +90,7 @@ def copy_wasm(examples: List[Example]) -> None:
)


def collect_examples() -> List[Example]:
def collect_examples() -> list[Example]:
commit = os.environ.get("COMMIT_HASH") or "main"
logging.info(f"Commit hash: {commit}")
examples = []
Expand All @@ -107,14 +107,14 @@ def collect_examples() -> List[Example]:
return examples


def save_examples_rrd(examples: List[Example]) -> None:
def save_examples_rrd(examples: list[Example]) -> None:
logging.info("\nSaving examples as .rrd")

for example in examples:
example.save()


def render_examples(examples: List[Example]) -> None:
def render_examples(examples: list[Example]) -> None:
logging.info("\nRendering examples")

template_path = os.path.join(SCRIPT_PATH, "demo_assets/templates/example.html")
Expand Down
1 change: 1 addition & 0 deletions scripts/check_requirements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from __future__ import annotations

import os
from glob import glob
Expand Down
1 change: 1 addition & 0 deletions scripts/fetch_crashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
```
"""
from __future__ import annotations

import argparse
import json
Expand Down
13 changes: 7 additions & 6 deletions scripts/generate_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
The result can be copy-pasted into CHANGELOG.md, though it often needs some manual editing too.
"""
from __future__ import annotations

import multiprocessing
import re
import sys
from dataclasses import dataclass
from typing import Any, List, Optional
from typing import Any

import requests
from git import Repo # pip install GitPython
Expand All @@ -34,14 +35,14 @@
class PrInfo:
gh_user_name: str
pr_title: str
labels: List[str]
labels: list[str]


@dataclass
class CommitInfo:
hexsha: str
title: str
pr_number: Optional[int]
pr_number: int | None


def get_github_token() -> str:
Expand All @@ -66,15 +67,15 @@ def get_github_token() -> str:


# Slow
def fetch_pr_info_from_commit_info(commit_info: CommitInfo) -> Optional[PrInfo]:
def fetch_pr_info_from_commit_info(commit_info: CommitInfo) -> PrInfo | None:
if commit_info.pr_number is None:
return None
else:
return fetch_pr_info(commit_info.pr_number)


# Slow
def fetch_pr_info(pr_number: int) -> Optional[PrInfo]:
def fetch_pr_info(pr_number: int) -> PrInfo | None:
url = f"https://api.github.com/repos/{OWNER}/{REPO}/pulls/{pr_number}"
gh_access_token = get_github_token()
headers = {"Authorization": f"Token {gh_access_token}"}
Expand All @@ -99,7 +100,7 @@ def get_commit_info(commit: Any) -> CommitInfo:
return CommitInfo(hexsha=commit.hexsha, title=commit.summary, pr_number=None)


def print_section(title: str, items: List[str]) -> None:
def print_section(title: str, items: list[str]) -> None:
if 0 < len(items):
print(f"#### {title}")
for line in items:
Expand Down
5 changes: 3 additions & 2 deletions scripts/generate_pr_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
Requires the following packages:
pip install google-cloud-storage Jinja2 PyGithub # NOLINT
"""
from __future__ import annotations

import argparse
import io
import os
from typing import Any, Dict
from typing import Any

from github import Github # NOLINT
from google.cloud import storage
Expand All @@ -40,7 +41,7 @@ def generate_pr_summary(github_token: str, github_repository: str, pr_number: in
commit_short = commit[:7]
print(f"Checking commit: {commit_short}...")

found: Dict[str, Any] = {}
found: dict[str, Any] = {}

# Check if there is a hosted app for the current commit
app_blob = viewer_bucket.blob(f"commit/{commit_short}/index.html")
Expand Down
5 changes: 3 additions & 2 deletions scripts/generate_prerelease_pip_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
Requires the following packages:
pip install google-cloud-storage Jinja2 PyGithub # NOLINT
"""
from __future__ import annotations

import argparse
import io
import os
from typing import Any, Dict
from typing import Any

from google.cloud import storage
from jinja2 import Template
Expand All @@ -30,7 +31,7 @@ def generate_pip_index(commit: str, upload: bool) -> None:
commit_short = commit[:7]
print(f"Checking commit: {commit_short}...")

found: Dict[str, Any] = {}
found: dict[str, Any] = {}

# Get the wheel files for the commit
wheel_blobs = list(wheels_bucket.list_blobs(prefix=f"commit/{commit_short}/wheels"))
Expand Down
9 changes: 5 additions & 4 deletions scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
Adding "NOLINT" to any line makes the linter ignore that line.
"""
from __future__ import annotations

import argparse
import os
import re
import sys
from typing import Any, List, Optional, Tuple
from typing import Any

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

Expand All @@ -22,7 +23,7 @@
nb_prefix = re.compile(r"nb_")


def lint_line(line: str) -> Optional[str]:
def lint_line(line: str) -> str | None:
if "NOLINT" in line:
return None # NOLINT ignores the linter

Expand Down Expand Up @@ -166,7 +167,7 @@ def is_empty(line: str) -> bool:
return False


def lint_vertical_spacing(lines_in: List[str]) -> Tuple[List[str], List[str]]:
def lint_vertical_spacing(lines_in: list[str]) -> tuple[list[str], list[str]]:
"""Only for Rust files."""
prev_line = None

Expand Down Expand Up @@ -266,7 +267,7 @@ def test_lint_vertical_spacing() -> None:
re_workspace_dep = re.compile(r"workspace\s*=\s*(true|false)")


def lint_workspace_deps(lines_in: List[str]) -> Tuple[List[str], List[str]]:
def lint_workspace_deps(lines_in: list[str]) -> tuple[list[str], list[str]]:
"""Only for Cargo files."""

errors = []
Expand Down
1 change: 1 addition & 0 deletions scripts/pr_link_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Requires the following packages:
pip install PyGithub # NOLINT
"""
from __future__ import annotations

import argparse

Expand Down
Loading

0 comments on commit 7419dfe

Please sign in to comment.