Skip to content

fix: handle missing canprint attribute in Flickr API usage element#91

Merged
DaxServer merged 3 commits into
mainfrom
fix/flickr-missing-canprint-usage-attrs
Jun 7, 2026
Merged

fix: handle missing canprint attribute in Flickr API usage element#91
DaxServer merged 3 commits into
mainfrom
fix/flickr-missing-canprint-usage-attrs

Conversation

@DaxServer

Copy link
Copy Markdown
Owner

Problem

Some Flickr photos return a <usage> element without the canprint attribute (and potentially other attributes like candownload, canblog, canshare). The flickr-photos-api library (v3.12.1) does a hard dict access usage_elem.attrib["canprint"] with no fallback, raising KeyError: 'canprint'. This was caught by the broad except Exception in get_flickr_photo and logged as:

ERROR [55315578212] 'canprint'

The result was that affected photos only received a bare Flickr photo ID claim, losing all richer metadata (creator, source, date, location).

The <usage> element is undocumented in the official Flickr API spec — the library added support for it in v3.4 (May 2025) assuming all attributes are always present. A similar fix was applied upstream for KeyError: 'realname' in v3.8.1.

Fix

Subclass FlickrApi as _PatchedFlickrApi and override parse_single_photo_info to fill in missing <usage> attributes with a default of "0" (conservative: cannot print/download/blog/share) before delegating to super(). This follows Python best practice of subclassing over direct method replacement.

The patch is scoped entirely to FlickrBot and is trivially removable once flickr-photos-api ships the upstream fix.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Jun 7, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

Safe to merge — the fix is correctly scoped, the upstream with_api_key factory uses cls(...) so the subclass override is guaranteed to be called at runtime, and both the fill-in and non-overwrite paths are covered by new tests.

The patch is a minimal, targeted workaround for a known upstream bug. The in-place XML mutation happens before the super() call, which is the correct ordering. The with_api_key classmethod in flickr-photos-api returns cls(client=client) (confirmed from source), so _PatchedFlickrApi.with_api_key(...) yields a _PatchedFlickrApi instance and the override will be invoked on every real call. New tests cover both the happy-path fill and the do-not-overwrite guard.

No files require special attention.

Important Files Changed

Filename Overview
src/wikibots/flickr.py Adds _PatchedFlickrApi subclass that safely fills missing <usage> attributes before delegating to super(); FlickrBot now instantiates it via with_api_key, which correctly returns a subclass instance.
tests/test_flickr_rate_limit.py Adds two focused unit tests for _PatchedFlickrApi: one confirms missing attributes are filled with "0", another confirms existing attributes are not overwritten. The make_bot mock target is updated from FlickrApi to _PatchedFlickrApi.

Sequence Diagram

sequenceDiagram
    participant FB as FlickrBot
    participant PA as _PatchedFlickrApi
    participant FA as FlickrApi (super)
    participant Flickr as Flickr HTTP API

    FB->>PA: get_single_photo_info(photo_id)
    PA->>Flickr: flickr.photos.getInfo
    Flickr-->>PA: XML info_resp (usage may be missing attrs)
    PA->>PA: parse_single_photo_info(info_resp, photo_id)
    Note over PA: find photo/usage element<br/>fill missing candownload/canblog/<br/>canprint/canshare with "0"
    PA->>FA: super().parse_single_photo_info(info_resp, photo_id)
    FA-->>PA: SinglePhotoInfo dict
    PA-->>FB: SinglePhotoInfo dict
Loading

Reviews (2): Last reviewed commit: "fix: sort imports in test file" | Re-trigger Greptile

Comment thread tests/test_flickr_rate_limit.py
DaxServer and others added 2 commits June 7, 2026 14:00
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@DaxServer DaxServer merged commit e8be7b2 into main Jun 7, 2026
8 checks passed
@DaxServer DaxServer deleted the fix/flickr-missing-canprint-usage-attrs branch June 7, 2026 12:04
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