Skip to content

Commit 3bd831c

Browse files
authored
Stricter linting (#6)
1 parent c3dfd93 commit 3bd831c

File tree

3 files changed

+80
-33
lines changed

3 files changed

+80
-33
lines changed

.github/workflows/ci.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
tags:
9+
- 'v*'
10+
pull_request:
11+
branches:
12+
- main
13+
14+
jobs:
15+
16+
lint:
17+
name: Linting
18+
timeout-minutes: 5
19+
runs-on: ubuntu-latest
20+
strategy:
21+
fail-fast: false
22+
steps:
23+
- uses: actions/checkout@v5
24+
- name: Set up Python
25+
uses: actions/setup-python@v6
26+
with:
27+
python-version: '3.13'
28+
- name: Install dependencies
29+
run: |
30+
python -m pip install --upgrade pip
31+
pip install ruff
32+
- name: Ruff lint
33+
run: |
34+
ruff check --output-format=github .
35+
- name: Ruff format
36+
run: |
37+
ruff format --check .
38+

_version.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
_version.py v1.3
2+
_version.py v1.4
33
44
Simple version string management, using a hard-coded version string
55
for simplicity and compatibility, while adding git info at runtime.
@@ -14,11 +14,12 @@
1414
* On a new release, you just update the __version__.
1515
"""
1616

17+
# ruff: noqa: RUF100, S310, PLR2004, D400, D415, S603, BLE001
18+
1719
import logging
1820
import subprocess
1921
from pathlib import Path
2022

21-
2223
# This is the base version number, to be bumped before each release.
2324
# The build system detects this definition when building a distribution.
2425
__version__ = "0.0.0"
@@ -35,17 +36,15 @@
3536
repo_dir = repo_dir if repo_dir.joinpath(".git").exists() else None
3637

3738

38-
def get_version():
39+
def get_version() -> str:
3940
"""Get the version string."""
4041
if repo_dir:
4142
return get_extended_version()
42-
else:
43-
return __version__
43+
return __version__
4444

4545

46-
def get_extended_version():
46+
def get_extended_version() -> str:
4747
"""Get an extended version string with information from git."""
48-
4948
release, post, labels = get_version_info_from_git()
5049

5150
# Sample first 3 parts of __version__
@@ -55,9 +54,9 @@ def get_extended_version():
5554
if not release:
5655
release = base_release
5756
elif release != base_release:
58-
logger.warning(
57+
warning(
5958
f"{project_name} version from git ({release})"
60-
+ f" and __version__ ({base_release}) don't match."
59+
f" and __version__ ({base_release}) don't match."
6160
)
6261

6362
# Build the total version
@@ -72,14 +71,14 @@ def get_extended_version():
7271
return version
7372

7473

75-
def get_version_info_from_git():
76-
"""Get (release, post, labels) from Git.
74+
def get_version_info_from_git() -> str:
75+
"""
76+
Get (release, post, labels) from Git.
7777
7878
With `release` the version number from the latest tag, `post` the
7979
number of commits since that tag, and `labels` a tuple with the
8080
git-hash and optionally a dirty flag.
8181
"""
82-
8382
# Call out to Git
8483
command = [
8584
"git",
@@ -91,9 +90,11 @@ def get_version_info_from_git():
9190
"--first-parent",
9291
]
9392
try:
94-
p = subprocess.run(command, cwd=repo_dir, capture_output=True)
93+
p = subprocess.run(
94+
command, check=False, cwd=repo_dir, capture_output=True
95+
)
9596
except Exception as e:
96-
logger.warning(f"Could not get {project_name} version: {e}")
97+
warning(f"Could not get {project_name} version: {e}")
9798
p = None
9899

99100
# Parse the result into parts
@@ -103,7 +104,7 @@ def get_version_info_from_git():
103104
output = p.stdout.decode(errors="ignore")
104105
if p.returncode:
105106
stderr = p.stderr.decode(errors="ignore")
106-
logger.warning(
107+
warning(
107108
f"Could not get {project_name} version.\n\nstdout: "
108109
+ output
109110
+ "\n\nstderr: "
@@ -121,16 +122,23 @@ def get_version_info_from_git():
121122
return release, post, labels
122123

123124

124-
def _to_tuple(v):
125-
"""Convert __version__ to version_info tuple."""
125+
def version_to_tuple(v: str) -> tuple:
126126
v = __version__.split("+")[0] # remove hash
127127
return tuple(int(i) if i.isnumeric() else i for i in v.split("."))
128128

129129

130+
def prnt(m: str) -> None:
131+
sys.stdout.write(m + "\n")
132+
133+
134+
def warning(m: str) -> None:
135+
logger.warning(m)
136+
137+
130138
# Apply the versioning
131139
base_version = __version__
132140
__version__ = get_version()
133-
version_info = _to_tuple(__version__)
141+
version_info = version_to_tuple(__version__)
134142

135143

136144
# The CLI part
@@ -150,41 +158,39 @@ def _to_tuple(v):
150158
import urllib.request
151159

152160
_, *args = sys.argv
161+
this_file = Path(__file__)
153162

154-
if not args:
155-
print(f"{project_name} v{__version__}")
156-
157-
elif args[0] == "version":
158-
print(f"{project_name} v{__version__}")
163+
if not args or args[0] == "version":
164+
prnt(f"{project_name} v{__version__}")
159165

160166
elif args[0] == "bump":
161167
if len(args) != 2:
162168
sys.exit("Expected a version number to bump to.")
163169
new_version = args[1].lstrip("v") # allow '1.2.3' and 'v1.2.3'
164-
if not new_version.count(".") == 2:
170+
if new_version.count(".") != 2:
165171
sys.exit("Expected two dots in new version string.")
166172
if not all(s.isnumeric() for s in new_version.split(".")):
167173
sys.exit("Expected only numbers in new version string.")
168-
with open(__file__, "rb") as f:
174+
with this_file.open("rb") as f:
169175
text = ref_text = f.read().decode()
170176
text = text.replace(base_version, new_version, 1)
171-
with open(__file__, "wb") as f:
177+
with this_file.open("wb") as f:
172178
f.write(text.encode())
173-
print(f"Bumped version from '{base_version}' to '{new_version}'.")
179+
prnt(f"Bumped version from '{base_version}' to '{new_version}'.")
174180

175181
elif args[0] == "update":
176182
u = "https://raw.githubusercontent.com/pygfx/_version/main/_version.py"
177183
with urllib.request.urlopen(u) as f:
178184
text = ref_text = f.read().decode()
179185
text = text.replace("0.0.0", base_version, 1)
180186
text = text.replace("PROJECT_NAME", project_name, 1)
181-
with open(__file__, "wb") as f:
187+
with this_file.open("wb") as f:
182188
f.write(text.encode())
183-
print("Updated to the latest _version.py.")
189+
prnt("Updated to the latest _version.py.")
184190

185191
elif args[0].lstrip("-") in ["h", "help"]:
186-
print(CLI_USAGE)
192+
prnt(CLI_USAGE)
187193

188194
else:
189-
print(f"Unknown command for _version.py: {args[0]!r}")
190-
print("Use ``python _version.py help`` to see a list of options.")
195+
prnt(f"Unknown command for _version.py: {args[0]!r}")
196+
prnt("Use ``python _version.py help`` to see a list of options.")

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
line-length = 80
33

44
[tool.ruff.lint]
5-
select = ["F", "E", "W", "N", "B", "RUF", "TC"]
5+
# Run all the rules, we turn some rules off in the file itself.
6+
select = ["ALL"]
7+
# Ignore rules that are conflicting, and would otherwise produce a warning with 'ruff format'
8+
ignore = ["D203", "D212", "COM812"]

0 commit comments

Comments
 (0)