Skip to content

Fix trailing whitespace and a shadowed gettext _ in AssociationsTool#996

Merged
GaryGriffin merged 2 commits into
gramps-project:maintenance/gramps60from
eduralph:fix/base-lint-gramps60
Jul 24, 2026
Merged

Fix trailing whitespace and a shadowed gettext _ in AssociationsTool#996
GaryGriffin merged 2 commits into
gramps-project:maintenance/gramps60from
eduralph:fix/base-lint-gramps60

Conversation

@eduralph

@eduralph eduralph commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Problem

Two pre-existing defects on maintenance/gramps60 that the addon CI pipeline (#820) surfaces the moment its lint gate runs:

  1. Trailing whitespace on three tracked Python files (27 lines): ArchiveAssist/ArchiveAssist.py (22), FilterRules/matcheventfilterrole.py (4), FilterRules/matchparentoffilterfamily.py (1). The pipeline's trailing-whitespace check fails on them.

  2. A real latent bug in AssociationsTool: _build_with_progress() uses for _ in range(batch_size), which makes _ a function-local throughout the method and shadows the module-level _ = _trans.gettext. On the error paths this raises UnboundLocalError (the _("Error initializing data: %s") handler, which references _ before the loop binds it) and TypeError (the in-loop _("Error processing person …") handler, which calls _ after the loop bound it to an int). ruff F82 flags it.

Fix

  • Strip the trailing whitespace (whitespace-only; git diff -w is empty).
  • Rename the unused loop counter to _batch_step so _ resolves to the gettext function everywhere in the method.

Four files, no pipeline/CI files. Splitting these out of #820 so that PR stays pipeline-only; #820's lint lane goes green once this merges and is merged back. Found while running #820's own gates against the tree during an adversarial review of that PR.

eduralph added 2 commits July 20, 2026 02:10
The addons-source CI (PR 820) adds a lint step that fails on any tracked
Python file carrying trailing whitespace. Three pre-existing files trip it
(27 lines total): ArchiveAssist/ArchiveAssist.py (22), and two FilterRules
modules (5). Strip the trailing whitespace so the gate can pass; whitespace
only, no behavioural change (git diff -w is empty).
_build_with_progress used `for _ in range(batch_size)`, which makes _
a local variable throughout the whole method (Python binds it at compile
time). That shadows the module-level `_ = _trans.gettext`, so:

  - the except handler at the top of the method, LOG.error(_("Error
    initializing data: %s") % str(e)), runs before the loop assigns _
    and raises UnboundLocalError; and
  - the in-loop handler LOG.warning(_("Error processing person %s: %s"
    % ...)) calls _ after the loop bound it to an int, raising TypeError.

Both fire only on the error paths, so they slipped through. The loop
counter is unused; rename it to _batch_step so _ resolves to the module
gettext everywhere. ruff (E9,F63,F7,F82) is clean on the module.

This also clears the F82x lint error PR 820's ruff gate reports on the
current tree.
@romjeromealt

Copy link
Copy Markdown
Contributor

A real latent bug in AssociationsTool: _build_with_progress() uses for _ in range(batch_size), which makes _ a function-local throughout the method and shadows the module-level _ = _trans.gettext. On the error paths this raises UnboundLocalError (the _("Error initializing data: %s") handler, which references _ before the loop binds it) and TypeError (the in-loop _("Error processing person …") handler, which calls _ after the loop bound it to an int). ruff F82 flags it.

My bad, I did not see it during review for #976
Also, testing was under gramps52. Also, as translation strings (related to _ variable) were loaded at first runtime (or addon load), such backend hidden method did not always raise an error? e.g., batch size < 50. Anyway, good catch. Thanks!

@romjeromealt

romjeromealt commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

There was a blind change (according to my working branch) on #967 (gramps52)
Also read that it seems to have a change on translation handling into gramps60 and +.

IA pointed out possible issues, but this one was not explicit (there was two locations...), despite matched.

Solution:

    Never use _ or LOG as variable names in loops or unpacking.
    Use explicit variable names (e.g., privacy, citation, note) if you must unpack tuples.
    Better yet: avoid unpacking entirely by using the public API (Fix 1).

Example of What to Avoid:

for ref in person.serialize()[-1]:
    _, _, _, two, value = ref  # ❌ Shadows gettext's _
    # or
    LOG, _, _, two, value = ref  # ❌ Shadows the LOG logger

#967 (comment)

@eduralph

Copy link
Copy Markdown
Contributor Author

A real latent bug in AssociationsTool: _build_with_progress() uses for _ in range(batch_size), which makes _ a function-local throughout the method and shadows the module-level _ = _trans.gettext. On the error paths this raises UnboundLocalError (the _("Error initializing data: %s") handler, which references _ before the loop binds it) and TypeError (the in-loop _("Error processing person …") handler, which calls _ after the loop bound it to an int). ruff F82 flags it.

My bad, I did not see it during review for #976 Also, testing was under gramps52. Also, as translation strings (related to _ variable) were loaded at first runtime (or addon load), such backend hidden method did not always raise an error? e.g., batch size < 50. Anyway, good catch. Thanks!

No worries - it surfaced during my work on a CI for addons-source, so it's doing what it's supposed to. :-)

@GaryGriffin
GaryGriffin merged commit 6c3d732 into gramps-project:maintenance/gramps60 Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants