Skip to content

Commit

Permalink
Enable lint for not specifying encoding on opening files in python (r…
Browse files Browse the repository at this point in the history
…erun-io#5991)

### What

This fixes issues on windows where `open('somefile')` would use the
currently active encoding which may be governed my the locale / the
command line's configuration. This then causes issues loading utf8
encoded files.

The lint is part of Ruff's
[preview](https://docs.astral.sh/ruff/preview/) feature and therefore
requires enabling the `preview` option.
Unfortunately, this also changes the formatting despite specifying
`lint.explicit-preview-rules`, but looking over the changed formatting
it's consistently an improvement!

**Review commit by commit**


### 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)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5991?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5991?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/5991)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
Wumpf authored Apr 16, 2024
1 parent 85cb102 commit c83306c
Show file tree
Hide file tree
Showing 72 changed files with 2,027 additions and 2,587 deletions.
26 changes: 12 additions & 14 deletions docs/snippets/all/annotation_context_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@

rr.log(
"/",
rr.AnnotationContext(
[
ClassDescription(
info=0,
keypoint_annotations=[
(0, "zero", (255, 0, 0)),
(1, "one", (0, 255, 0)),
(2, "two", (0, 0, 255)),
(3, "three", (255, 255, 0)),
],
keypoint_connections=[(0, 2), (1, 2), (2, 3)],
)
]
),
rr.AnnotationContext([
ClassDescription(
info=0,
keypoint_annotations=[
(0, "zero", (255, 0, 0)),
(1, "one", (0, 255, 0)),
(2, "two", (0, 0, 255)),
(3, "three", (255, 255, 0)),
],
keypoint_connections=[(0, 2), (1, 2), (2, 3)],
)
]),
static=True,
)

Expand Down
2 changes: 1 addition & 1 deletion examples/python/arkit_scenes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@


def load_json(js_path: Path) -> dict[str, Any]:
with open(js_path) as f:
with open(js_path, encoding="utf8") as f:
json_data: dict[str, Any] = json.load(f)
return json_data

Expand Down
2 changes: 1 addition & 1 deletion examples/python/detect_and_track_objects/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def update_trackers_with_detections(


def track_objects(video_path: str, *, max_frame_count: int | None) -> None:
with open(COCO_CATEGORIES_PATH) as f:
with open(COCO_CATEGORIES_PATH, encoding="utf8") as f:
coco_categories = json.load(f)
class_descriptions = [
rr.AnnotationInfo(id=cat["id"], color=cat["color"], label=cat["name"]) for cat in coco_categories
Expand Down
2 changes: 1 addition & 1 deletion examples/python/external_data_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def main() -> None:
else:
entity_path = args.filepath

with open(args.filepath) as file:
with open(args.filepath, encoding="utf8") as file:
body = file.read()
text = f"""## Some Python code\n```python\n{body}\n```\n"""
rr.log(
Expand Down
10 changes: 4 additions & 6 deletions examples/python/human_pose_tracking/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ def track_pose(video_path: str, model_path: str, *, segment: bool, max_frame_cou
# Use a separate annotation context for the segmentation mask.
rr.log(
"video/mask",
rr.AnnotationContext(
[
rr.AnnotationInfo(id=0, label="Background"),
rr.AnnotationInfo(id=1, label="Person", color=(0, 0, 0)),
]
),
rr.AnnotationContext([
rr.AnnotationInfo(id=0, label="Background"),
rr.AnnotationInfo(id=1, label="Person", color=(0, 0, 0)),
]),
static=True,
)
rr.log("person", rr.ViewCoordinates.RIGHT_HAND_Y_DOWN, static=True)
Expand Down
36 changes: 15 additions & 21 deletions examples/python/open_photogrammetry_format/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,21 @@ def log_calibrated_cameras(self, jpeg_quality: int | None) -> None:
# https://pix4d.github.io/opf-spec/specification/calibrated_cameras.html#calibrated-camera
omega, phi, kappa = tuple(np.deg2rad(a) for a in calib_camera.orientation_deg)
rot = (
np.array(
[
[1, 0, 0],
[0, np.cos(omega), -np.sin(omega)],
[0, np.sin(omega), np.cos(omega)],
]
)
@ np.array(
[
[np.cos(phi), 0, np.sin(phi)],
[0, 1, 0],
[-np.sin(phi), 0, np.cos(phi)],
]
)
@ np.array(
[
[np.cos(kappa), -np.sin(kappa), 0],
[np.sin(kappa), np.cos(kappa), 0],
[0, 0, 1],
]
)
np.array([
[1, 0, 0],
[0, np.cos(omega), -np.sin(omega)],
[0, np.sin(omega), np.cos(omega)],
])
@ np.array([
[np.cos(phi), 0, np.sin(phi)],
[0, 1, 0],
[-np.sin(phi), 0, np.cos(phi)],
])
@ np.array([
[np.cos(kappa), -np.sin(kappa), 0],
[np.sin(kappa), np.cos(kappa), 0],
[0, 0, 1],
])
)

rr.log(entity, rr.Transform3D(translation=calib_camera.position, mat3x3=rot))
Expand Down
2 changes: 1 addition & 1 deletion examples/python/raw_mesh/download_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def download(url: str, path: Path) -> None:
resp = requests.get(url)
resp.raise_for_status()
os.makedirs(path.parent, exist_ok=True)
with open(path, "wb") as f:
with open(path, "wb", encoding="utf8") as f:
f.write(resp.content)

name = name.lower()
Expand Down
2 changes: 1 addition & 1 deletion rerun_py/docs/gen_common_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def all_archetypes() -> list[str]:
pattern = r'"([^"]*)"'

# Open the file for reading
with open(file_path) as file:
with open(file_path, encoding="utf8") as file:
# Read the file line by line
for line in file:
# Use re.findall to find all quoted strings in the line
Expand Down
24 changes: 17 additions & 7 deletions rerun_py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ rerun = "rerun_bindings:main"
# This is needed because otherwise ruff will not be able to trim whitespaces in (codegened) docstrings.
unsafe-fixes = true

# Allow preview lints to be enabled (like `PLW1514` to force `encoding` on open).
preview = true
# But we only want to opt-in to certain preview rules!
lint.explicit-preview-rules = true

extend-exclude = [
# Automatically generated test artifacts
"venv/",
Expand Down Expand Up @@ -101,13 +106,18 @@ lint.ignore = [

line-length = 120
lint.select = [
"D", # pydocstyle codes https://www.pydocstyle.org/en/latest/error_codes.html
"E", # pycodestyle error codes: https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
"F", # Flake8 error codes https://flake8.pycqa.org/en/latest/user/error-codes.html
"I", # Isort
"TID", # flake8-tidy-imports
"W", # pycodestyle warning codes: https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
"UP", # pyupgrade (ensures idomatic code for supported python version)
"D", # pydocstyle codes https://www.pydocstyle.org/en/latest/error_codes.html
"E", # pycodestyle error codes: https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
"F", # Flake8 error codes https://flake8.pycqa.org/en/latest/user/error-codes.html
"I", # Isort
"TID", # flake8-tidy-imports
"W", # pycodestyle warning codes: https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
"UP", # pyupgrade (ensures idomatic code for supported python version)
"PLW1514", # Force setting `encoding` for open calls. This is in order to prevent issues when opening utf8 files on windows where the default encoding may depend on the active locale. https://docs.astral.sh/ruff/rules/unspecified-encoding/
]

lint.unfixable = [
"PLW1514", # Automatic fix for `encoding` doesn't do what we want - it queries the locale for the preferred encoding which is exactly what we want to avoid.
]

[tool.ruff.lint.per-file-ignores]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions rerun_py/rerun_sdk/rerun/blueprint/components/container_kind.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions rerun_py/rerun_sdk/rerun/blueprint/components/corner2d.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c83306c

Please sign in to comment.