Skip to content

Annotate the property getters of Snak and Qualifiers - #1164

Merged
LeMyst merged 1 commit into
masterfrom
annotate-property-getters
Nov 1, 2026
Merged

LeMyst merged 1 commit into
masterfrom
annotate-property-getters

Conversation

@LeMyst

@LeMyst LeMyst commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Unblocks #1163 (Dependabot: mypy 2.4.0 → 2.4.1).

Why #1163 fails

mypy 2.4.1 checks the getter of a property that has a setter for disallow_untyped_defs. 2.4.0 skipped it: a getter with a setter is an overloaded definition, and the 2.4.1 change is most likely "Process overloads as a whole in two-phase type-checking" (python/mypy#22231). Five getters never had a return type, which #1103 missed because 2.4.0 didn't report them:

wikibaseintegrator/models/snak.py:20,29,48,56: error: Function is missing a return type annotation  [no-untyped-def]
wikibaseintegrator/models/qualifiers.py:19: error: Function is missing a return type annotation  [no-untyped-def]

Fix: their real types

An unannotated getter returns Any, so mypy never checked the code that used these values. Annotating them honestly brought up 51 errors, which this PR fixes rather than hides:

Getter Type Consequence
Snak.snaktype WikibaseSnakType none
Snak.hash str | None none
Qualifiers.qualifiers dict[str, list[Snak]] none
Snak.datavalue dict The setter turns None into {}, as __init__ already did, so a snak without a value always has an empty dict. Nothing in the library sets None. The now-useless isinstance(datavalue, dict) checks of fastrun are removed. Snak.get_json() was annotated dict[str, str] and is now dict[str, Any].
Snak.property_number str | None A snak can have no property. See below.

Snaks without a property

The claims, the qualifiers, the reference snaks and fastrun store a snak under its property. A snak without a property number was stored under None and sent as is to the Wikibase instance, which rejects it. Now:

  • Claims.add(), Qualifiers.add(), Snaks.add() (so Reference.add()) and fastrun raise ValueError: The snak has no property number (prop_nr) ...;
  • Claims.add() checks all the claims before changing anything;
  • Qualifiers.remove() of a qualifier without a property does nothing, like for any qualifier that isn't there.

Checks

  • mypy 2.4.1 on the package and the tests, with the test dependencies: Success: no issues found in 111 source files.
  • mypy 2.4.0 (the lock of this branch) and all the other CI checks pass locally, with 100 % coverage.
  • New tests: test/test_snak_property_number.py covers the ValueError in each container and in fastrun, that Claims.add() leaves nothing behind when it raises, the qualifier removal, and the datavalue normalization.

Once this is merged, Dependabot rebases #1163 and its lint job should pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MmrM92iiDGANkDhDv2Zugr

mypy 2.4.1 checks the getter of a property with a setter for
disallow_untyped_defs (Process overloads as a whole in two-phase
type-checking, #22231), which 2.4.0 skipped: the Dependabot bump to
2.4.1 (#1163) fails on the getters of Snak.snaktype, property_number,
hash, datavalue and of Qualifiers.qualifiers, which had no return type.

Unannotated, they returned Any, so the code using them was never
checked. Annotated with their real types:
- snaktype, hash and qualifiers: WikibaseSnakType, str | None and
  dict[str, list[Snak]];
- datavalue is a dict: the setter turns None into {}, as __init__
  already did, so a snak without a value always has an empty dict.
  The isinstance(datavalue, dict) checks of fastrun are removed, and
  Snak.get_json() is annotated dict[str, Any];
- property_number is str | None: a snak can have no property. The
  claims, qualifiers, reference snaks and fastrun store a snak under
  its property, and stored one without a property under None, sent as
  is to the Wikibase instance. They now raise ValueError for a snak
  without a property number (Claims.add() checks all the claims before
  changing anything), and removing a qualifier without a property does
  nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MmrM92iiDGANkDhDv2Zugr
@LeMyst
LeMyst merged commit 193973a into master Nov 1, 2026
19 checks passed
@LeMyst
LeMyst deleted the annotate-property-getters branch November 1, 2026 09:31
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.

1 participant