Skip to content

Fix pokemon moves lookup assuming contiguous ids#1603

Merged
Naramsim merged 1 commit into
PokeAPI:masterfrom
santichausis:fix/pokemon-move-methods-index
Jul 15, 2026
Merged

Fix pokemon moves lookup assuming contiguous ids#1603
Naramsim merged 1 commit into
PokeAPI:masterfrom
santichausis:fix/pokemon-move-methods-index

Conversation

@santichausis

Copy link
Copy Markdown
Contributor

Problem

PokemonDetailSerializer.get_pokemon_moves looks up VersionGroup and MoveLearnMethod summaries by list position (id - 1), assuming both tables form a contiguous 1-indexed sequence with no gaps.

Any gap in either table (e.g. a deleted row, or a new entry inserted with an id that doesn't match its position) causes the wrong version group / move learn method to be returned for a move, or raises an IndexError.

Fix

Build a dict keyed by the actual id instead of relying on list position, so lookups are correct regardless of gaps in the id sequence.

Test plan

  • Added test_pokemon_moves_with_non_contiguous_version_group_and_method_ids, which creates and deletes a VersionGroup and a MoveLearnMethod to force a gap, then asserts the pokemon endpoint still returns the correct version group / move learn method.
  • Verified this test reproduces the IndexError against the old code and passes with the fix.
  • manage.py test pokemon_v2 passes (55 tests).
  • black --check passes.

Fixes #1567

VersionGroup and MoveLearnMethod summaries were looked up by list
position (id - 1), assuming ids form a contiguous sequence starting
at 1. Any gap in either table (e.g. a deleted row) causes the wrong
version group or learn method to be returned, or an IndexError.

Fixes PokeAPI#1567

@Naramsim Naramsim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi! Thanks for the submission! The passing tests tell me that everything is working as expected. I honestly don't understand very well the additional python test you added, but that's my lack of knowledge. I'll merge this, thanks for the contribution!

Can you spot in the code base other issues like this one?

@Naramsim Naramsim merged commit e39c910 into PokeAPI:master Jul 15, 2026
19 checks passed
@pokeapi-machine-user

Copy link
Copy Markdown

A PokeAPI/api-data refresh has started. In ~45 minutes the staging branch of PokeAPI/api-data will be pushed with the new generated data.

The staging branch will be deployed in our staging environment and the entire API will be ready to review.

A Pull Request (master<-staging) will be also created at PokeAPI/api-data and assigned to the PokeAPI Core team to be reviewed. If approved and merged new data will soon be available worldwide at pokeapi.co.

@pokeapi-machine-user

Copy link
Copy Markdown

The updater script has finished its job and has now opened a Pull Request towards PokeAPI/api-data with the updated data.

The Pull Request can be seen deployed in our staging environment when CircleCI deploy will be finished (check the start time of the last build).

@santichausis

Copy link
Copy Markdown
Contributor Author

Thanks for merging! Yes, I found at least one more spot with the same pattern: LocationAreaDetailSerializer.get_encounters in pokemon_v2/serializers.py (around line 1222) does the same version_data[ver["version"] - 1] list-index lookup instead of keying by the actual Version id. It has the same failure mode as this one (wrong version returned, or IndexError, if the version ids aren't a contiguous 1-indexed sequence).

This one was actually touched before in #1314 (closed without merging), so there's some history there. Happy to open a follow-up PR fixing it the same way (dict keyed by id) with a regression test, if that's useful — let me know.

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.

pokemon_move_methods and version_groups assume 1-indexed lists with no gaps

3 participants