Skip to content

Commit 2e6709f

Browse files
b-longCopilot
andauthored
chore(main): enforce & provide improved code comments (#121)
* chore(main): enforce pydocstyle & pydoclint broadly * run 'pre-commit autoupdate' * chore: update extension recommendations * Apply suggestion from @b-long * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @b-long * Apply suggestion from @b-long --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 09984b8 commit 2e6709f

File tree

105 files changed

+798
-541
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+798
-541
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ repos:
3434

3535
- repo: https://github.com/astral-sh/ruff-pre-commit
3636
# Ruff version.
37-
rev: v0.14.6
37+
rev: v0.14.8
3838
hooks:
3939
# Run the linter.
4040
- id: ruff-check

.vscode/extensions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"recommendations": [
3-
"golang.go"
3+
"charliermarsh.ruff"
44
]
55
}

conftest.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
Pytest configuration and fixtures for the OpenTDF Python SDK tests.
1+
"""Pytest configuration and fixtures for the OpenTDF Python SDK tests.
32
43
This module contains pytest hooks and fixtures that will be automatically
54
loaded by pytest when running tests.
@@ -14,13 +13,13 @@
1413

1514
@pytest.fixture(scope="session")
1615
def project_root(request) -> Path:
16+
"""Get project root directory."""
1717
return request.config.rootpath # Project root
1818

1919

2020
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
2121
def pytest_runtest_makereport(item, call):
22-
"""
23-
Hook that runs after each test phase (setup, call, teardown).
22+
"""Collect server logs when test fails after each test phase.
2423
2524
This hook automatically collects server logs when a test fails.
2625
"""
@@ -53,8 +52,7 @@ def pytest_runtest_makereport(item, call):
5352

5453
@pytest.fixture
5554
def collect_server_logs():
56-
"""
57-
Fixture that provides a function to manually collect server logs.
55+
"""Fixture that provides a function to manually collect server logs.
5856
5957
Usage:
6058
def test_something(collect_server_logs):

otdf-python-proto/scripts/generate_connect_proto.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
2-
"""
3-
Enhanced script to generate Python Connect RPC clients from .proto definitions.
2+
"""Enhanced script to generate Python Connect RPC clients from .proto definitions.
43
54
This script:
65
1. Downloads the latest proto files from OpenTDF platform
@@ -200,8 +199,7 @@ def create_init_files(generated_dir: Path) -> None:
200199

201200

202201
def _fix_ignore_if_default_value(proto_files_dir):
203-
"""
204-
TODO: Fix buf validation: Updated the proto files to use the correct enum value:
202+
"""TODO: Fix buf validation: Updated the proto files to use the correct enum value:
205203
206204
Changed IGNORE_IF_DEFAULT_VALUE → IGNORE_IF_ZERO_VALUE in:
207205
attributes.proto

pyproject.toml

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ norecursedirs = ["otdf-python-proto"]
7272

7373
[tool.ruff]
7474
line-length = 88
75+
target-version = "py310"
7576

7677
# See https://docs.astral.sh/ruff/rules/
7778
# for rule information.
@@ -80,28 +81,33 @@ lint.ignore = [
8081
"E501",
8182
]
8283
lint.select = [
83-
# pycodestyle checks.
84-
"E",
85-
"W",
86-
# pyflakes checks.
87-
"F",
88-
# flake8-bugbear checks.
89-
"B",
90-
# flake8-comprehensions checks.
91-
"C4",
92-
# McCabe complexity
93-
"C90",
94-
# isort
95-
"I",
96-
# Performance-related rules
97-
"PERF", # Ruff's performance rules
98-
"PTH", # pathlib (path handling)
99-
# Additional useful rules
100-
"UP", # pyupgrade (modern Python features)
101-
"SIM", # flake8-simplify (simplifications)
102-
"RUF", # Ruff-specific rules
103-
"FURB", # refurb (FURB)
104-
"PT018", # flake8-pytest-style (pytest style)
84+
"B", # flake8-bugbear
85+
"C4", # flake8-comprehensions
86+
"C90", # McCabe complexity
87+
"D", # pydocstyle
88+
"DOC", # pydoclint
89+
"E", # pycodestyle errors
90+
"F", # pyflakes
91+
"FURB", # refurb
92+
"I", # isort
93+
"PERF", # performance
94+
"PT018", # pytest style
95+
"PTH", # pathlib
96+
"Q", # flake8-quotes
97+
"RUF", # ruff-specific
98+
"SIM", # flake8-simplify
99+
"UP", # pyupgrade
100+
"W", # pycodestyle warnings
105101
]
106102
# Ignore generated files
107103
extend-exclude = ["otdf-python-proto/src/"]
104+
105+
[tool.ruff.lint.per-file-ignores]
106+
"tests/**" = ["D100", "D101", "D102", "D103", "D107", "D400", "D401", "D415"]
107+
"otdf-python-proto/**" = ["D"] # Ignore all D (docstring) rules for generated proto files
108+
109+
# TODO: Remaining work - 4 buckets to fix (140 errors remaining):
110+
# Bucket #1: D102 (missing method docstrings) - 98 errors
111+
# Bucket #2: D105 (missing magic method docstrings) - 23 errors
112+
# Bucket #3: D205 (blank line formatting), D103, D417, D104 - 19 errors
113+
"src/**" = ["D102", "D105", "D205"]

