Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .github/workflows/normalise-handle-gate.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: Normalise handle gate

# Fails if any module under ``taosmd/`` contains more than one definition
# of the internal helper ``_normalise_handle``. Overload stubs decorated
# with ``@typing.overload`` or ``@overload`` are excluded. Files that
# cannot be decoded as UTF-8 are skipped with a warning.
# Fails if any top-level function or method name is defined more than once
# in the same module under ``taosmd/`` or ``tests/`` (e.g. shadowed test
# functions). ``@typing.overload`` stubs and ``@property`` getter /
# ``@<name>.setter`` / ``@<name>.getter`` / ``@<name>.deleter`` accessor
# pairs are excluded. Files that cannot be decoded as UTF-8 are skipped
# with a warning.
#
# See scripts/normalise_handle_gate.py for the implementation.

Expand All @@ -23,5 +25,5 @@ jobs:
run: uv python install 3.12
- name: Install deps
run: uv sync --python 3.12
- name: Check for duplicate _normalise_handle definitions
- name: Check for duplicate definitions in taosmd/ and tests/
run: python scripts/normalise_handle_gate.py
2 changes: 2 additions & 0 deletions changelog.d/tsk-35ydf3-generalise-duplicate-gate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Fixed
- Generalised the `normalise-handle-gate` CI check from the single name `_normalise_handle` to any top-level function or method name defined more than once in the same module, so shadowed test functions (and any other accidental redefinition) are reported. The scan now also covers `tests/`. `@typing.overload` stubs, `@property` getters, and `@<name>.setter` / `@<name>.getter` / `@<name>.deleter` accessor pairs are still excluded as legitimate same-name definitions.
120 changes: 94 additions & 26 deletions scripts/normalise_handle_gate.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
#!/usr/bin/env python3
"""Normalise handle gate.

Scans ``taosmd/`` for duplicate definitions of the internal helper
``_normalise_handle`` and fails if any module contains more than one.
``@typing.overload`` stubs are excluded: they are not rival copies.

Files that cannot be decoded as UTF-8 are skipped with a warning rather
than allowed to crash the gate.
"""Duplicate-definition gate.

Scans ``taosmd/`` and ``tests/`` for any top-level function or method name
defined more than once in the same module (scope) and fails if rival
definitions are found. The original gate only looked for the internal helper
``_normalise_handle``; this one examines every name so that shadowed test
functions -- and any other accidental redefinition -- are reported.

``@typing.overload`` stubs, ``@property`` getters, and ``@<name>.setter`` /
``@<name>.getter`` / ``@<name>.deleter`` accessories are legitimate same-name
pairs and are therefore excluded. Files that cannot be decoded as UTF-8 are
skipped with a warning rather than allowed to crash the gate.
"""
from __future__ import annotations

import ast
import sys
from collections import defaultdict
from dataclasses import dataclass, field
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent

TARGET_PATTERNS = ("taosmd/**/*.py", "tests/**/*.py")


@dataclass
class Duplicate:
name: str
scope: str
lines: list[int] = field(default_factory=list)


def _has_overload_decorator(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
for decorator in node.decorator_list:
Expand All @@ -34,42 +49,95 @@ def _has_overload_decorator(node: ast.FunctionDef | ast.AsyncFunctionDef) -> boo
return False


def _count_definitions(file_path: Path) -> int:
def _has_property_decorator(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
"""``@property`` getters and ``@<name>.setter`` / ``.getter`` /
``.deleter`` accessories always share their name with the companion
accessor, so they are never rival definitions."""
for decorator in node.decorator_list:
if isinstance(decorator, ast.Name) and decorator.id == "property":
return True
if isinstance(decorator, ast.Attribute) and decorator.attr in (
"setter",
"getter",
"deleter",
):
return True
return False


def _is_exempt(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
return _has_overload_decorator(node) or _has_property_decorator(node)


def _collect_definitions(
body: list[ast.stmt], class_path: tuple[str, ...] = ()
) -> list[tuple[str, str, int]]:
"""Yield ``(scope, name, lineno)`` for every non-exempt function/method.

``scope`` is ``"module"`` for a top-level function, ``"class Foo.Bar"``
for a method of ``Foo.Bar``. Nested functions (closures) are
intentionally NOT collected: they are neither top-level functions nor
methods, and same-name closures in different parents are legal.
"""
scope = "module" if not class_path else "class " + ".".join(class_path)
found: list[tuple[str, str, int]] = []
for node in body:
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
if not _is_exempt(node):
found.append((scope, node.name, node.lineno))
elif isinstance(node, ast.ClassDef):
found.extend(_collect_definitions(node.body, class_path + (node.name,)))
return found


def _duplicate_definitions(file_path: Path) -> list[Duplicate]:
try:
source = file_path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
print(
f"normalise-handle-gate: skipping {file_path}: unreadable",
file=sys.stderr,
)
return 0
return []

try:
tree = ast.parse(source)
except SyntaxError:
return 0
return []

count = 0
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
if node.name == "_normalise_handle" and not _has_overload_decorator(node):
count += 1
return count
defs: dict[tuple[str, str], list[int]] = defaultdict(list)
for scope, name, lineno in _collect_definitions(tree.body):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: _collect_definitions(tree.body) misses functions inside module-level control flow

_collect_definitions only iterates over tree.body, so functions defined inside module-level if, try, with, for, while, or match blocks are invisible to the gate. The previous implementation used ast.walk(tree) and would catch duplicate definitions anywhere in the module. This narrowing means shadowed tests or redefs inside conditional blocks at module scope will be silently missed.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

defs[(scope, name)].append(lineno)

duplicates: list[Duplicate] = [
Duplicate(name=name, scope=scope, lines=lines)
for (scope, name), lines in defs.items()
if len(lines) > 1
]
duplicates.sort(key=lambda d: d.lines[0])
return duplicates


def _iter_target_files() -> list[Path]:
files: list[Path] = []
for pattern in TARGET_PATTERNS:
files.extend(REPO_ROOT.glob(pattern))
return sorted(set(files))


def main(argv: list[str] | None = None) -> int:
failures: list[tuple[str, int]] = []
for file_path in sorted(REPO_ROOT.glob("taosmd/**/*.py")):
count = _count_definitions(file_path)
if count > 1:
rel = file_path.relative_to(REPO_ROOT)
failures.append((str(rel), count))
failures: list[tuple[str, Duplicate]] = []
for file_path in _iter_target_files():
for dup in _duplicate_definitions(file_path):
failures.append((str(file_path.relative_to(REPO_ROOT)), dup))

if failures:
print("NORMALISE_HANDLE GATE FAIL:")
for path, count in failures:
print("DUPLICATE-DEFINITION GATE FAIL:")
for path, dup in failures:
count = len(dup.lines)
print(
f" {path}: found {count} definitions; at most 1 is allowed"
f" {path}: {dup.scope} '{dup.name}' defined on lines "
f"{dup.lines}; found {count} definitions; at most 1 is allowed"
)
return 1

Expand Down
Loading