Repository navigation
Annotate the property getters of Snak and Qualifiers - #1164
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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:Snak.snaktypeWikibaseSnakTypeSnak.hashstr | NoneQualifiers.qualifiersdict[str, list[Snak]]Snak.datavaluedictNoneinto{}, as__init__already did, so a snak without a value always has an empty dict. Nothing in the library setsNone. The now-uselessisinstance(datavalue, dict)checks of fastrun are removed.Snak.get_json()was annotateddict[str, str]and is nowdict[str, Any].Snak.property_numberstr | NoneSnaks 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
Noneand sent as is to the Wikibase instance, which rejects it. Now:Claims.add(),Qualifiers.add(),Snaks.add()(soReference.add()) and fastrun raiseValueError: 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
Success: no issues found in 111 source files.test/test_snak_property_number.pycovers theValueErrorin each container and in fastrun, thatClaims.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