Skip to content

fix: DynamicCommunitySelection crashes with KeyError when no communities at level 0#2384

Open
hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-2382-dcs-level0-keyerror
Open

fix: DynamicCommunitySelection crashes with KeyError when no communities at level 0#2384
hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-2382-dcs-level0-keyerror

Conversation

@hanhan761
Copy link
Copy Markdown

Fixes #2382

Problem

DynamicCommunitySelection.__init__ at dynamic_community_selection.py:71 accesses self.levels["0"] directly. When the input data contains no communities at level "0" (e.g., all communities start at level "1" or the list is empty), this raises KeyError: '0' and crashes the dynamic community selection flow.

self.levels is built from the communities list — if no community has level="0", the key is never added.

Fix

Replace self.levels["0"] with self.levels.get("0", []) so that starting_communities defaults to an empty list when no root-level communities exist.

Test

Added test_dynamic_community_selection_no_level_zero which creates communities only at levels 1 and 2 (no level 0) and verifies:

  • The constructor does not raise KeyError
  • selector.starting_communities is an empty list
  • "0" is absent from selector.levels

@hanhan761 hanhan761 requested a review from a team as a code owner May 30, 2026 08:58
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.

DynamicCommunitySelection crashes with KeyError when no communities at level 0

1 participant