Skip to content

Commit 20d0460

Browse files
committed
Improve the ability to run make pr
1 parent 7e13eba commit 20d0460

3 files changed

Lines changed: 83 additions & 69 deletions

File tree

CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ The project includes a `dev` script that provides the following commands:
124124
|--------|-------------|
125125
| `poetry run dev init-dev` | Initialize development environment |
126126
| `poetry run dev install-dev` | Install package in editable mode |
127-
| `poetry run dev format` | Format code with shed |
128-
| `poetry run dev lint` | Run linting checks (read-only) |
127+
| `poetry run dev format` | Format code with black and ruff |
128+
| `poetry run dev lint` | Run flake8 linting (read-only) |
129129
| `poetry run dev lint-fix` | Run linting and fix issues automatically |
130130
| `poetry run dev flake8` | Run flake8 linting only |
131+
| `poetry run dev ruff` | Run ruff linting only |
131132
| `poetry run dev check` | Run all static analysis checks |
132133
| `poetry run dev test` | Run unit tests |
133134
| `poetry run dev test-unit` | Run unit tests with coverage |

terminusdb_client/scripts/dev.py

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
These scripts provide centralized development commands through Poetry.
55
"""
66

7+
import glob
78
import os
89
import shutil
910
import subprocess
1011
import sys
1112
from pathlib import Path
1213

14+
# Constants
15+
PYTHON_CLIENT_DIR = "terminusdb_client/"
16+
RUNNING_FLAKE8_MSG = "Running flake8..."
17+
RUNNING_RUFF_MSG = "Running ruff..."
18+
RUFF_WARNING_MSG = "⚠️ Ruff found some issues it couldn't fix automatically:\n Run 'poetry run dev ruff' to see all issues"
1319
# Constants for pytest arguments
1420
PYTEST_TB_SHORT = "--tb=short"
1521
PYTEST_COV = "--cov=terminusdb_client"
@@ -56,75 +62,64 @@ def install():
5662

5763

5864
def format():
59-
"""Format code with shed."""
65+
"""Format code with black and ruff (no auto-commits)."""
6066
print("Formatting code...")
61-
run_command(["poetry", "run", "shed"])
67+
# Find all Python files and format them
68+
python_files = glob.glob("terminusdb_client/**/*.py", recursive=True)
69+
if python_files:
70+
# Run black for formatting
71+
print("Running black...")
72+
run_command(["poetry", "run", "black"] + python_files)
73+
# Run ruff for import sorting and other fixes (ignore unfixed errors)
74+
print(RUNNING_RUFF_MSG)
75+
try:
76+
run_command(["poetry", "run", "ruff", "check", "--fix", PYTHON_CLIENT_DIR])
77+
except subprocess.CalledProcessError:
78+
print(RUFF_WARNING_MSG)
79+
else:
80+
print("No Python files found to format.")
6281

6382

6483
def lint():
6584
"""Run linting checks (read-only)."""
6685
print("Running linting checks...")
6786

68-
# Check code formatting with shed
69-
print("Checking code formatting with shed...")
70-
temp_dir = Path("/tmp/terminusdb-lint-check")
71-
temp_dir.mkdir(exist_ok=True)
72-
73-
try:
74-
# Copy code to temp directory
75-
src_dir = Path("terminusdb_client")
76-
if src_dir.exists():
77-
shutil.copytree(src_dir, temp_dir / src_dir.name, dirs_exist_ok=True)
78-
79-
# Initialize git repo in temp directory if not exists
80-
subprocess.run(["git", "init"], cwd=temp_dir, capture_output=True)
81-
subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=temp_dir, capture_output=True)
82-
subprocess.run(["git", "config", "user.name", "Test User"], cwd=temp_dir, capture_output=True)
83-
subprocess.run(["git", "add", "."], cwd=temp_dir, capture_output=True)
84-
subprocess.run(["git", "commit", "-m", "initial"], cwd=temp_dir, capture_output=True)
85-
86-
# Run shed in temp directory
87-
subprocess.run(
88-
["poetry", "run", "shed"],
89-
cwd=temp_dir,
90-
capture_output=True
91-
)
92-
93-
# Check if any files were modified
94-
git_result = subprocess.run(
95-
["git", "status", "--porcelain"],
96-
cwd=temp_dir,
97-
capture_output=True,
98-
text=True
99-
)
100-
101-
if git_result.returncode != 0 or git_result.stdout.strip():
102-
print("❌ Code formatting issues found. Run 'poetry run dev lint-fix' to fix.")
103-
sys.exit(1)
104-
else:
105-
print("✅ Code formatting is correct.")
106-
finally:
107-
# Clean up
108-
shutil.rmtree(temp_dir, ignore_errors=True)
109-
11087
# Run flake8
111-
print("Running flake8...")
88+
print(RUNNING_FLAKE8_MSG)
11289
run_command(["poetry", "run", "flake8", "terminusdb_client"])
11390

11491

11592
def flake8():
11693
"""Run flake8 linting only."""
117-
print("Running flake8...")
94+
print(RUNNING_FLAKE8_MSG)
11895
run_command(["poetry", "run", "flake8", "terminusdb_client"])
11996

12097

98+
def ruff():
99+
"""Run ruff linting only."""
100+
print(RUNNING_RUFF_MSG)
101+
run_command(["poetry", "run", "ruff", "check", PYTHON_CLIENT_DIR])
102+
103+
121104
def lint_fix():
122105
"""Run linting and fix issues automatically."""
123106
print("Running linting fixes...")
124107

125-
# Format code with shed
126-
print("Fixing code formatting with shed...")
127-
run_command(["poetry", "run", "shed"])
108+
# Format code with black and ruff
109+
print("Fixing code formatting...")
110+
python_files = glob.glob("terminusdb_client/**/*.py", recursive=True)
111+
if python_files:
112+
# Run black for formatting
113+
print("Running black...")
114+
run_command(["poetry", "run", "black"] + python_files)
115+
# Run ruff for import sorting and other fixes
116+
print(RUNNING_RUFF_MSG)
117+
try:
118+
run_command(["poetry", "run", "ruff", "check", "--fix", PYTHON_CLIENT_DIR])
119+
except subprocess.CalledProcessError:
120+
print(RUFF_WARNING_MSG)
121+
else:
122+
print("No Python files found to format.")
128123

129124
print("✅ Linting fixes completed!")
130125
print("Note: Some issues (like flake8 violations) may need manual fixes.")
@@ -253,19 +248,36 @@ def pr():
253248
# 1. Clean
254249
clean()
255250

256-
# 2. Format
257-
format()
251+
# 2. Check formatting (don't fix)
252+
print("\nChecking code formatting...")
253+
try:
254+
run_command(["poetry", "run", "black", "--check", "--diff", "terminusdb_client/"])
255+
print("✅ Black formatting is correct.")
256+
except subprocess.CalledProcessError:
257+
print("❌ Black formatting issues found.")
258+
print(" Run 'poetry run dev format' to fix formatting issues")
259+
sys.exit(1)
258260

259261
# 3. Lint
260262
lint()
263+
264+
# 4. Check ruff for any remaining issues
265+
print("\nChecking for ruff issues...")
266+
try:
267+
run_command(["poetry", "run", "ruff", "check", PYTHON_CLIENT_DIR])
268+
print("✅ No ruff issues found.")
269+
except subprocess.CalledProcessError:
270+
print("❌ Ruff issues found. Please fix them manually.")
271+
print(" Run 'poetry run dev ruff' to see all issues")
272+
sys.exit(1)
261273

262-
# 4. Run all tests
274+
# 5. Run all tests
263275
test_all()
264276

265277
print("\nAll PR preparation checks completed successfully!")
266278
print("\nSummary of checks performed:")
267-
print(" ✓ Code formatted with shed")
268-
print(" ✓ Linting passed (flake8, shed check)")
279+
print(" ✓ Code formatting is correct (black, ruff)")
280+
print(" ✓ Linting passed (flake8, ruff check)")
269281
print(" ✓ All tests passed (unit + integration)")
270282
print(" ✓ Coverage report generated")
271283
print("\nYour PR is ready for submission!")
@@ -279,10 +291,11 @@ def main():
279291
print("\nAvailable commands:")
280292
print(" init-dev - Initialize development environment")
281293
print(" install-dev - Install package in editable mode")
282-
print(" format - Format code with shed")
283-
print(" lint - Run linting checks (read-only)")
294+
print(" format - Format code with black and ruff")
295+
print(" lint - Run flake8 linting (read-only)")
284296
print(" lint-fix - Run linting and fix issues automatically")
285297
print(" flake8 - Run flake8 linting only")
298+
print(" ruff - Run ruff linting only")
286299
print(" check - Run all static analysis checks")
287300
print(" test - Run unit tests")
288301
print(" test-unit - Run unit tests only")
@@ -302,10 +315,11 @@ def main():
302315
print("\nAvailable commands:")
303316
print(" init-dev - Initialize development environment")
304317
print(" install-dev - Install package in editable mode")
305-
print(" format - Format code with shed")
306-
print(" lint - Run linting checks (read-only)")
318+
print(" format - Format code with black and ruff")
319+
print(" lint - Run flake8 linting (read-only)")
307320
print(" lint-fix - Run linting and fix issues automatically")
308321
print(" flake8 - Run flake8 linting only")
322+
print(" ruff - Run ruff linting only")
309323
print(" check - Run all static analysis checks")
310324
print(" test - Run unit tests")
311325
print(" test-unit - Run unit tests only")
@@ -326,6 +340,7 @@ def main():
326340
"lint": lint,
327341
"lint-fix": lint_fix,
328342
"flake8": flake8,
343+
"ruff": ruff,
329344
"check": check,
330345
"test": test,
331346
"test-unit": test_unit,

terminusdb_client/scripts/scripts.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
from importlib import import_module
99

1010
import click
11+
import terminusdb_client.woqlschema.woql_schema as woqlschema
1112
from shed import shed
1213
from tqdm import tqdm
1314

14-
import terminusdb_client.woqlschema.woql_schema as woqlschema
15-
1615
from .. import woql_type as wt
1716
from ..client.Client import Client
1817
from ..errors import InterfaceError
@@ -215,10 +214,8 @@ def add_docstring(self, obj_dict):
215214
print_script += '"""\n'
216215
for obj_str in dir(woqlschema):
217216
obj = eval(f"woqlschema.{obj_str}") # noqa: S307
218-
if (
219-
isinstance(obj, woqlschema.TerminusClass)
220-
or isinstance(obj, enum.EnumMeta)
221-
or isinstance(obj, type(woqlschema.TerminusKey))
217+
if isinstance(
218+
obj, (woqlschema.TerminusClass, enum.EnumMeta, type(woqlschema.TerminusKey))
222219
):
223220
import_objs.append(obj_str)
224221
print_script += "from typing import List, Optional, Set\n"
@@ -353,7 +350,7 @@ def commit(message):
353350
)
354351
for obj_str in dir(schema_plan):
355352
obj = eval(f"schema_plan.{obj_str}") # noqa: S307
356-
if isinstance(obj, woqlschema.TerminusClass) or isinstance(obj, enum.EnumMeta):
353+
if isinstance(obj, (woqlschema.TerminusClass, enum.EnumMeta)):
357354
if obj_str not in ["DocumentTemplate", "EnumTemplate", "TaggedUnion"]:
358355
schema_obj.add_obj(obj.__name__, obj)
359356
if message is None:
@@ -469,7 +466,7 @@ def _df_to_schema(class_name, df):
469466
converted_type = class_name
470467
else:
471468
converted_type = np_to_buildin[dtype.type]
472-
if converted_type == object:
469+
if converted_type is object:
473470
converted_type = str # pandas treats all string as objects
474471
converted_type = wt.to_woql_type(converted_type)
475472

@@ -647,7 +644,8 @@ def exportcsv(class_obj, keepid, maxdep, filename=None):
647644
def alldocs(schema, type_, query, head, export, keepid, maxdep, filename=None):
648645
"""Get all documents in the database, use --schema to specify schema, --type to select type and -q to make queries (e.g. -q date=2021-07-01)
649646
650-
If using --type and not --schema, can export using -e with options: --keepid, --maxdep and --filename"""
647+
If using --type and not --schema, can export using -e with options: --keepid, --maxdep and --filename
648+
"""
651649
settings = _load_settings()
652650
status = _load_settings(".TDB", check=[])
653651
settings.update(status)

0 commit comments

Comments
 (0)