src/otdf_python/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
OpenTDF Python SDK
1+
"""OpenTDF Python SDK.
32
43
A Python implementation of the OpenTDF SDK for working with Trusted Data Format (TDF) files.
54
Provides both programmatic APIs and command-line interface for encryption and decryption.

src/otdf_python/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
2-
"""
3-
Main entry point for running otdf_python as a module.
2+
"""Main entry point for running otdf_python as a module.
43
54
This allows the package to be run with `python -m otdf_python` and properly
65
handles the CLI interface without import conflicts.

src/otdf_python/address_normalizer.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Address normalization utilities for OpenTDF.
3-
"""
1+
"""Address normalization utilities for OpenTDF."""
42

53
import logging
64
import re
@@ -12,8 +10,7 @@
1210

1311

1412
def normalize_address(url_string: str, use_plaintext: bool) -> str:
15-
"""
16-
Normalize a URL address to ensure it has the correct scheme and port.
13+
"""Normalize a URL address to ensure it has the correct scheme and port.
1714
1815
Args:
1916
url_string: The URL string to normalize
@@ -24,6 +21,7 @@ def normalize_address(url_string: str, use_plaintext: bool) -> str:
2421
2522
Raises:
2623
SDKException: If there's an error parsing or creating the URL
24+
2725
"""
2826
scheme = "http" if use_plaintext else "https"
2927

src/otdf_python/aesgcm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
"""AES-GCM encryption and decryption functionality."""
2+
13
import os
24

35
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
46

57

68
class AesGcm:
9+
"""AES-GCM encryption and decryption operations."""
10+
711
GCM_NONCE_LENGTH = 12
812
GCM_TAG_LENGTH = 16
913

1014
def __init__(self, key: bytes):
15+
"""Initialize AES-GCM cipher with key."""
1116
if not key or len(key) not in (16, 24, 32):
1217
raise ValueError("Invalid key size for GCM encryption")
1318
self.key = key
@@ -17,7 +22,10 @@ def get_key(self) -> bytes:
1722
return self.key
1823

1924
class Encrypted:
25+
"""Encrypted data with initialization vector and ciphertext."""
26+
2027
def __init__(self, iv: bytes, ciphertext: bytes):
28+
"""Initialize encrypted data."""
2129
self.iv = iv
2230
self.ciphertext = ciphertext
2331

src/otdf_python/assertion_config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
"""Assertion configuration for TDF."""
2+
13
from enum import Enum, auto
24
from typing import Any
35

46

57
class Type(Enum):
8+
"""Assertion type enumeration."""
9+
610
HANDLING_ASSERTION = "handling"
711
BASE_ASSERTION = "base"
812

@@ -11,6 +15,8 @@ def __str__(self):
1115

1216

1317
class Scope(Enum):
18+
"""Assertion scope enumeration."""
19+
1420
TRUSTED_DATA_OBJ = "tdo"
1521
PAYLOAD = "payload"
1622

@@ -19,12 +25,16 @@ def __str__(self):
1925

2026

2127
class AssertionKeyAlg(Enum):
28+
"""Assertion key algorithm enumeration."""
29+
2230
RS256 = auto()
2331
HS256 = auto()
2432
NOT_DEFINED = auto()
2533

2634

2735
class AppliesToState(Enum):
36+
"""Assertion applies-to state enumeration."""
37+
2838
ENCRYPTED = "encrypted"
2939
UNENCRYPTED = "unencrypted"
3040

@@ -33,14 +43,19 @@ def __str__(self):
3343

3444

3545
class BindingMethod(Enum):
46+
"""Assertion binding method enumeration."""
47+
3648
JWS = "jws"
3749

3850
def __str__(self):
3951
return self.value
4052

4153

4254
class AssertionKey:
55+
"""Assertion signing key configuration."""
56+
4357
def __init__(self, alg: AssertionKeyAlg, key: Any):
58+
"""Initialize assertion key."""
4459
self.alg = alg
4560
self.key = key
4661

@@ -49,7 +64,10 @@ def is_defined(self):
4964

5065

5166
class Statement:
67+
"""Assertion statement with format, schema, and value."""
68+
5269
def __init__(self, format: str, schema: str, value: str):
70+
"""Initialize assertion statement."""
5371
self.format = format
5472
self.schema = schema
5573
self.value = value
@@ -67,6 +85,8 @@ def __hash__(self):
6785

6886

6987
class AssertionConfig:
88+
"""TDF assertion configuration."""
89+
7090
def __init__(
7191
self,
7292
id: str,
@@ -76,6 +96,7 @@ def __init__(
7696
statement: Statement,
7797
signing_key: AssertionKey | None = None,
7898
):
99+
"""Initialize assertion configuration."""
79100
self.id = id
80101
self.type = type
81102
self.scope = scope

0 commit comments

Comments
 (0)